Submission

Status:

PPPPPP-PPPPP-P----PP

Subtask/Task Score:

70/100

Score: 70

User: wleo.lwo

Problemset: Othello

Language: cpp

Time: 0.005 second

Submitted On: 2026-10-07 14:47:04

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {

    vector<string> mp(8);

    for (int i = 0; i < 8; i++) {
        cin >> mp[i];
    }

    // ซ้าย, ซ้ายบน, บน, ขวาบน,
    // ขวา, ขวาล่าง, ล่าง, ซ้ายล่าง
    int dx[8] = {0, -1, -1, -1, 0, 1, 1, 1};
    int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

    char color = 'B';     // ดำเริ่มก่อนเสมอ


    while (true) {

        // -------------------------
        // เช็กว่าบอร์ดเต็มก่อน
        // -------------------------
        bool full = true;

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (mp[i][j] == '_') {
                    full = false;
                }
            }
        }

        if (full == true) {
            break;
        }


        int x, y;
        cin >> x >> y;

        if (x == -1 && y == -1) {
            break;
        }


        // วางหมาก
        mp[x][y] = color;

        char other;

        if (color == 'B') {
            other = 'W';
        }
        else {
            other = 'B';
        }


        // -------------------------
        // เช็ก 8 ทิศ
        // -------------------------
        for (int d = 0; d < 8; d++) {

            int nx = x + dx[d];
            int ny = y + dy[d];


            // ช่องแรกต้องเป็นสีตรงข้ามก่อน
            if (nx < 0 || nx >= 8 ||
                ny < 0 || ny >= 8) {
                continue;
            }

            if (mp[nx][ny] != other) {
                continue;
            }


            // เดินผ่านหมากสีตรงข้าม
            while (nx >= 0 && nx < 8 &&
                   ny >= 0 && ny < 8 &&
                   mp[nx][ny] == other) {

                nx += dx[d];
                ny += dy[d];
            }


            // เดินจนหลุดกระดาน
            if (nx < 0 || nx >= 8 ||
                ny < 0 || ny >= 8) {
                continue;
            }


            // ถ้าสุดท้ายไม่ใช่สีตัวเอง
            // แปลว่าล้อมไม่ได้
            if (mp[nx][ny] != color) {
                continue;
            }


            // -------------------------
            // ล้อมสำเร็จ -> พลิก
            // -------------------------
            int fx = x + dx[d];
            int fy = y + dy[d];

            while (fx != nx || fy != ny) {

                mp[fx][fy] = color;

                fx += dx[d];
                fy += dy[d];
            }
        }


        // -------------------------
        // สลับคนเล่น
        // -------------------------
        if (color == 'B') {
            color = 'W';
        }
        else {
            color = 'B';
        }
    }



    // -------------------------
    // นับคะแนน
    // -------------------------
    int black = 0;
    int white = 0;

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {

            if (mp[i][j] == 'B') {
                black++;
            }

            if (mp[i][j] == 'W') {
                white++;
            }
        }
    }


    // -------------------------
    // แสดงบอร์ด
    // -------------------------
    for (int i = 0; i < 8; i++) {
        cout << mp[i] << '\n';
    }


    // -------------------------
    // แสดงผู้ชนะ
    // -------------------------
    if (black > white) {
        cout << "black wins";
    }
    else if (white > black) {
        cout << "white wins";
    }
    else {
        cout << "draw";
    }

    return 0;
}