Submission

Status:

PPPPPP-PPPPP-P----PP

Subtask/Task Score:

70/100

Score: 70

User: nemuchannnUwU

Problemset: Othello

Language: cpp

Time: 0.004 second

Submitted On: 2026-10-05 22:43:55

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

int dx[8]={1,-1,0,0,1,-1,1,-1}, dy[8]={0,0,1,-1,1,-1,-1,1};
int d_chiang_right[2][2]={{1,1},{-1,-1}};
int d_chiang_left[2][2]={{1,-1},{-1,1}};

void show_board(vector<vector<char>> &board){
	for (int i=0;i<8;i++){
		for (int j=0;j<8;j++){
			cout << board[i][j];
		}	
		cout << "\n";
	}
}

void chk_left_right(vector<vector<char>> &board,int x,int y,int body_cnt,int &cnt_BLACK,int &cnt_WHITE){
	vector<vector<bool>> visited(8,vector<bool> (8,false));
	queue<pair<int,int>> q;
	q.push({x,y});
	visited[x][y]=true;
	int l=y,r=y;
	while(!q.empty()){
		auto [xx,yy] = q.front();
		q.pop();
		
		for (int i=2;i<=3;i++){
			int nx=xx;
			int ny=yy+dy[i];
			if (nx<0||nx>=8||ny<0||ny>=8) continue;
			if (visited[nx][ny]||board[nx][ny]=='_') continue;
			if (body_cnt==0){
				visited[nx][ny]=true;
				if (board[nx][ny]=='W') q.push({nx,ny});
				if (board[nx][ny]=='B'){
					if (ny<y) l=ny;
					if (ny>y) r=ny;
				}
			}else if(body_cnt==1){
				visited[nx][ny]=true;
				if (board[nx][ny]=='B') q.push({nx,ny});
				if (board[nx][ny]=='W'){
					if (ny<y) l=ny;
					if (ny>y) r=ny;
				}
			}
		}
	}
	for (int i=l;i<=r;i++){
		if (body_cnt==0){
			if (board[x][i]=='B') continue;
			board[x][i]='B';
			cnt_BLACK++;
			cnt_WHITE--;
		}else if(body_cnt==1){
			if (board[x][i]=='W') continue;
			board[x][i]='W';
			cnt_BLACK--;
			cnt_WHITE++;
		}
	}
}

void chk_up_down(vector<vector<char>> &board,int x,int y,int body_cnt,int &cnt_BLACK,int &cnt_WHITE){
	vector<vector<bool>> visited(8,vector<bool> (8,false));
	queue<pair<int,int>> q;
	q.push({x,y});
	visited[x][y]=true;
	int u=x,d=x;
	while(!q.empty()){
		auto [xx,yy] = q.front();
		q.pop();

		for (int i=0;i<=1;i++){
			int nx=xx+dx[i];
			int ny=yy;
			if (nx<0||nx>=8||ny<0||ny>=8) continue;
			if (visited[nx][ny]||board[nx][ny]=='_') continue;
			if (body_cnt==0){
				visited[nx][ny]=true;
				if (board[nx][ny]=='W') q.push({nx,ny});
				if (board[nx][ny]=='B'){
					if (nx<x) u=nx;
					if (nx>x) d=nx;
				}
			}else if(body_cnt==1){
				visited[nx][ny]=true;
				if (board[nx][ny]=='B') q.push({nx,ny});
				if (board[nx][ny]=='W'){
					if (nx<x) u=nx;
					if (nx>x) d=nx;
				}
			}
		}
	}
	for (int i=u;i<=d;i++){
		if (body_cnt==0){
			if (board[i][y]=='B') continue;
			board[i][y]='B';
			cnt_BLACK++;
			cnt_WHITE--;
		}else if(body_cnt==1){
			if (board[i][y]=='W') continue;
			board[i][y]='W';
			cnt_BLACK--;
			cnt_WHITE++;
		}
	}
}

void chk_cross_right(vector<vector<char>> &board,int x,int y,int body_cnt,int &cnt_BLACK,int &cnt_WHITE){
	vector<vector<bool>> visited(8,vector<bool> (8,false));
	queue<pair<int,int>> q;
	q.push({x,y});
	visited[x][y]=true;
	pair<int,int> top_left={x,y},bottom_right={x,y};
	while(!q.empty()){
		auto [xx,yy] = q.front();
		q.pop();

		for (int i=0;i<=1;i++){
			int nx=xx+d_chiang_right[i][0];
			int ny=yy+d_chiang_right[i][1];
			if (nx<0||nx>=8||ny<0||ny>=8) continue;
			if (visited[nx][ny]||board[nx][ny]=='_') continue;
			if (body_cnt==0){
				visited[nx][ny]=true;
				if (board[nx][ny]=='W') q.push({nx,ny});
				if (board[nx][ny]=='B'){
					top_left = {min(top_left.first, nx), min(top_left.second, ny)};
					bottom_right = {max(bottom_right.first, nx), max(bottom_right.second, ny)};
				}
			}else if(body_cnt==1){
				visited[nx][ny]=true;
				if (board[nx][ny]=='B') q.push({nx,ny});
				if (board[nx][ny]=='W'){
					top_left = {min(top_left.first, nx), min(top_left.second, ny)};
					bottom_right = {max(bottom_right.first, nx), max(bottom_right.second, ny)};
				}
			}
		}
	}
	if (top_left.first>bottom_right.first || top_left.second>bottom_right.second) return;
	int i=top_left.first, j=top_left.second;
	while (i>=0 && j>=0 && i<8 && j<8 && i<=bottom_right.first && j<=bottom_right.second) {
		if (body_cnt == 0) {
			if (board[i][j] != 'B') {
				board[i][j] = 'B';
				cnt_BLACK++;
				cnt_WHITE--;
			}
		} else if (body_cnt == 1) {
			if (board[i][j] != 'W') {
				board[i][j] = 'W';
				cnt_BLACK--;
				cnt_WHITE++;
			}
		}
		i++;
		j++;
	}
}

void chk_cross_left(vector<vector<char>> &board,int x,int y,int body_cnt,int &cnt_BLACK,int &cnt_WHITE){
	vector<vector<bool>> visited(8,vector<bool> (8,false));
	queue<pair<int,int>> q;
	q.push({x,y});
	visited[x][y]=true;
	pair<int,int> top_right={x,y},bottom_left{x,y};
	while(!q.empty()){
		auto [xx,yy] = q.front();
		q.pop();

		for (int i=0;i<=1;i++){
			int nx=xx+d_chiang_left[i][0];
			int ny=yy+d_chiang_left[i][1];
			if (nx<0||nx>=8||ny<0||ny>=8) continue;
			if (visited[nx][ny]||board[nx][ny]=='_') continue;
			if (body_cnt==0){
				visited[nx][ny]=true;
				if (board[nx][ny]=='W') q.push({nx,ny});
				if (board[nx][ny]=='B'){
					top_right={min(top_right.first,nx),max(top_right.second,ny)};
					bottom_left={max(bottom_left.first, nx), min(bottom_left.second,ny)};
				}
			}else if(body_cnt==1){
				visited[nx][ny]=true;
				if (board[nx][ny]=='B') q.push({nx,ny});
				if (board[nx][ny]=='W'){
					top_right = {min(top_right.first,nx),max(top_right.second,ny)};
					bottom_left = {max(bottom_left.first,nx),min(bottom_left.second,ny)};
				}
			}
		}
	}
	if (bottom_left.first<top_right.first || bottom_left.second>top_right.second) return;
	int i=bottom_left.first, j=bottom_left.second;
	while (i>=0 && j>=0 && i<8 && j<8 && i>=top_right.first && j<=top_right.second) {
		if (body_cnt == 0) {
			if (board[i][j] != 'B') {
				board[i][j] = 'B';
				cnt_BLACK++;
				cnt_WHITE--;
			}
		} else if (body_cnt == 1) {
			if (board[i][j] != 'W') {
				board[i][j] = 'W';
				cnt_BLACK--;
				cnt_WHITE++;
			}
		}
		i--;
		j++;
	}
}

int main(){
	cin.tie(nullptr)->sync_with_stdio(0);
	vector<vector<char>> board(8,vector<char> (8));
	int cnt_BLACK=0,cnt_WHITE=0;
	for (int i=0;i<8;i++){
		for (int j=0;j<8;j++){
			cin >> board[i][j];
			if (board[i][j]=='W') cnt_WHITE++;
			else if(board[i][j]=='B') cnt_BLACK++;
		}
	}
	int body_cnt=0;
	while(true){
		int x,y; cin >> x >> y;
		body_cnt%=2;
		if (x==-1&&y==-1){
			show_board(board);
			if (cnt_BLACK>cnt_WHITE) cout << "black wins";
			else if(cnt_WHITE>cnt_BLACK) cout << "white wins";
			else cout << "draw";
			return 0;
		}
		if (body_cnt==0){
			cnt_BLACK++;
			board[x][y]='B';
		}
		else if (body_cnt==1){
			cnt_WHITE++;
			board[x][y]='W';
		}
		chk_left_right(board,x,y,body_cnt,cnt_BLACK,cnt_WHITE);
		chk_up_down(board,x,y,body_cnt,cnt_BLACK,cnt_WHITE);
		chk_cross_right(board,x,y,body_cnt,cnt_BLACK,cnt_WHITE);
		chk_cross_left(board,x,y,body_cnt,cnt_BLACK,cnt_WHITE);
		body_cnt++;
		if (cnt_WHITE+cnt_BLACK==64){
			show_board(board);
			if (cnt_BLACK>cnt_WHITE) cout << "black wins";
			else if(cnt_WHITE>cnt_BLACK) cout << "white wins";
			else cout << "draw";
			return 0;
		}
	}
}