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 15:03:13

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

int main() {

    vector<string> mp(8);

    // รับกระดาน 8x8
    for (int i = 0; i < 8; i++) {
        cin >> mp[i];
    }

    // 8 ทิศ
    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;

        // กันกรณี input หมด
        if (!(cin >> x >> y)) {
            break;
        }

        // เจอ -1 -1 -> เกมจบ
        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++;
            }
            else if (mp[i][j] == 'W') {
                white++;
            }
        }
    }


    // =================================
    // แสดงกระดาน
    // =================================

    for (int i = 0; i < 8; i++) {
        cout << mp[i] << '\n';
    }


    // =================================
    // แสดงผู้ชนะ
    // =================================

    if (black > white) {
        cout << "black wins\n";
    }
    else if (white > black) {
        cout << "white wins\n";
    }
    else {
        cout << "draw\n";
    }

    return 0;
}