Algorithm 문제풀이/simulation

[BOJ] 백준 14503 - 로봇청소기(JAVA)

cjsong 2019. 7. 16. 00:58

문제 풀이 순서대로 코드 구현

dx도 북동남서 순서대로 선언해줬으면 더 편하게 풀었을 거 같음

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package algo;
 
 
public class Main14503_로봇청소기 {
    private static int N;
    private static int M;
    private static int r;
    private static int c;
    private static int d;
    private static int[][] map;
    private static boolean[][] visited;
    static int[] dx = { -1100 }; // 북,남,동,서
    static int[] dy = { 001-1 }; 
    private static int result;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(br.readLine(), " ");
        r = Integer.parseInt(st.nextToken());
        c = Integer.parseInt(st.nextToken());
        d = Integer.parseInt(st.nextToken());
 
        map = new int[N][M];
        visited = new boolean[N][M];
 
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < M; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        result = 1;
        visited[r][c] = true;
        clean(r, c, d, 0);
        System.out.println(result);
 
    }// end of main
    
    // 0 - 북 , 1 - 동 , 2 - 남 , 3 - 서
    public static void clean(int x, int y, int dir, int count) {
        if (count == 4) {
            if(dir == 0) {
                int nx = x + dx[1];
                int ny = y + dy[1];
                if(!check(nx,ny) || map[nx][ny] ==1) {
                    return;
                }else {
                    clean(nx,ny,0,0);
                }
            }else if(dir ==1) {
                int nx = x + dx[3];
                int ny = y + dy[3];
                if(!check(nx,ny) || map[nx][ny]==1) {
                    return;
                }else {
                    clean(nx,ny,1,0);
                }
            }else if(dir ==2) {
                int nx = x + dx[0];
                int ny = y + dy[0];
                if(!check(nx,ny) || map[nx][ny]==1) {
                    return;
                }else {
                    clean(nx,ny,2,0);
                }
            }else if(dir ==3) {
                int nx = x + dx[2];
                int ny = y + dy[2];
                if(!check(nx,ny) || map[nx][ny]==1) {
                    return;
                }else {
                    clean(nx,ny,3,0);
                }
            }
        } else {
            
            if (dir == 0) {
                int nx = x + dx[3];
                int ny = y + dy[3];
                if (check(nx, ny) && map[nx][ny] == 0 && !visited[nx][ny]) {
                    visited[nx][ny] = true;
                    result++;
                    clean(nx, ny, 30);
//                    print();
                } else {
                    clean(x, y, 3, count+1);
                }
            } else if (dir == 1) {
                int nx = x + dx[0];
                int ny = y + dy[0];
                if (check(nx, ny) && map[nx][ny] == 0 && !visited[nx][ny]) {
                    visited[nx][ny] = true;
                    result++;
                    clean(nx, ny, 00);
//                    print();
                } else {
                    clean(x, y, 0, count+1);
                }
            } else if (dir == 2) {
 
                int nx = x + dx[2];
                int ny = y + dy[2];
                if (check(nx, ny) && map[nx][ny] == 0 && !visited[nx][ny]) {
                    visited[nx][ny] = true;
                    result++;
                    clean(nx, ny, 10);
//                    print();
                } else {
                    clean(x, y, 1, count+1);
                }
            } else if (dir == 3) {
                int nx = x + dx[1];
                int ny = y + dy[1];
                if (check(nx, ny) && map[nx][ny] == 0 && !visited[nx][ny]) {
                    visited[nx][ny] = true;
                    result++;
                    clean(nx, ny, 20);
//                    print();
                } else {
                    clean(x, y, 2, count+1);
                }
            }
        }
    }// end of clean
 
    public static boolean check(int x, int y) {
        if (x >= 0 && y >= 0 && x < N && y < M) {
            return true;
        }
        return false;
    }// end of check
    
    public static void print() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if(visited[i][j] == true) {
                    System.out.print(1 + " ");
                }else {
                    System.out.print(0 + " ");
                }
            }
            System.out.println();
            
        }
        System.out.println(result);
        System.out.println("=================");
    }
 
}// end of class
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs