1일1알

백준 2615번 오목 C++ 본문

알고리즘

백준 2615번 오목 C++

영춘권의달인 2023. 5. 6. 13:49

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

 

2615번: 오목

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

www.acmicpc.net

 

육목 이상이 되는 부분을 조심해야한다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int dRow[4] = { -1,0,1,1 };
int dCol[4] = { 1,1,1,0 };

vector<vector<int>> board;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    board = vector<vector<int>>(19, vector<int>(19));
    for (int i = 0; i < 19; i++)
        for (int j = 0; j < 19; j++)
            cin >> board[i][j];

    bool finish = false;
    for (int i = 0; i < 19; i++) {
        for (int j = 0; j < 19; j++) {
            if (board[i][j] == 0) continue;
            for (int k = 0; k < 4; k++) {
                int nextRow = i;
                int nextCol = j;
                int prevCnt = 0;
                int nextCnt = 1;
                while (true) {
                    nextRow -= dRow[k];
                    nextCol -= dCol[k];
                    if (nextRow < 0 || nextRow >= 19) break;
                    if (nextCol < 0 || nextCol >= 19) break;
                    if (board[nextRow][nextCol] != board[i][j]) break;
                    prevCnt++;
                }
                nextRow = i;
                nextCol = j;
                while (true) {
                    nextRow += dRow[k];
                    nextCol += dCol[k];
                    if (nextRow < 0 || nextRow >= 19) break;
                    if (nextCol < 0 || nextCol >= 19) break;
                    if (board[nextRow][nextCol] != board[i][j]) break;
                    nextCnt++;
                }
                int cnt = prevCnt + nextCnt;
                if (cnt == 5 && prevCnt == 0) {
                    finish = true;
                    cout << board[i][j] << "\n";
                    cout << i + 1 << " " << j + 1;
                }
            }
        }
    }
    if (finish == false) cout << 0;
}

'알고리즘' 카테고리의 다른 글

백준 2331번 반복수열 C++  (0) 2023.05.08
백준 17828번 문자열 화폐 C++  (0) 2023.05.07
백준 17829번 222-풀링 C++  (0) 2023.05.05
백준 2508번 사탕 박사 고창영 C++  (0) 2023.05.04
백준 3060번 욕심쟁이 돼지 C++  (0) 2023.05.03