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

int a[39][39]; 
int bo[39][39];
int rel[2000][39][39];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int dix=10,diy=10;
int rx=30,ry=30,nnn=19,cnt=0;
string ins;
bool find(int x,int y,int mor){
	if(a[x][y]==10+mor) return false;
	else if(a[x][y]==mor) a[x][y]+=10;
	if(a[x][y]==0 || (x==rx && y==ry && mor+10==a[x][y])) return true;
	else if(a[x][y]!=mor+10) return false;
	return(find(x+1,y,mor) || find(x-1,y,mor) || find(x,y+1,mor) || find(x,y-1,mor)); 
}
bool rfind(int x,int y,int mor){
	if(a[x][y]==10+mor) return false;
	else if(a[x][y]==mor) a[x][y]+=10;
	if(a[x][y]==0) return true;
	else if(a[x][y]!=mor+10) return false;
	return(find(x+1,y,mor) || find(x-1,y,mor) || find(x,y+1,mor) || find(x,y-1,mor)); 
}
void kill(int x,int y,int mor){
	if(a[x][y]==mor){
		a[x][y]=0;
		bo[x][y]=1;
		for(int i=0;i<4;i++){
			kill(x+dx[i],y+dy[i],mor);
		}
	}
	return;
}
void check(){	
	for(int i=1;i<=nnn;i++){
		for(int j=1;j<=nnn;j++){
			bo[i][j]=0;
		}
	}
	for(int i=1;i<=nnn;i++){
		for(int j=1;j<=nnn;j++){
			if((a[i][j]==1 || a[i][j]==2) && bo[i][j]==0){
				if(!find(i,j,a[i][j])){
					for(int ii=1;ii<=nnn;ii++){
						for(int jj=1;jj<=nnn;jj++){
							if(a[ii][jj]>10) a[ii][jj]-=10;
						}
					}
					kill(i,j,a[i][j]);
				}
				
				for(int ii=1;ii<=nnn;ii++){
					for(int jj=1;jj<=nnn;jj++){
						if(a[ii][jj]>10) a[ii][jj]-=10;
					}
				}
				
			}
		}
	}
	if(!rfind(rx,ry,a[rx][ry])){
		for(int ii=1;ii<=nnn;ii++){
			for(int jj=1;jj<=nnn;jj++){
				if(a[ii][jj]>10) a[ii][jj]-=10;
			}
		}
		kill(rx,ry,a[rx][ry]);
	}
	for(int ii=1;ii<=nnn;ii++){
		for(int jj=1;jj<=nnn;jj++){
			if(a[ii][jj]>10) a[ii][jj]-=10;
		}
	}
}
int main(){
	for(int i=0;i<=33;i++){
		for(int j=0;j<=33;j++){
			a[i][j]=3;
		}
	}
	for(int i=1;i<=nnn;i++){
		for(int j=1;j<=nnn;j++){
			a[i][j]=0;
		}
	}
	a[30][30]=0;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
	while(true){
		check();
		cnt++;
		for(int i=1;i<=nnn;i++){
			for(int j=1;j<=nnn;j++){
				if(a[i][j]==1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
				if(a[i][j]==2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
				if(i%6==4 && j%6==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
				if(i==rx && j==ry) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);//设置黄色 
				if(i==dix && j==diy) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);//设置紫色 
				if(a[i][j]==0)printf("+");
				if(a[i][j]==1)printf("A");
				if(a[i][j]==2)printf("B");
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
				rel[cnt][i][j]=a[i][j];
			}
			printf("\n");
		}
		//scanf("%d%d",&rx,&ry);
		
		cin >> ins;
		system("cls");
		if(ins[0]=='a') diy--;
		if(ins[0]=='s') dix++;
		if(ins[0]=='d') diy++;
		if(ins[0]=='w') dix--;
		while(ins[0]!='e'){
			for(int i=1;i<=nnn;i++){
				for(int j=1;j<=nnn;j++){
					if(a[i][j]==1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
					if(a[i][j]==2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
					if(i%6==4 && j%6==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
					if(i==rx && j==ry) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);//设置黄色 
					if(i==dix && j==diy) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);//设置紫色 
					if(a[i][j]==0)printf("+");
					if(a[i][j]==1)printf("A");
					if(a[i][j]==2)printf("B");
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
					rel[cnt][i][j]=a[i][j];
				}
				printf("\n");
			}
			cin >> ins;
			system("cls");
			if(ins[0]=='a' && diy!=1) diy--;
			if(ins[0]=='s' && dix!=19) dix++;     
			if(ins[0]=='d' && diy!=19) diy++;
			if(ins[0]=='w' && dix!=1) dix--;
		}
		if(rx==dix && ry==diy) a[dix][diy]=-1;
		rx=dix;
		ry=diy;
		if(rx==100) {
			for(int i=1;i<=nnn;i++){
				for(int j=1;j<=nnn;j++){
					a[i][j]=rel[cnt-1][i][j];
				}
			}
		}
		else if(a[rx][ry]==0) a[rx][ry]=1;
		else if(a[rx][ry]==-1) {
			a[rx][ry]=0;
			rx=0,ry=0;
		}
		check();
		cnt++;
		for(int i=1;i<=nnn;i++){
			for(int j=1;j<=nnn;j++){
				if(a[i][j]==1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
				if(a[i][j]==2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
				if(i%6==4 && j%6==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
				if(i==rx && j==ry) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);//设置黄色
				if(i==dix && j==diy) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);//设置紫色  
				if(a[i][j]==0)printf("+");
				if(a[i][j]==1)printf("A");
				if(a[i][j]==2)printf("B");
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
				rel[cnt][i][j]=a[i][j];
			}
			printf("\n");
		}
		cnt++;
		//scanf("%d%d",&rx,&ry);
		cin >> ins;
		system("cls");
		if(ins[0]=='a') diy--;
		if(ins[0]=='s') dix++;
		if(ins[0]=='d') diy++;
		if(ins[0]=='w') dix--;
		while(ins[0]!='e'){
			for(int i=1;i<=nnn;i++){
				for(int j=1;j<=nnn;j++){
					if(a[i][j]==1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
					if(a[i][j]==2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
					if(i%6==4 && j%6==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
					if(i==rx && j==ry) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);//设置黄色 
					if(i==dix && j==diy) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);//设置紫色 
					if(a[i][j]==0)printf("+");
					if(a[i][j]==1)printf("A");
					if(a[i][j]==2)printf("B");
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
					rel[cnt][i][j]=a[i][j];
				}
				printf("\n");
			}
			cin >> ins;
			system("cls");
			if(ins[0]=='a' && diy!=1) diy--;
			if(ins[0]=='s' && dix!=19) dix++;     
			if(ins[0]=='d' && diy!=19) diy++;
			if(ins[0]=='w' && dix!=1) dix--;
		}
		if(rx==dix && ry==diy) a[dix][diy]=-1;
		rx=dix;
		ry=diy;
		if(rx==100) {
			for(int i=1;i<=nnn;i++){
				for(int j=1;j<=nnn;j++){
					a[i][j]=rel[cnt-1][i][j];
				}
			}
		}
		else if(a[rx][ry]==0) a[rx][ry]=2;
		else if(a[rx][ry]==-1){
			a[rx][ry]=0;
			rx=0,ry=0;
		} 
	}
	return 0;
}