Java HashMap实现邻接列表 类图{ //每个节点的邻接列表的映射 地图; 公共图(ArrayList节点){ adj=新的HashMap(); 对于(int i=0;i
不要将点存储在Java HashMap实现邻接列表 类图{ //每个节点的邻接列表的映射 地图; 公共图(ArrayList节点){ adj=新的HashMap(); 对于(int i=0;i,java,hashmap,Java,Hashmap,不要将点存储在数组中。将坐标封装在定义的点中,并将其存储在HashMap中。点具有点坐标的成员。别忘了为点实现等于和hashCode 不要将点存储在数组中。将坐标封装在定义的点中,并将其存储在HashMap中。点具有点坐标的成员。不要忘了为点实现等于和hashCode class Graph { //Map of adjacency lists for each node Map<int[], LinkedList<int[]>> adj; publi
数组中。将坐标封装在定义的点中,并将其存储在HashMap
中。点具有点坐标的成员。别忘了为点实现等于和hashCode
不要将点存储在数组中
。将坐标封装在定义的点
中,并将其存储在HashMap
中。点
具有点坐标的成员。不要忘了为点
实现等于和hashCode
class Graph {
//Map of adjacency lists for each node
Map<int[], LinkedList<int[]>> adj;
public Graph(ArrayList<int[]> nodes) {
adj = new HashMap<int[], LinkedList<int[]>>();
for (int i = 0; i < nodes.size(); ++i) {
adj.put(nodes.get(i), new LinkedList<int[]>());
}
}
public void addNeighbor(int [] a, int [] b) {
adj.get(a).add(b);
}
public LinkedList<int[]> getNeighbors(int a[]) {
return adj.get(a);
}
}
public class Assignment2 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int x= sc.nextInt();
int y= sc.nextInt();
int n= sc.nextInt();
ArrayList<int []> al= new ArrayList<>();
for(int i=0;i<n;i++){
int[] a = new int[2];
a[0]=sc.nextInt();
a[1]=sc.nextInt();
al.add(i, a);
}
int[] s={0,100};
int[] t={x-5,150};
Graph g = new Graph(al);
g.adj.put(s, new LinkedList<int[]>());
g.adj.put(t, new LinkedList<int[]>());
for(int i=0;i<al.size();i++){
int a[]=al.get(i);
for(int j=i;j<al.size();j++){
int b[]=al.get(j);
int r=100;
int value=(int) (Math.pow(a[0]-b[0],2)+Math.pow(a[1]-b[1],2));
if(0<=value && value <=200){
g.addNeighbor(a, b);
g.addNeighbor(b, a);
}
}
}
}