是记录
思路
我并没有想到用两点之间距离公式这个,其他的倒是想到了
将输入的坐标存储到x与y到role当中,递归思路
将used[u]设置为true,遍历所有修仙者的位置,若出现没有访问过且离当前修仙者的距离小于等于D就将该修仙者传入dfs中进行下一次递归即可
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Role{
double x,y;
public Role(double x,double y){
this.x = x;
this.y = y;
}
}
public class Main{
static Role[] roles;
static int D;
static boolean[] used;
public static double getDis(Role u,Role i){
return Math.sqrt(Math.pow(u.x-i.x,2)+Math.pow(u.y-i.y,2));
}
public static void dfs(int u){
used[u]=true;
for(int i=0;i<roles.length;i++){
if(!used[i] && getDis(roles[u],roles[i])<=D){
dfs(i);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cnt = Integer.parseInt(br.readLine());
roles = new Role[cnt];
for(int i=0;i<cnt;i++){
String[] content = br.readLine().split(" ");
double a = Integer.parseInt(content[0]);
double b = Integer.parseInt(content[1]);
roles[i] = new Role(a,b);
}
D = Integer.parseInt(br.readLine());
used = new boolean[1010];
dfs(0);
for(int i=0;i<roles.length;i++){
if(used[i])
System.out.println(1);
else
System.out.println(0);
}
br.close();
}
}