1+ // [실버1] 2615번. 오목
2+ // 메모리 : 11772 KB, 시간 : 68 ms
3+
4+ import java .io .*;
5+ import java .util .*;
6+
7+ class P2615 {
8+
9+ static int [][] board = new int [19 ][19 ];
10+
11+ // 가로, 세로, 대각선(↘), 대각선(↗)
12+ static int [] dr = {0 , 1 , 1 , -1 };
13+ static int [] dc = {1 , 0 , 1 , 1 };
14+
15+ public static void main (String [] args ) throws Exception {
16+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
17+ for (int i = 0 ; i < 19 ; i ++) {
18+ StringTokenizer st = new StringTokenizer (br .readLine ());
19+ for (int j = 0 ; j < 19 ; j ++) {
20+ board [i ][j ] = Integer .parseInt (st .nextToken ());
21+ }
22+ }
23+
24+ // 승리 여부 체크
25+ for (int i = 0 ; i < 19 ; i ++) {
26+ for (int j = 0 ; j < 19 ; j ++) {
27+ if (board [i ][j ] != 0 && isWin (i , j )) {
28+ System .out .println (board [i ][j ]);
29+ System .out .println ((i + 1 ) + " " + (j + 1 ));
30+ return ;
31+ }
32+ }
33+ }
34+
35+ // 승리하는 경우 X
36+ System .out .println (0 );
37+ }
38+
39+ // 0 (알이 놓이지 않은 자리), 1 (검은 바둑알), 2 (흰 바둑알)
40+ static boolean isWin (int r , int c ) {
41+ int stone = board [r ][c ];
42+
43+ for (int d = 0 ; d < 4 ; d ++) {
44+ // 이전 방향의 돌이 같은 돌이면 시작점이 아님
45+ int prevR = r - dr [d ];
46+ int prevC = c - dc [d ];
47+ if (isValid (prevR , prevC ) && board [prevR ][prevC ] == stone ) {
48+ continue ;
49+ }
50+
51+ int cnt = 1 ;
52+ int nr = r + dr [d ];
53+ int nc = c + dc [d ];
54+
55+ while (isValid (nr , nc ) && board [nr ][nc ] == stone ) {
56+ cnt ++;
57+ nr += dr [d ];
58+ nc += dc [d ];
59+ }
60+
61+ // 5개인지 확인 (6개 이상이면 무효)
62+ if (cnt == 5 ) {
63+ int nextR = r + dr [d ] * 5 ;
64+ int nextC = c + dc [d ] * 5 ;
65+ if (isValid (nextR , nextC ) && board [nextR ][nextC ] == stone ) {
66+ continue ;
67+ }
68+ return true ;
69+ }
70+ }
71+ return false ;
72+ }
73+
74+ static boolean isValid (int r , int c ) {
75+ return r >= 0 && r < 19 && c >= 0 && c < 19 ;
76+ }
77+ }
0 commit comments