백준 문제풀이/Bruteforcing

[C++] 백준 문제풀이 (Bruteforcing) 2615번 오목

코딩준우 2023. 6. 27. 19:27

 

 

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

 

2615번: 오목

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

www.acmicpc.net

 

 

 

//[C++] 백준 문제풀이 (Bruteforcing)

#include <bits/stdc++.h>

const int MAX = 19;
int map[MAX][MAX];


bool check (int y, int x){

    // 탐색 순서
    // 좌 --> 우
    // 상 --> 하
    // 좌측상단 --> 우측하단
    // 좌측하단 --> 우측상단
    const int dy[] = {0, 1, 1, -1};
    const int dx[] = {1, 0, 1, 1};
    int value = map[y][x];


    for (int dir = 0; dir < 4; ++dir){
        int currY = y - dy[dir];
        int currX = x - dx[dir];
        
        // 탐색할 방향의 반대방향에 현재 위치와 같은 색상이 존재하면 정답 후보군이 될 수 없다.
        // 왜냐하면 현재 위치보다 왼쪽 또는 위쪽에 같은 색상이 존재하기 때문이다.
        if (currY >= 0 && currY < 19 && currX >= 0 && currX < 19 && map[currY][currX] == value) continue;

        int cnt = 0;
        currY = y;
        currX = x;

        while(currY >= 0 && currY < 19 && currX >= 0 && currX < 19){
            if (map[currY][currX] != value) break;
            ++cnt;
            currY += dy[dir];
            currX += dx[dir];
        }
        if (cnt == 5) return true;
    }
    return false;
}


int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    for (int y = 0; y < MAX; ++y){
        for (int x = 0; x < MAX; ++ x){
            std::cin >> map[y][x];
        }
    }

    for (int y = 0; y < MAX; ++y){
        for (int x = 0; x < MAX; ++x){
            if (map[y][x] == 0) continue;
            if (check(y, x)){
                std::cout << map[y][x] << "\n" << y + 1 << " " << x + 1 << "\n"; 
                return 0;
            }
        }
    }

    std::cout << 0 <<  "\n";
    return 0;
}