WA第二点,求调。
查看原帖
WA第二点,求调。
773805
wsm52楼主2024/8/7 21:24
#include<bits/stdc++.h>
using namespace std;
int q;
int chess[10][9]={
1,2,3,4,5,4,3,2,1,
0,0,0,0,0,0,0,0,0,
6,0,0,0,0,0,0,0,6,
7,0,7,0,7,0,7,0,7,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
14,0,14,0,14,0,14,0,14,
13,0,0,0,0,0,0,0,13,
0,0,0,0,0,0,0,0,0,
8,9,10,11,12,11,10,9,8
};
//红车1 红马2 红象3 红士4 红王5 红印度刺客6 红兵7
//蓝车8 蓝马9 蓝象10 蓝士11 蓝王12 蓝印度刺客13 蓝兵14
//马
int move_horse[2][8]={{-2,-2,-1,1,2,2,1,-1},{-1,1,2,2,1,-1,-2,-2}};
int horse_not_move[2][4]={{-1,0,1,0},{0,1,0,-1}};
//象
int move_elephant[2][4]={{-2,-2,2,2},{-2,2,-2,2}};
int elephant_not_move[2][4]={{-1,-1,1,1},{-1,1,-1,1}};
//士
int move_guard[2][4]={{-1,-1,1,1},{-1,1,-1,1}};
//王
int move_captain[2][4]={{-1,1,0,0},{0,0,-1,1}};
//印度刺客
int move_duck[2][8]={{-3,-3,-2,2,3,3,2,-2},{-2,2,3,3,2,-2,-3,-3}};
int duck_not_move1[2][4]={{-1,0,1,0},{0,1,0,-1}};
int duck_not_move2[2][8]={{-2,-2,-1,1,2,2,1,-1},{-1,1,2,2,1,-1,-2,-2}};
//兵
int move_soldier[2][8]={{0,-1,-1,-1,0,1,1,1},{-1,-1,0,1,1,1,0,-1}};

bool check_move(int sx,int sy,int ex,int ey){
	if(chess[sx][sy]%7==0){ //兵 
		for(int i=0;i<8;i++){
			int dx=sx+move_soldier[0][i],dy=sy+move_soldier[1][i];
			if(dx==ex&&dy==ey) return true;
		}
	}else if(chess[sx][sy]%7==1){ //车 
		int i;
		for(i=sx+1;i<10&&!chess[i][sy];i++);
		if(i==ex&&sy==ey) return true;
		for(i=sx-1;i>=0&&!chess[i][sy];i--);
		if(i==ex&&sy==ey) return true;
		for(i=sy+1;i<9&&!chess[sx][i];i++);
		if(sx==ex&&i==ey) return true;
		for(i=sy-1;i>=0&&!chess[sx][i];i--);
		if(sx==ex&&i==ey) return true;
	}else if(chess[sx][sy]%7==2){ //马 
		for(int i=0;i<8;i++){
			if(chess[sx+horse_not_move[0][i/2]][sy+horse_not_move[1][i/2]]) continue;
			int dx=sx+move_horse[0][i],dy=sy+move_horse[1][i];
			if(dx==ex&&dy==ey) return true;
		} 
	}else if(chess[sx][sy]%7==3){ //象 
		for(int i=0;i<4;i++){
			if(chess[sx+elephant_not_move[0][i]][sy+elephant_not_move[1][i]]) continue;
			int dx=sx+move_elephant[0][i],dy=sy+move_elephant[1][i];
			if(dx==ex&&dy==ey) return true;
		}
	}else if(chess[sx][sy]%7==4){ //士 
		for(int i=0;i<4;i++){
			int dx=sx+move_guard[0][i],dy=sy+move_guard[1][i];
			if(dx==ex&&dy==ey) return true;
		}
	}else if(chess[sx][sy]%7==5){ //王
		for(int i=0;i<4;i++){
			int dx=sx+move_captain[0][i],dy=sy+move_captain[1][i];
			if(dx==ex&&dy==ey) return true;
		} 
	}else if(chess[sx][sy]%7==6){ //印度刺客 
		for(int i=0;i<8;i++){
			if(chess[sx+duck_not_move1[0][i/2]][sy+duck_not_move1[1][i/2]]) continue;
			if(chess[sx+duck_not_move2[0][i]][sy+duck_not_move2[1][i]]) continue;
			int dx=sx+move_duck[0][i],dy=sy+move_duck[1][i];
			if(dx==ex&&dy==ey) return true;
		}
	}
	return false;
}
bool captain_will_die(int color){
	int sx,sy;
	for(int i=0;i<10;i++) for(int j=0;j<9;j++) if(chess[i][j]%7==5&&(chess[i][j]-1)/7==color) sx=i,sy=j;
	for(int i=0;i<10;i++){
		for(int j=0;j<9;j++){
			if(chess[i][j]&&(chess[i][j]-1)/7!=color){
				if(chess[i][j]%7==0){ //兵 
					for(int k=0;k<8;k++){
						int dx=i+move_soldier[0][k],dy=j+move_soldier[1][k];
						if(dx==sx&&dy==sy) return true;
					}
				}else if(chess[i][j]%7==1){ //车 
					int k;
					for(k=i+1;k<10&&!chess[k][j];k++);
					if(k==sx&&j==sy) return true;
					for(k=i-1;k>=0&&!chess[k][j];k--);
					if(k==sx&&j==sy) return true;
					for(k=j+1;k<9&&!chess[i][k];k++);
					if(i==sx&&k==sy) return true;
					for(k=j-1;k>=0&&!chess[i][k];k--);
					if(i==sx&&k==sy) return true;
				}else if(chess[i][j]%7==2){ //马 
					for(int k=0;k<8;k++){
						if(chess[i+horse_not_move[0][k/2]][j+horse_not_move[1][k/2]]) continue;
						int dx=i+move_horse[0][k],dy=j+move_horse[1][k];
						if(dx==sx&&dy==sy) return true;
					}
				}else if(chess[i][j]%7==3){ //象
					for(int k=0;k<4;k++){
						if(chess[i+elephant_not_move[0][k]][j+elephant_not_move[1][k]]) continue;
						int dx=i+move_elephant[0][k],dy=j+move_elephant[1][k];
						if(dx==sx&&dy==sy) return true;
					}
				}else if(chess[i][j]%7==4){ //士 
					for(int k=0;k<4;k++){
						int dx=i+move_guard[0][k],dy=j+move_guard[1][k];
						if(dx==sx&&dy==sy) return true;
					}
				}else if(chess[i][j]%7==5){ //王
					for(int k=0;k<4;k++){
						int dx=i+move_captain[0][k],dy=j+move_captain[1][k];
						if(dx==sx&&dy==sy) return true;
					}
				}else if(chess[i][j]%7==6){ //印度刺客 
					for(int k=0;k<8;k++){
						if(chess[i+duck_not_move1[0][k/2]][j+duck_not_move1[1][k/2]]) continue;
						if(chess[i+duck_not_move2[0][k]][j+duck_not_move2[1][k]]) continue;
						int dx=i+move_duck[0][k],dy=j+move_duck[1][k];
						if(dx==sx&&dy==sy) return true;
					}
				}
			}
		}
	}
	return false;
}
void work(bool &flag,int &color,int sx,int sy,int tx,int ty){
	//移动了哪个棋子 
	if(!color) printf("red ");
	else printf("blue ");
	if(chess[sx][sy]%7==0) printf("soldier;");
	else if(chess[sx][sy]%7==1) printf("car;");
	else if(chess[sx][sy]%7==2) printf("horse;");
	else if(chess[sx][sy]%7==3) printf("elephant;");
	else if(chess[sx][sy]%7==4) printf("guard;");
	else if(chess[sx][sy]%7==5) printf("captain;");
	else if(chess[sx][sy]%7==6) printf("duck;");
	//吃掉了哪个棋子 
	if(!chess[tx][ty]){
		printf("NA;");
		swap(chess[sx][sy],chess[tx][ty]);
	}else{
		if(!color) printf("blue ");
		else printf("red ");
		if(chess[tx][ty]%7==0) printf("soldier;");
		else if(chess[tx][ty]%7==1) printf("car;");
		else if(chess[tx][ty]%7==2) printf("horse;");
		else if(chess[tx][ty]%7==3) printf("elephant;");
		else if(chess[tx][ty]%7==4) printf("guard;");
		else if(chess[tx][ty]%7==5){
			printf("captain;");
			flag=true;
		}else if(chess[tx][ty]%7==6) printf("duck;");
		chess[tx][ty]=chess[sx][sy];
		chess[sx][sy]=0;
	}
	//是否将军
	bool lyt=false;
	if(flag) printf("no;");
	else{
		if(chess[tx][ty]%7==0){ //兵 
			for(int i=0;i<8;i++){
				int dx=tx+move_soldier[0][i],dy=ty+move_soldier[1][i];
				if((color&&chess[dx][dy]==5)||(!color&&chess[dx][dy]==12)){
					lyt=true;
					printf("yes;");
				}
			}
		}else if(chess[tx][ty]%7==1){ //车 
			int i;
			for(i=tx+1;i<10&&!chess[i][ty];i++);
			if((color&&chess[i][ty]==5)||(!color&&chess[i][ty]==12)){
				lyt=true;
				printf("yes;");
			}
			for(i=tx-1;i>=0&&!chess[i][ty]&&!lyt;i--);
			if((color&&chess[i][ty]==5)||(!color&&chess[i][ty]==12)){
				lyt=true;
				printf("yes;");
			}
			for(i=ty+1;i<9&&!chess[tx][i]&&!lyt;i++);
			if((color&&chess[tx][i]==5)||(!color&&chess[tx][i]==12)){
				lyt=true;
				printf("yes;");
			}
			for(i=ty-1;i>=0&&!chess[tx][i]&&!lyt;i--);
			if((color&&chess[tx][i]==5)||(!color&&chess[tx][i]==12)){
				lyt=true;
				printf("yes;");
			}
		}else if(chess[tx][ty]%7==2){ //马 
			for(int i=0;i<8;i++){
				int dx=tx+move_horse[0][i],dy=ty+move_horse[1][i];
				if((color&&chess[dx][dy]==5)||(!color&&chess[dx][dy]==12)){
					lyt=true;
					printf("yes;");
				}
			}
		}else if(chess[tx][ty]%7==3){ //象 
			for(int i=0;i<4;i++){
				int dx=tx+move_elephant[0][i],dy=ty+move_elephant[1][i];
				if((color&&chess[dx][dy]==5)||(!color&&chess[dx][dy]==12)){
					lyt=true;
					printf("yes;");
				}
			}
		}else if(chess[tx][ty]%7==4){ //士 
			for(int i=0;i<4;i++){
				int dx=tx+move_guard[0][i],dy=ty+move_guard[1][i];
				if((color&&chess[dx][dy]==5)||(!color&&chess[dx][dy]==12)){
					lyt=true;
					printf("yes;");
				}
			}
		}else if(chess[tx][ty]%7==5){ //王
			for(int i=0;i<4;i++){
				int dx=tx+move_captain[0][i],dy=ty+move_captain[1][i];
				if((color&&chess[dx][dy]==5)||(!color&&chess[dx][dy]==12)){
					lyt=true;
					printf("yes;");
				}
			}
		}else if(chess[tx][ty]%7==6){ //印度刺客
			for(int i=0;i<8;i++){
				int dx=tx+move_duck[0][i],dy=ty+move_duck[1][i];
				if((color&&chess[dx][dy]==5)||(!color&&chess[dx][dy]==12)){
					lyt=true;
					printf("yes;");
				}
			}
		}
		if(captain_will_die(color)){
			lyt=true;
			printf("yes;");
		}
		if(!lyt) printf("no;");
	}
	//是否获胜
	if(flag) printf("yes\n");
	else printf("no\n");
	color=1-color;
}
int main(){
	int sx,sy,tx,ty;
	bool flag=false;
	int color=0;
	scanf("%d",&q);
	while(q--){
		scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
		if(flag||!chess[sx][sy]||(chess[sx][sy]-1)/7!=color||!check_move(sx,sy,tx,ty)||(chess[tx][ty]&&(chess[tx][ty]-1)/7==color)) printf("Invalid command\n");
		else work(flag,color,sx,sy,tx,ty);
	}
	return 0;
}
2024/8/7 21:24
加载中...