여니의 취준 준비/코딩테스트 (Java)

[2615] 오목 in Java

여니's 2022. 8. 5. 15:21

https://www.acmicpc.net/problem/2615

 

2615번: 오목

오목은 바둑판에 검은 바둑알과 흰 바둑알을 교대로 놓아서 겨루는 게임이다. 바둑판에는 19개의 가로줄과 19개의 세로줄이 그려져 있는데 가로줄은 위에서부터 아래로 1번, 2번, ... ,19번의 번호

www.acmicpc.net


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
package study;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class n2615 {
 
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        int[][] array=new int[19][19];
        for(int i=0; i<19; i++) {
            st = new StringTokenizer(br.readLine());
            for(int j=0; j<19; j++) {
                array[i][j]=Integer.parseInt(st.nextToken());
            }
        }
        boolean button=false;
        int answer=0;
        int x=0int y=0// 위치
        
        int[] dxs= {0,1,1,-1}; // 우상, 우, 우하, 하
        int[] dys= {1,1,0,1};
        
        for(int i=0; i<19; i++) {
            for(int j=0; j<19; j++) {
                if(array[i][j]>0) {        
                    for(int k=0; k<4; k++) {
                        int cnt=1;
                        int nx=i+dxs[k]; int ny=j+dys[k];
                        while(nx>=0 && nx<19 && ny>=0 && ny<19 && array[nx][ny]==array[i][j]) {
                            cnt++;
                            if(cnt==5) {
                                if((i-dxs[k]>=0 && i-dxs[k]<19 && j-dys[k]>=0 && j-dys[k]<19 && array[i-dxs[k]][j-dys[k]]==array[i][j])) {
                                    cnt++;
                                    break;
                                }
                                if((nx+dxs[k]>=0 && nx+dxs[k]<19 && ny+dys[k]>=0 && ny+dys[k]<19 && array[nx+dxs[k]][ny+dys[k]]==array[i][j])) {
                                    cnt++;
                                    break;
                                }
                            }
                            if(cnt==5) {                                
                                button=true;
                                answer=array[i][j];
                                x=i; y=j;
                                break;
                            }
                            nx+=dxs[k];
                            ny+=dys[k];
                        }
                    }
                }
                if (button) break;
            }
            if (button) break;
        }
        if(!button) {
            System.out.println(0);
        }else {
            System.out.println(answer);
            System.out.println((x+1+ " " + (y+1));
        }
    }
}
/*
1: 검은 바둑알, 2: 흰 바둑알, 0 : 빈자리
출력
검은색 win: 1 , 흰색 win : 2, 승부 x : 0
    승부가 났을 때, 가장 왼쪽에 있는 바둑알의 row, col 출력
*/
cs