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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package backjun;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class backjun_2615_오목 {
 
    private static int[][] map;
    private static int[][] visit;
    private static int cnt, ans_x, ans_y;
    private static boolean f_five, f_six, s_five, s_six;
 
    private static boolean range(int x, int y) {
        if (x < 0 || x >= 19 || y < 0 || y >= 19)
            return false;
        else
            return true;
    }
 
    private static void DFS(int nn) {
        int x = nn / 19;
        int y = nn % 19;
 
        int tempx = x;
        int tempy = y;
 
        f_five = false;
        f_six = false;
        if (nn >= 18 * 19 + 18)
            return;
        // 5개가 만들어지는 경우와 6개가 만들어지는 경우 살펴본다.
        if (map[x][y] == 1) {
            // 직선
 
            if(y==0 || (range(x,y-1&& map[x][y-1]!=1)) { //이전 오목 
            cnt = 1;
            k: for (int i = 1; i < 6; i++) {
                y += 1;
                if (range(x, y)) { // 범위에 들어간다면
                    if (map[x][y] == 1) {
                        
                        cnt += 1;
                    }
                    else
                        break k;
                } else {
                    ;
                    // 다섯개와 여섯개 둘다 못만드므로 false;
                }
                if (cnt >=6 || cnt<5) {
                    f_six = true;
                }
            }
            if (cnt == 5) {
                f_five = true;
                ans_x = x + 1;
                ans_y = tempy + 1;
                return;
            }
 
            x = tempx;
            y = tempy;
            // 오른쪽 대각선
            }
            
            if(x==0 || y==0 || (range(x-1,y-1&& map[x-1][y-1]!=1)) {
            cnt = 1;
            k: for (int i = 1; i < 6; i++) {
                x += 1;
                y += 1;
                if (range(x, y)) { // 범위에 들어간다면
                    if (map[x][y] == 1)
                        cnt += 1;
                    else
                        break k;
                } else {
                    ;
                    // 다섯개와 여섯개 둘다 못만드므로 false;
                }
                if (cnt >=6 || cnt<5) {
                    f_six = true;
                }
            }
 
            if (cnt == 5) {
                ans_x = tempx + 1;
                ans_y = tempy + 1;
                f_five = true;
                return;
            }
            }
            x = tempx;
            y = tempy;
            // 아래
            cnt = 1;
            if(x==0 || (range(x-1,y) && map[x-1][y] !=1)) {
            k: for (int i = 1; i < 6; i++) {
                x += 1;
                if (range(x, y)) { // 범위에 들어간다면
                    if (map[x][y] == 1)
                        cnt += 1;
                    else
                        break k;
                } else {
                    ;
                    // 다섯개와 여섯개 둘다 못만드므로 false;
                }
 
                if (cnt >=6 || cnt<5) {
                    f_six = true;
                }
            }
 
            if (cnt == 5) {
                ans_x = tempx + 1;
                ans_y = tempy + 1;
                f_five = true;
                return;
            }
            }
            x = tempx;
            y = tempy;
            // 아래
            
            if(x==0 || (range(x-1,y+1&& map[x-1][y+1]!=1)) {
            cnt = 1;
            k: for (int i = 1; i < 6; i++) {
                x += 1;
                y -= 1;
                if (range(x, y)) { // 범위에 들어간다면
                    if (map[x][y] == 1)
                        cnt += 1;
                    else
                        break k;
                } else {
                    ;
                    // 다섯개와 여섯개 둘다 못만드므로 false;
                }
                if (cnt >=6 || cnt<5) {
                    f_six = true;
                }
            }
 
            if (cnt == 5) {
                ans_x = x;
                ans_y = y + 2;
                f_five = true;
                return;
            }
            }
        } else if (map[x][y] == 2) {
            // 직선
            x = tempx;
            y = tempy;
 
            if(y==0 || (range(x,y-1&& map[x][y-1]!=2)) {
            cnt = 1;
            k: for (int i = 1; i < 6; i++) {
                y += 1;
                if (range(x, y)) { // 범위에 들어간다면
                    if (map[x][y] == 2)
                        cnt += 1;
                    else
                        break k;
                } else {
                    ;
                    // 다섯개와 여섯개 둘다 못만드므로 false;
                }
                if (cnt >=6 || cnt<5) {
                    s_six = true;
                }
            }
            if (cnt == 5) {
                ans_x = x + 1;
                ans_y = tempy + 1;
                s_five = true;
                return;
            }
            }
            // 오른쪽 대각선
            x = tempx;
            y = tempy;
            
            cnt = 1;
            if(x==0 || y ==0 || (range(x-1,y-1&& map[x-1][y-1]!=2)) {
            k: for (int i = 1; i < 6; i++) {
                x += 1;
                y += 1;
                if (range(x, y)) { // 범위에 들어간다면
                    if (map[x][y] == 2)
                        cnt += 1;
                    else
                        break k;
                } else {
                    ;
                    // 다섯개와 여섯개 둘다 못만드므로 false;
                }
                if (cnt >=6 || cnt<5) {
                    s_six = true;
                }
            }
 
            if (cnt == 5) {
                ans_x = tempx + 1;
                ans_y = tempy + 1;
                s_five = true;
                return;
            }
            }
            // 아래
            x = tempx;
            y = tempy;
            cnt = 1;
            if(x==0 || (range(x-1,y) && map[x-1][y]!=2)) {
            k: for (int i = 1; i < 6; i++) {
                x += 1;
                if (range(x, y)) { // 범위에 들어간다면
                    if (map[x][y] == 2)
                        cnt += 1;
                    else
                        break k;
                } else {
                    ;
                    // 다섯개와 여섯개 둘다 못만드므로 false;
                }
                if (cnt >=6 || cnt<5) {
                    s_six = true;
                }
            }
 
            if (cnt == 5) {
                ans_x = tempx + 1;
                ans_y = tempy + 1;
                s_five = true;
                return;
            }
            }
            x = tempx;
            y = tempy;
            // 아래
            cnt = 1;
            if(x==0 ||  (range(x-1,y+1&& map[x-1][y+1]!=2)) {
            k: for (int i = 1; i < 6; i++) {
                x += 1;
                y -= 1;
                if (range(x, y)) { // 범위에 들어간다면
                    if (map[x][y] == 2)
                        cnt += 1;
                    else
                        break k;
                } else {
                    ;
                    // 다섯개와 여섯개 둘다 못만드므로 false;
                }
                if (cnt >=6 || cnt<5 ) {
                    s_six = true;
                }
            }
 
            if (cnt == 5) {
                ans_x = x;
                ans_y = y + 2;
                s_five = true;
                return;
            }
            }
        }
 
        DFS(nn + 1);
 
    }
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        map = new int[19][19];
        visit = new int[19][19];
        for (int i = 0; i < 19; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < 19; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        } // end of for loop
 
        // System.out.println("debug");
        // 출력할때는 x,y 좌표 각각에 +1 씩 해두자.
 
        DFS(0);
 
        if (f_five == true) {
            System.out.println("1");
            System.out.println(ans_x + " " + ans_y);
 
        }else if (f_six == true) {
            System.out.println("0");
 
        }else if (s_five == true) {
            System.out.println("2");
            System.out.println(ans_x + " " + ans_y);
        } else if (s_six == true) {
            System.out.println("0");
        }
 
    }
}
 
cs

시뮬레이션 문제

 

헷갈렸던 부분

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if(range(x,y-1&& map[x][y-1]!=1) { //이전 오목, 심지어 x가 0인 경우 y가 0인경우 조건문에
                                                                         // 들어오지 않아서 체크해야 했다.
                                                         // 여러 시뮬레이션 문제를 풀어보자. 
 
if (cnt >=6 || cnt<5) {
                    f_six = true;
}//6개 이상이거나 5개 미만 인 경우 체크 
 
if (f_five == true) {
            System.out.println("1");
            System.out.println(ans_x + " " + ans_y);
        }else if (f_six == true) {
            System.out.println("0");
        }else if (s_five == true) {
            System.out.println("2");
            System.out.println(ans_x + " " + ans_y);
        } else if (s_six == true) {
            System.out.println("0");
        }
//if-else case 가 중간에 하나걸리면 끝이기 때문에 
//1 인 경우 5개 모인경우와 모이지 않은 경우
//2 인 경우 5개 모인경우와 모이지 않은 경우 
//순서대로 진행된다. 
cs