|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +public class Main{ |
| 6 | + private static int N, M; |
| 7 | + private static int Vd; // 0북 1동 2남 |
| 8 | + private static char[][] Map; |
| 9 | + private static int Vr, Vc; |
| 10 | + private static final int[][] Drdcs = { |
| 11 | + {-1, 0}, |
| 12 | + {0, 1}, |
| 13 | + {1, 0}, |
| 14 | + {0, -1} |
| 15 | + }; |
| 16 | + public static void main(String[] args) throws IOException { |
| 17 | + /* |
| 18 | + 로봇청소기 N*M |
| 19 | + (0, 0) 시작 |
| 20 | + N, M <= 50, |
| 21 | + d = 0북 1동 2남 |
| 22 | + 작동 멈출때까지 청소하는 칸 개수 |
| 23 | + */ |
| 24 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 25 | + String[] tokens = br.readLine().split(" "); |
| 26 | + N = Integer.parseInt(tokens[0]); |
| 27 | + M = Integer.parseInt(tokens[1]); |
| 28 | + Map = new char[N+2][M+2]; |
| 29 | + tokens = br.readLine().split(" "); |
| 30 | + Vr = Integer.parseInt(tokens[0]) + 1; |
| 31 | + Vc = Integer.parseInt(tokens[1]) + 1; |
| 32 | + Vd = Integer.parseInt(tokens[2]); |
| 33 | + for(int c = 0; c<M+2; c++){ |
| 34 | + for(int r : new int[]{0, N+1}){ |
| 35 | + Map[r][c] = 'W'; |
| 36 | + } |
| 37 | + } |
| 38 | + for(int r=1; r<N+1; r++){ |
| 39 | + for(int c: new int[]{0, M+1}){ |
| 40 | + Map[r][c] = 'W'; |
| 41 | + } |
| 42 | + } |
| 43 | + for(int r = 1; r<N+1; r++){ |
| 44 | + tokens = br.readLine().split(" "); |
| 45 | + for(int c = 1; c<M+1; c++){ |
| 46 | + int isWall = Integer.parseInt(tokens[c-1]); |
| 47 | + if(isWall == 1){ |
| 48 | + Map[r][c] = 'W'; |
| 49 | + }else{ |
| 50 | + Map[r][c] = 'X'; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + int answer = 0; |
| 55 | + while(true) { |
| 56 | + //1. 현재칸 청소X->현재칸 청소 |
| 57 | + if(Map[Vr][Vc] == 'X'){ |
| 58 | + Map[Vr][Vc] = 'O'; |
| 59 | + answer++; |
| 60 | + } |
| 61 | + //2. 현재칸 주변4칸에 청소되지않은 빈칸이없을때 |
| 62 | + boolean isNotX = true; |
| 63 | + for(int i = -1; i>= -4; i--){ |
| 64 | + int nd = addDirenction(Vd, i); |
| 65 | + int nr = Vr + Drdcs[nd][0]; |
| 66 | + int nc = Vc + Drdcs[nd][1]; |
| 67 | + if(Map[nr][nc] == 'X'){ |
| 68 | + Vd = nd; |
| 69 | + Vr = nr; |
| 70 | + Vc = nc; |
| 71 | + isNotX = false; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + if(isNotX){ |
| 76 | + int nr = Vr + Drdcs[addDirenction(Vd, 2)][0]; |
| 77 | + int nc = Vc + Drdcs[addDirenction(Vd, 2)][1]; |
| 78 | + if(Map[nr][nc] != 'W'){ |
| 79 | + Vr = nr; Vc = nc; |
| 80 | + continue; |
| 81 | + }else{ |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + // 2-1.후진가능-> 한칸 후진 후 1로 |
| 86 | + // 2-2.후진불가능->작동멈춤 |
| 87 | + //3. 현재칸 주변 4칸 중 청소된지않은 빈칸 존재 |
| 88 | + // 3-1. 반시계90도회전 |
| 89 | + // 3-2.현 방향 앞이 청소되지않았으면 한칸 전진 |
| 90 | + } |
| 91 | + System.out.println(answer); |
| 92 | + br.close(); |
| 93 | + } |
| 94 | + private static int addDirenction(int vd, int i){ |
| 95 | + return (vd + Drdcs.length) % Drdcs.length; |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | +``` |
0 commit comments