Submission

Status:

PPPPPP-PPPPP-P----PP

Subtask/Task Score:

70/100

Score: 70

User: CiCiiAlWh

Problemset: Othello

Language: cpp

Time: 0.003 second

Submitted On: 2025-09-28 15:53:21

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

string board[8];
bool black_play = true;
int r, c;

// Show board
void show_board() {
	for(int i = 0; i < 8; ++i)
		cout << board[i] << '\n';
}

// Count and print result
void result() {
	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";
	else if (w > b) cout << "white wins";
	else cout << "draw";
}

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

void playing(char color) {
	char opponent = (color == 'B' ? 'W' : 'B');
	for (int d = 0; d < 8; ++d) {
		int nr = r + dr[d];
		int nc = c + dc[d];
		vector<pair<int, int>> to_flip;

		// Traverse in the direction
		while (nr >= 0 && nr < 8 && nc >= 0 && nc < 8) {
			if (board[nr][nc] == opponent) {
				to_flip.push_back({nr, nc});
				nr += dr[d];
				nc += dc[d];
			}
			else if (board[nr][nc] == color) {
				for (auto [fr, fc] : to_flip) {
					board[fr][fc] = color;
				}
				break;
			}
			else break; // empty cell or end of board
		}
	}
}

int main() {
	// Input board
	for (int i = 0; i < 8; ++i) {
		cin >> board[i];
	}

	// Count available moves
	int ct = 0;
	for (int i = 0; i < 8; ++i)
		for (int j = 0; j < 8; ++j)
			if (board[i][j] == '_') ++ct;

	// Play moves
	while (ct > 0) {
		--ct;
		cin >> r >> c;
		if (r == -1 && c == -1) break;

		char color = black_play ? 'B' : 'W';
		board[r][c] = color;
		playing(color);
		black_play = !black_play;
	}

	// Output final board and result
	show_board();
	result();
}