求助
查看原帖
求助
390770
S0CRiA楼主2020/11/20 20:49

可能是将军判断出了问题

//P5380
//duck chess 
#include <iostream>
#include <string>
#define xy int xs,int ys,int xt,int yt
using namespace std;
int chessboard[10][9]={{11,12,13,14,15,14,13,12,11},
                       { 0, 0, 0, 0, 0, 0, 0, 0, 0},
                       {16, 0, 0, 0, 0, 0, 0, 0,16},
                       {17, 0,17, 0,17, 0,17, 0,17},
                       { 0, 0, 0, 0, 0, 0, 0, 0, 0},
                       { 0, 0, 0, 0, 0, 0, 0, 0, 0},
                       {27, 0,27, 0,27, 0,27, 0,27},
                       {26, 0, 0, 0, 0, 0, 0, 0,26},
                       { 0, 0, 0, 0, 0, 0, 0, 0, 0},
                       {21,22,23,24,25,24,23,22,21}};
string color[3]={"","red","blue"};
string chess[8]={"","car","horse","elephant","guard","captain","duck","soldier"};
bool pre_color=true;//red true; blue false 
bool game_over=false;
int red_captain[2]={0,4};
int blue_captain[2]={9,4};
//chessboard[x][y]
//1red 2blue
//1car 2horse 3elephant 4guard 5captain 6duck 7soldier

void print(){//打印
	for(int i=0; i<10; i++){
		for(int j=0; j<9; j++) cout << chessboard[i][j] << (chessboard[i][j]?" ":"  ");
		cout << endl;
	}
	return;
}
int is_car(xy){
	if(xs!=xt && ys!=yt) return 101;//非同一直线
	else {
		if(xs==xt) for(int i=ys+1; i<yt; i++) if(chessboard[xs][i]) return 102;//y轴隔棋 
		if(ys==yt) for(int i=xs+1; i<xt; i++) if(chessboard[i][ys]) return 103;//x轴隔棋 
	}
	return 100;
}
int is_horse(xy){
	int dx[8]={-2,-2,-1,1,-1,1,2,2},dy[8]={-1,1,-2,-2,2,2,-1,1},px[4]={-1,0,0,1},py[4]={0,-1,1,0};
	for(int i=0; i<8; i++) if(xt-xs==dx[i] && yt-ys==dy[i] && chessboard[xs+px[i/2]][ys+py[i/2]]==0) return 200;
	return 201; 
}
int is_elephant(xy){
	int dx[4]={-2,-2,2,2},dy[4]={-2,2,-2,2},px[4]={-1,-1,1,1},py[4]={-1,1,-1,1};
	for(int i=0; i<4; i++) if(xt-xs==dx[i] && yt-ys==dy[i] && chessboard[xs+px[i]][ys+py[i]]==0) return 300;
	return 301;
}
int is_guard(xy){
	int dx[4]={-1,-1,1,1},dy[4]={-1,1,-1,1};
	for(int i=0; i<4; i++) if(xt-xs==dx[i] && yt-ys==dy[i]) return 400;
	return 401;
}
int is_captain(xy){
	int dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
	for(int i=0; i<4; i++) if(xt-xs==dx[i] && yt-ys==dy[i]) return 500;
	return 501;
}
int is_duck(xy){
	int dx[8]={-3,-3,-2,2,-2,2,3,3},dy[8]={-2,2,-3,-3,3,3,-2,2};
	int px1[8]={-1,-1,0,0,0,0,1,1},py1[8]={0,0,-1,-1,1,1,0,0},px2[8]={-2,-2,-1,1,-1,1,2,2},py2[8]={-1,1,-2,-2,2,2,-1,1};
	for(int i=0; i<8; i++) if(xt-xs==dx[i] && yt-ys==dy[i] && chessboard[xs+px1[i]][ys+py1[i]]==0 && chessboard[xs+px2[i]][ys+py2[i]]==0) return 600;
	return 601;
}
int is_soldier(xy){
	if(xs-xt<2 && xs-xt>-2 && ys-yt<2 && ys-yt>-2) return 700;
	else return 701;
}
int is_invalid(xy){//输出整百数不非法,否则非法 
	int chess1=chessboard[xs][ys],chess2=chessboard[xt][yt];
	if(game_over)                    return 001;//游戏结束 
	if(!chess1)                      return 002;//无子非法 
	if(pre_color && chess1/10==2)    return 003;//红棋蓝下 
	if(!pre_color && chess1/10==1)   return 004;//蓝棋红下
	if(chess1/10==chess2/10)         return 005;//打队友
	if(xt<0 || yt<0 || xt>9 || yt>8) return 006;//出界
	if(chess1%10==1){int car=is_car(xs,ys,xt,yt);           return car;}
	if(chess1%10==2){int horse=is_horse(xs,ys,xt,yt);       return horse;} 
	if(chess1%10==3){int elephant=is_elephant(xs,ys,xt,yt); return elephant;} 
	if(chess1%10==4){int guard=is_guard(xs,ys,xt,yt);       return guard;}  
	if(chess1%10==5){int captain=is_captain(xs,ys,xt,yt);   return captain;} 
	if(chess1%10==6){int duck=is_duck(xs,ys,xt,yt);         return duck;}
	if(chess1%10==7){int soldier=is_soldier(xs,ys,xt,yt);   return soldier;}
}
bool is_check(){//输出整百数将军,否则不将军 
	int check=1;
	for(int i=0; i<10; i++) for(int j=0; j<9; j++) if(chessboard[i][j]){
		int chess1=chessboard[i][j];
		if(chess1/10==1){
			if(chess1%10==1){int car=is_car(i,j,blue_captain[0],blue_captain[1]);           check=car;}
			if(chess1%10==2){int horse=is_horse(i,j,blue_captain[0],blue_captain[1]);       check=horse;} 
			if(chess1%10==3){int elephant=is_elephant(i,j,blue_captain[0],blue_captain[1]); check=elephant;} 
			if(chess1%10==4){int guard=is_guard(i,j,blue_captain[0],blue_captain[1]);       check=guard;}  
			if(chess1%10==5){int captain=is_captain(i,j,blue_captain[0],blue_captain[1]);   check=captain;} 
			if(chess1%10==6){int duck=is_duck(i,j,blue_captain[0],blue_captain[1]);         check=duck;}
			if(chess1%10==7){int soldier=is_soldier(i,j,blue_captain[0],blue_captain[1]);   check=soldier;}
		}
		if(chess1/10==2){
			if(chess1%10==1){int car=is_car(i,j,red_captain[0],red_captain[1]);           check=car;}
			if(chess1%10==2){int horse=is_horse(i,j,red_captain[0],red_captain[1]);       check=horse;} 
			if(chess1%10==3){int elephant=is_elephant(i,j,red_captain[0],red_captain[1]); check=elephant;} 
			if(chess1%10==4){int guard=is_guard(i,j,red_captain[0],red_captain[1]);       check=guard;}  
			if(chess1%10==5){int captain=is_captain(i,j,red_captain[0],red_captain[1]);   check=captain;} 
			if(chess1%10==6){int duck=is_duck(i,j,red_captain[0],red_captain[1]);         check=duck;}
			if(chess1%10==7){int soldier=is_soldier(i,j,red_captain[0],red_captain[1]);   check=soldier;}
		}
		if(check%100==0) return true;
	}
	return false;
}
bool is_gameover(xy){
	if(chessboard[xt][yt]%10==5){
		game_over=true;
		return true;
	}
	return false;
}
void move(){
	int xs,ys,xt,yt;
	cin >> xs >> ys >> xt >> yt;
	int r_or_b1=chessboard[xs][ys]/10,r_or_b2=chessboard[xt][yt]/10;
	int chess_name1=chessboard[xs][ys]%10,chess_name2=chessboard[xt][yt]%10;
	int inv=is_invalid(xs,ys,xt,yt);
//	cout << chessboard[xs][ys] << ' ' << chessboard[xt][yt] << ' '; 
	if(inv%100){
		cout << "Invalid command" << endl;
//		print();
		return;
	}
	cout << color[r_or_b1] << ' ' << chess[chess_name1] << ';';
	if(chessboard[xt][yt]) cout << color[r_or_b2] << ' ' << chess[chess_name2] << ';';
	else                   cout << "NA;";
	if(chessboard[xs][ys]==15) red_captain[0]=xt,red_captain[1]=yt;
	if(chessboard[xs][ys]==25) blue_captain[0]=xt,blue_captain[1]=yt;
	cout << ((chessboard[xt][yt]%10!=5 && is_check())?"yes":"no") << ';';
	cout << (is_gameover(xs,ys,xt,yt)?"yes":"no");
	chessboard[xt][yt]=chessboard[xs][ys];
	chessboard[xs][ys]=0;
	pre_color=!pre_color;
	cout << endl;
//	print();
	return;
}

int main(){
	int n;
	cin >> n;
	for(int i=0; i<n; i++) move();
	return 0;
}
2020/11/20 20:49
加载中...