Submission

Status:

PPPPP-P-----P-P-----

Subtask/Task Score:

40/100

Score: 40

User: CiCiiAlWh

Problemset: Othello

Language: cpp

Time: 0.006 second

Submitted On: 2025-09-28 14:48:25

#include <bits/stdc++.h>
using namespace std;

int dr[8] = {-1,-1,-1,0,0,1,1,1};
int dc[8] = {-1,0,1,-1,1,-1,0,1};

bool inside(int r,int c){
    return r>=0 && r<8 && c>=0 && c<8;
}

bool validMove(char board[8][8], int r, int c, char cur, char other) {
    if(board[r][c] != '_') return false;
    for(int d=0; d<8; d++) {
        int nr = r + dr[d], nc = c + dc[d];
        bool foundOther = false;
        while(inside(nr,nc) && board[nr][nc] == other) {
            foundOther = true;
            nr += dr[d];
            nc += dc[d];
        }
        if(foundOther && inside(nr,nc) && board[nr][nc] == cur) return true;
    }
    return false;
}

void placePiece(char board[8][8], int r, int c, char cur, char other) {
    board[r][c] = cur;
    for(int d=0; d<8; d++) {
        int nr = r + dr[d], nc = c + dc[d];
        vector<pair<int,int>> path;
        while(inside(nr,nc) && board[nr][nc] == other) {
            path.push_back({nr,nc});
            nr += dr[d];
            nc += dc[d];
        }
        if(inside(nr,nc) && board[nr][nc] == cur && !path.empty()) {
            for(auto &p : path) board[p.first][p.second] = cur;
        }
    }
}

bool boardFull(char board[8][8]) {
    for(int i=0; i<8; i++)
        for(int j=0; j<8; j++)
            if(board[i][j] == '_') return false;
    return true;
}

int main() {
    char board[8][8];
    string s;
    for(int i=0; i<8; i++) {
        cin >> s;
        for(int j=0; j<8; j++) {
            board[i][j] = s[j];
        }
    }

    int r, c;
    int turn = 0;
    auto opp = [&](char ch){ return ch=='B' ? 'W' : 'B'; };

    while(cin >> r >> c) {
        if(r == -1 && c == -1) break;
        char cur = (turn % 2 == 0) ? 'B' : 'W';
        char other = opp(cur);
        if(!validMove(board, r, c, cur, other)) continue;
        placePiece(board, r, c, cur, other);
        if(boardFull(board)) break;
        turn++;
    }

    for(int i=0; i<8; i++) {
        for(int j=0; j<8; j++) cout << board[i][j];
        cout << "\n";
    }

    int b = 0, w = 0;
    for(int i=0; i<8; i++)
        for(int j=0; j<8; j++) {
            if(board[i][j] == 'B') b++;
            else if(board[i][j] == 'W') w++;
        }

    if(b > w) cout << "black wins\n";
    else if(w > b) cout << "white wins\n";
    else cout << "draw\n";

    return 0;
}