Submission

Status:

PPPPPP-PPPPP-P----PP

Subtask/Task Score:

70/100

Score: 70

User: pxsit

Problemset: Othello

Language: cpp

Time: 0.004 second

Submitted On: 2026-10-06 18:16:31

#include <iostream>
using namespace std;

int main() {
    // Initialize 8x8 board
    char board[8][8];

    // Read initial board state
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            cin >> board[i][j];
        }
    }

    char currentColor = 'B'; // Black always starts first

    // Game loop
    while (true) {
        int r, c;
        cin >> r >> c;

        // Check if game should end
        if (r == -1 && c == -1) {
            break;
        }

        // Determine opponent color
        char opponent;
        if (currentColor == 'B') {
            opponent = 'W';
        } else {
            opponent = 'B';
        }

        // Place the piece
        board[r][c] = currentColor;

        // Check all 8 directions and flip pieces
        // Directions: up, down, left, right, up-left, up-right, down-left, down-right
        int dr[] = {-1, 1, 0, 0, -1, -1, 1, 1};
        int dc[] = {0, 0, -1, 1, -1, 1, -1, 1};

        for (int dir = 0; dir < 8; dir++) {
            // Store positions that might be flipped
            int flipR[64], flipC[64];
            int flipCount = 0;

            int newR = r + dr[dir];
            int newC = c + dc[dir];

            // Walk in this direction and collect opponent pieces
            while (newR >= 0 && newR < 8 && newC >= 0 && newC < 8) {
                if (board[newR][newC] == opponent) {
                    // Found an opponent piece
                    flipR[flipCount] = newR;
                    flipC[flipCount] = newC;
                    flipCount++;
                    newR += dr[dir];
                    newC += dc[dir];
                } else if (board[newR][newC] == currentColor) {
                    // Found our color - flip all pieces in between
                    if (flipCount > 0) {
                        for (int f = 0; f < flipCount; f++) {
                            board[flipR[f]][flipC[f]] = currentColor;
                        }
                    }
                    break;
                } else {
                    // Empty space - stop
                    break;
                }
            }
        }

        // Check if board is full
        bool isFull = true;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board[i][j] == '_') {
                    isFull = false;
                    break;
                }
            }
            if (!isFull) {
                break;
            }
        }

        if (isFull) {
            break;
        }

        // Switch turns
        if (currentColor == 'B') {
            currentColor = 'W';
        } else {
            currentColor = 'B';
        }
    }

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

    // Count pieces
    int blackCount = 0;
    int whiteCount = 0;
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (board[i][j] == 'B') {
                blackCount++;
            } else if (board[i][j] == 'W') {
                whiteCount++;
            }
        }
    }

    // Determine winner
    if (blackCount > whiteCount) {
        cout << "black wins\n";
    } else if (whiteCount > blackCount) {
        cout << "white wins\n";
    } else {
        cout << "draw\n";
    }

    return 0;
}