Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1.     public static Node[][] initGraph(int a, int b) {
  2.         Node[][] graph = new Node[a+1][b+1];
  3.        
  4.         for (int i = 0; i < graph.length; i++) {
  5.             for (int j = 0; j < graph[i].length; j++) {
  6.                 graph[i][j] = new Node(i, j);
  7.             }
  8.         }
  9.        
  10.         for (int i = 1; i < graph.length - 1; i++) {
  11.             for (int j = 0; j < graph[i].length; j++) {
  12.                 graph[i][j].edges.add(graph[i+1][j]);
  13.                 graph[i][j].edges.add(graph[i-1][j]);
  14.                 if (j == 0) graph[i][j].edges.add(graph[i][j+1]);
  15.                 else if (j == graph[i].length - 1) graph[i][j].edges.add(graph[i][j-1]);
  16.                 else {
  17.                     graph[i][j].edges.add(graph[i][j-1]);
  18.                     graph[i][j].edges.add(graph[i][j+1]);
  19.                 }
  20.             }
  21.         }
  22.        
  23.         int i = 0;
  24.        
  25.         for (int j = 0; j < graph[i].length; j++) {
  26.             graph[i][j].edges.add(graph[i+1][j]);
  27.             if (j == 0) graph[i][j].edges.add(graph[i][j+1]);
  28.             else if (j == graph[i].length - 1) graph[i][j].edges.add(graph[i][j-1]);
  29.             else {
  30.                 graph[i][j].edges.add(graph[i][j-1]);
  31.                 graph[i][j].edges.add(graph[i][j+1]);
  32.             }
  33.         }
  34.        
  35.         i = graph.length - 1;
  36.        
  37.         for (int j = 0; j < graph[graph.length - 1].length; j++) {
  38.             graph[i][j].edges.add(graph[i-1][j]);
  39.             if (j == 0) graph[i][j].edges.add(graph[i][j+1]);
  40.             else if (j == graph[i].length - 1) graph[i][j].edges.add(graph[i][j-1]);
  41.             else {
  42.                 graph[i][j].edges.add(graph[i][j-1]);
  43.                 graph[i][j].edges.add(graph[i][j+1]);
  44.             }
  45.         }
  46.        
  47.         return graph;
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement