From a8a4cafacf3e34515f6e916e867c758330fb0a50 Mon Sep 17 00:00:00 2001 From: "WooJin Park(Ader)" Date: Wed, 18 May 2022 03:38:28 +0900 Subject: [PATCH] =?UTF-8?q?add=20:=20Programmers=2080652=20=EB=B9=9B?= =?UTF-8?q?=EC=9D=98=EA=B2=BD=EB=A1=9C=EC=82=AC=EC=9D=B4=ED=81=B4=20?= =?UTF-8?q?=ED=92=80=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 빛의경로사이클 문제 풀이 추가 end --- ...\354\202\254\354\235\264\355\201\264.java" | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 "src/\354\225\204\353\215\224/Pro_86052_\353\271\233\354\235\230\352\262\275\353\241\234\354\202\254\354\235\264\355\201\264.java" diff --git "a/src/\354\225\204\353\215\224/Pro_86052_\353\271\233\354\235\230\352\262\275\353\241\234\354\202\254\354\235\264\355\201\264.java" "b/src/\354\225\204\353\215\224/Pro_86052_\353\271\233\354\235\230\352\262\275\353\241\234\354\202\254\354\235\264\355\201\264.java" new file mode 100644 index 0000000..084f9f0 --- /dev/null +++ "b/src/\354\225\204\353\215\224/Pro_86052_\353\271\233\354\235\230\352\262\275\353\241\234\354\202\254\354\235\264\355\201\264.java" @@ -0,0 +1,97 @@ +package 아더; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Pro_86052_빛의경로사이클 { + + public static void main(String[] args) { + Pro_86052_빛의경로사이클 pro = new Pro_86052_빛의경로사이클(); + String[] grid1 = {"SL","LR"}; + String[] grid2 = {"S"}; + String[] grid3 = {"R","R"}; + + System.out.println(Arrays.toString(pro.solution(grid1))); + System.out.println(Arrays.toString(pro.solution(grid2))); + System.out.println(Arrays.toString(pro.solution(grid3))); + } + + public int[] solution(String[] grid) { + List answer = new ArrayList<>(); + int[][] dir = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 서 북 동 남 + + int row = grid.length; + int col = grid[0].length(); + char[][] adj = new char[row][col]; + boolean [][][] visited = new boolean[row][col][dir.length]; + + // 입력 받기 + for (int i = 0; i < row; i++) { + String[] split = grid[i].split(""); + for (int j = 0; j < col; j++) { + adj[i][j] = split[j].charAt(0); + } + } + + // 로직 + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + for (int k = 0; k < dir.length; k++) { + if (visited[i][j][k]) continue; + int cycleCount = 0; + int tempRow = i; + int tempCol = j; + int tempDir = k; + + // 4방향에서 각 점에 접근한다 + while (true) { + // 각 점의 값에 따라 이동 및 방향 변경 + if (adj[tempRow][tempCol] == 'S') { + // 방향 변경할 필요 없음 + tempRow = (tempRow + dir[tempDir][0] % 4) % row; + tempCol = (tempCol + dir[tempDir][1] % 4) % col; + } else if (adj[tempRow][tempCol] == 'L') { + tempDir = (tempDir - 1 + 4) % 4; + tempRow = (tempRow + dir[(tempDir) % 4][0]) % row; + tempCol = (tempCol + dir[(tempDir) % 4][1]) % col; + } else if (adj[tempRow][tempCol] == 'R') { + tempDir = (tempDir + 1 + 4) % 4; + tempRow = (tempRow + dir[(tempDir) % 4][0]) % row; + tempCol = (tempCol + dir[(tempDir) % 4][1]) % col; + } + + // temp가 범위를 벗어나면 보정 + if (tempRow < 0) { + tempRow = row - 1; + } + if (tempCol < 0) { + tempCol = col - 1; + } + if (tempRow >= row) { + tempRow = 0; + } + if (tempCol >= col) { + tempCol = 0; + } + + // 방문했던 곳이면 사이클임 + if (visited[tempRow][tempCol][tempDir]) { + break; + } + + // 방문 처리 및 사이클 수 증가 + visited[tempRow][tempCol][tempDir] = true; + cycleCount++; + } + answer.add(cycleCount); + } + } + } + + return answer.stream() + .mapToInt(num -> num) + .sorted() + .toArray(); + } +}