1일1알

백준 15671번 오델로 C++ 본문

알고리즘

백준 15671번 오델로 C++

영춘권의달인 2023. 5. 11. 11:27

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

 

15671번: 오델로

오델로(Othello)는 검은색, 또는 하얀색 작은 원판을 6x6의 판 위에 늘어놓는 보드 게임이다. 보통 일본에서는 オセロ(오세로), 국내에서는 오델로라 부르고 있다. 어원은 오셀로 희곡으로 오셀로의

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[8] = { -1,-1, 0, 1, 1, 1, 0,-1 };
int dCol[8] = {  0, 1, 1, 1, 0,-1,-1,-1 };

enum class Turn {
    BLACK,
    WHITE
};

int n;

vector<vector<char>> board;

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

    board = vector<vector<char>>(7, vector<char>(7, '.'));
    board[3][3] = 'W';
    board[4][4] = 'W';
    board[3][4] = 'B';
    board[4][3] = 'B';
    cin >> n;

    Turn currTurn = Turn::BLACK;
    while (n--) {
        int r, c;
        cin >> r >> c;

        char myStone;
        if (currTurn == Turn::BLACK) myStone = 'B';
        else myStone = 'W';

        board[r][c] = myStone;
        for (int i = 0; i < 8; i++) {
            int nextRow = r;
            int nextCol = c;
            vector<pair<int, int>> tmp;
            while (true) {
                nextRow += dRow[i];
                nextCol += dCol[i];
                if (nextRow < 1 || nextRow > 6 || nextCol < 1 || nextCol > 6 || board[nextRow][nextCol] == '.') {
                    tmp.clear();
                    break;
                }
                if (board[nextRow][nextCol] == myStone) {
                    break;
                }
                tmp.push_back({ nextRow,nextCol });
            }
            for (auto a : tmp) {
                board[a.first][a.second] = myStone;
            }
        }

        if (currTurn == Turn::BLACK) currTurn = Turn::WHITE;
        else currTurn = Turn::BLACK;
    }
    int blackCnt = 0;
    int whiteCnt = 0;
    for (int i = 1; i <= 6; i++) {
        for (int j = 1; j <= 6; j++) {
            if (board[i][j] == 'B') blackCnt++;
            if (board[i][j] == 'W') whiteCnt++;
            cout << board[i][j];
        }
        cout << "\n";
    }
    if (blackCnt > whiteCnt) cout << "Black";
    else cout << "White";
}

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

백준 11536번 줄 세우기 C++  (0) 2023.05.13
백준 12933번 오리 C++  (0) 2023.05.12
백준 25206번 너의 평점은 C++  (0) 2023.05.09
백준 2331번 반복수열 C++  (0) 2023.05.08
백준 17828번 문자열 화폐 C++  (0) 2023.05.07