Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void dfsCycle(ArrayList<int[]> cycleVertexAndSize, char[][] map, int x, int y, boolean[][] visited, boolean[][] onStack, ArrayDeque<int[]> stack) {
- visited[x][y] = true;
- onStack[x][y] = true;
- int[] ar = {x, y};
- stack.push(ar);
- ar = null;
- int nextX;
- int nextY;
- if (map[x][y] == 'R') {
- nextX = x;
- nextY = y + 1;
- } else if (map[x][y] == 'L') {
- nextX = x;
- nextY = y - 1;
- } else if (map[x][y] == 'U') {
- nextX = x - 1;
- nextY = y;
- } else {
- nextX = x + 1;
- nextY = y;
- }
- if (!visited[nextX][nextY]) {
- dfsCycle(cycleVertexAndSize, map, nextX ,nextY, visited, onStack, stack);
- } else {
- if (onStack[nextX][nextY]) {
- int circleSize = 1;
- while (!(stack.peek()[0] == nextX && stack.peek()[1] == nextY)) {
- stack.pop();
- circleSize++;
- }
- int[] vertexAndSize = new int[3];
- vertexAndSize[0] = nextX;
- vertexAndSize[1] = nextY;
- vertexAndSize[2] = circleSize;
- cycleVertexAndSize.add(vertexAndSize);
- vertexAndSize = null;
- }
- }
- if (!stack.isEmpty() && stack.peek()[0] == x && stack.peek()[1] == y) {
- stack.pop();
- }
- onStack[x][y] = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement