Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. private static void dfsCycle(ArrayList<int[]> cycleVertexAndSize, char[][] map, int x, int y, boolean[][] visited, boolean[][] onStack, ArrayDeque<int[]> stack) {
  2.         visited[x][y] = true;
  3.         onStack[x][y] = true;
  4.         int[] ar = {x, y};
  5.         stack.push(ar);
  6.         ar = null;
  7.  
  8.         int nextX;
  9.         int nextY;
  10.         if (map[x][y] == 'R') {
  11.             nextX = x;
  12.             nextY = y + 1;
  13.         } else if (map[x][y] == 'L') {
  14.             nextX = x;
  15.             nextY = y - 1;
  16.         } else if (map[x][y] == 'U') {
  17.             nextX = x - 1;
  18.             nextY = y;
  19.         } else {
  20.             nextX = x + 1;
  21.             nextY = y;
  22.         }
  23.  
  24.         if (!visited[nextX][nextY]) {
  25.             dfsCycle(cycleVertexAndSize, map, nextX ,nextY, visited, onStack, stack);
  26.         } else {
  27.             if (onStack[nextX][nextY]) {
  28.                 int circleSize = 1;
  29.                 while (!(stack.peek()[0] == nextX && stack.peek()[1] == nextY)) {
  30.                     stack.pop();
  31.                     circleSize++;
  32.                 }
  33.  
  34.                 int[] vertexAndSize = new int[3];
  35.                 vertexAndSize[0] = nextX;
  36.                 vertexAndSize[1] = nextY;
  37.                 vertexAndSize[2] = circleSize;
  38.                 cycleVertexAndSize.add(vertexAndSize);
  39.                 vertexAndSize = null;
  40.             }
  41.         }
  42.  
  43.         if (!stack.isEmpty() && stack.peek()[0] == x && stack.peek()[1] == y) {
  44.             stack.pop();
  45.         }
  46.         onStack[x][y] = false;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement