题目:An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.
In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
import java.util.Scanner;
public class Wireless1 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int d=in.nextInt();
d*=d;
int[] arrx=new int[n+1];
int[] arry=new int[n+1];
int[] arr=new int[n+1];
int[] bool=new int[n+1];
for(int i=1;i<=n;i++){
arr[i]=i;
}
for(int i=1;i<=n;i++){
arrx[i]=in.nextInt();
arry[i]=in.nextInt();
}
while(in.hasNext())
{
if(in.next().equals("O")){
int w=in.nextInt();
if(bool[w]==1) continue;
for(int j=1;j<=n;j++){
if(bool[j]==0) continue;
int u=j;
while(arr[u]!=u)
u=arr[u];
int l=w;
while(arr[l]!=l)
l=arr[l];
if(l==u) continue;
int x=arrx[w]-arrx[j];
int y=arry[w]-arry[j];
if(x*x+y*y<=d)
{
if(u>l) arr[j]=arr[w]=arr[u]=l;
else arr[j]=arr[w]=arr[l]=u;
}
}
bool[w]=1;
}
else
{
int r=in.nextInt(),w=r;
while(arr[w]!=w)
w=arr[w];
int t=in.nextInt(),u=t;
while(arr[u]!=u)
u=arr[u];
arr[r]=w;
arr[t]=u;
if(arr[w]==arr[u]&&bool[w]==1) System.out.println("SUCCESS");
else System.out.println("FAIL");
}
}
// TODO Auto-generated method stub
}
}
无线网络修复模拟
本文介绍了一个模拟无线网络修复过程的Java程序。该程序通过输入坐标和距离限制来判断计算机间是否能建立通信,并实现了网络修复操作及通信测试功能。
330

被折叠的 条评论
为什么被折叠?



