其他分享
首页 > 其他分享> > P5380 [THUPC2019]鸭棋 做题记录

P5380 [THUPC2019]鸭棋 做题记录

作者:互联网

“快过年了,给大家A个水题庆祝一下” ————2022.1.26
于是,在一个下午的码代码和一个上午的debug后

#include<iostream>
#include<cstdio>
using namespace std;
char map[15][15][3]={
	{"rc","rh","re","rg","rk","rg","re","rh","rc"},
	{"0","0","0","0","0","0","0","0","0"},
	{"rd","0","0","0","0","0","0","0","rd"},
	{"rs","0","rs","0","rs","0","rs","0","rs"},
	{"0","0","0","0","0","0","0","0","0"},
	{"0","0","0","0","0","0","0","0","0"},
	{"bs","0","bs","0","bs","0","bs","0","bs"},
	{"bd","0","0","0","0","0","0","0","bd"},
	{"0","0","0","0","0","0","0","0","0"},
	{"bc","bh","be","bg","bk","bg","be","bh","bc"}
};
int Q,xs,ys,xt,yt;//步数及其参数 
char side;//轮到哪方
bool gameover=0;//是否结束 

bool checkpoint(int xs,int ys,int xt,int yt){//检查输入的两点是否合法 
	if(!(xs>=0&&xs<=9&&xt>=0&&xt<=9&&ys>=0&&ys<=8&&yt>=0&&yt<=8)) return 0;//是否在棋盘内 
	if(map[xs][ys][0]!=side||map[xt][yt][0]==side) return 0;//棋是否是己方的,落点是否是己方的 
	if(xs==xt&&ys==yt) return 0;//原地踏步 
	return 1;
}
int abs(int x) {return x<0?(-x):x;}
bool checkpath(int xs,int ys,int xt,int yt){//判断移动路径是否合法 
	if(map[xs][ys][1]=='k'){//王 
		if(!(abs(xs-xt)+abs(ys-yt)==1)) return 0;
	}else if(map[xs][ys][1]=='g'){//士 
		if(!(abs(xs-xt)==1&&abs(ys-yt)==1)) return 0;
	}else if(map[xs][ys][1]=='e'){//象 
		if(!(abs(xs-xt)==2&&abs(ys-yt)==2&&map[(xs+xt)/2][(ys+yt)/2][0]=='0')) return 0;
	}else if(map[xs][ys][1]=='h'){//马 
		if(!((abs(xs-xt)==2&&abs(ys-yt)==1&&map[(xs+xt)/2][ys][0]=='0')
		||(abs(ys-yt)==2&&abs(xs-xt)==1&&map[xs][(ys+yt)/2][0]=='0'))) return 0;
	}else if(map[xs][ys][1]=='c'){//车 
		if(xs==xt){
			if(ys>yt) {for(int i=ys-1;i>yt;i--) {if(map[xs][i][0]!='0') {return 0;}}}
			if(ys<yt) {for(int i=ys+1;i<yt;i++) {if(map[xs][i][0]!='0') {return 0;}}}
		}else if(ys==yt){
			if(xs>xt) {for(int i=xs-1;i>xt;i--) {if(map[i][ys][0]!='0') {return 0;}}}
			if(xs<xt) {for(int i=xs+1;i<xt;i++) {if(map[i][ys][0]!='0') {return 0;}}}
		}else return 0;
	}else if(map[xs][ys][1]=='d'){//鸭 
		if(!((abs(xs-xt)==2&&abs(ys-yt)==3&&ys<yt
		&&map[xs][ys+1][0]=='0'&&map[(xs+xt)/2][ys+2][0]=='0')
		||(abs(xs-xt)==2&&abs(ys-yt)==3&&ys>yt
		&&map[xs][ys-1][0]=='0'&&map[(xs+xt)/2][ys-2][0]=='0')
		||(abs(xs-xt)==3&&abs(ys-yt)==2&&xs<xt
		&&map[xs+1][ys][0]=='0'&&map[xs+2][(ys+yt)/2][0]=='0')
		||(abs(xs-xt)==3&&abs(ys-yt)==2&&xs>xt
		&&map[xs-1][ys][0]=='0'&&map[xs-2][(ys+yt)/2][0]=='0'))) return 0;
	}else if(map[xs][ys][1]=='s'){//兵 
		if(!((abs(xs-xt)==1&&abs(ys-yt)==1)
		||(abs(xs-xt)==1&&abs(ys-yt)==0)||(abs(xs-xt)==0&&abs(ys-yt)==1))) return 0;
	} 
	return 1;
}
void print(int xs,int ys){//输出棋子 
	if(map[xs][ys][0]=='b') printf("blue ");//从属于哪方 
	else if(map[xs][ys][0]=='r') printf("red ");
	
	if(map[xs][ys][1]=='k') printf("captain");//兵种 
	else if(map[xs][ys][1]=='g') printf("guard");
	else if(map[xs][ys][1]=='e') printf("elephant");
	else if(map[xs][ys][1]=='h') printf("horse");
	else if(map[xs][ys][1]=='c') printf("car");
	else if(map[xs][ys][1]=='d') printf("duck");
	else if(map[xs][ys][1]=='s') printf("soldier");
}
void move(int xs,int ys,int xt,int yt){//下棋 
	if(map[xt][yt][1]=='k') {gameover=1;return;}
	map[xt][yt][0]=map[xs][ys][0];map[xt][yt][1]=map[xs][ys][1];
	map[xs][ys][0]='0';map[xs][ys][1]='0';
}
bool general(int xs,int ys){//是否将军 
	if(map[xs][ys][0]!='0'){
		if(map[xs][ys][1]=='k'){//王 
			if((map[xs-1][ys][1]=='k'&&map[xs][ys][0]!=map[xs-1][ys][0])
			||(map[xs][ys-1][1]=='k'&&map[xs][ys][0]!=map[xs][ys-1][0])
			||(map[xs+1][ys][1]=='k'&&map[xs][ys][0]!=map[xs+1][ys][0])
			||(map[xs][ys+1][1]=='k'&&map[xs][ys][0]!=map[xs][ys+1][0])) return 1;
		}else if(map[xs][ys][1]=='g'){//士 
			if((map[xs-1][ys-1][1]=='k'&&map[xs][ys][0]!=map[xs-1][ys-1][0])
			||(map[xs+1][ys-1][1]=='k'&&map[xs][ys][0]!=map[xs+1][ys-1][0])
			||(map[xs+1][ys+1][1]=='k'&&map[xs][ys][0]!=map[xs+1][ys+1][0]) 
			||(map[xs-1][ys+1][1]=='k'&&map[xs][ys][0]!=map[xs-1][ys+1][0])) return 1;
		}else if(map[xs][ys][1]=='e'){//象 
			if((map[xs+2][ys+2][1]=='k'&&map[xs+1][ys+1][0]=='0'
			&&map[xs][ys][0]!=map[xs+2][ys+2][0])
			||(map[xs+2][ys-2][1]=='k'&&map[xs+1][ys-1][0]=='0'
			&&map[xs][ys][0]!=map[xs+2][ys-2][0])
			||(map[xs-2][ys+2][1]=='k'&&map[xs-1][ys+1][0]=='0'
			&&map[xs][ys][0]!=map[xs-2][ys+2][0])
			||(map[xs-2][ys-2][1]=='k'&&map[xs-1][ys-1][0]=='0'
			&&map[xs][ys][0]!=map[xs-2][ys-2][0])) return 1;
		}else if(map[xs][ys][1]=='h'){//马 
			if((map[xs+2][ys+1][1]=='k'&&map[xs+1][ys][0]=='0'
			&&map[xs][ys][0]!=map[xs+2][ys+1][0])
			||(map[xs+2][ys-1][1]=='k'&&map[xs+1][ys][0]=='0'
			&&map[xs][ys][0]!=map[xs+2][ys-1][0])
			||(map[xs-2][ys+1][1]=='k'&&map[xs-1][ys][0]=='0'
			&&map[xs][ys][0]!=map[xs-2][ys+1][0])
			||(map[xs-2][ys-1][1]=='k'&&map[xs-1][ys][0]=='0'
			&&map[xs][ys][0]!=map[xs-2][ys-1][0])
			||(map[xs+1][ys-2][1]=='k'&&map[xs][ys-1][0]=='0'
			&&map[xs][ys][0]!=map[xs+1][ys-2][0])
			||(map[xs+1][ys+2][1]=='k'&&map[xs][ys+1][0]=='0'
			&&map[xs][ys][0]!=map[xs+1][ys+2][0])
			||(map[xs-1][ys-2][1]=='k'&&map[xs][ys-1][0]=='0'
			&&map[xs][ys][0]!=map[xs-1][ys-2][0])
			||(map[xs-1][ys+2][1]=='k'&&map[xs][ys+1][0]=='0'
			&&map[xs][ys][0]!=map[xs-1][ys+2][0]))return 1;
		}else if(map[xs][ys][1]=='c'){//车 
			for(int i=xs-1;i>=0;i--)
				if(map[i][ys][0]!='0')
					{if(map[i][ys][1]=='k'&&map[xs][ys][0]!=map[i][ys][0]) {return 1;} break;}
			for(int i=xs+1;i<=9;i++)
				if(map[i][ys][0]!='0')
					{if(map[i][ys][1]=='k'&&map[xs][ys][0]!=map[i][ys][0]) {return 1;} break;}
			for(int i=ys-1;i>=0;i--)
				if(map[xs][i][0]!='0')
					{if(map[xs][i][1]=='k'&&map[xs][ys][0]!=map[xs][i][0]) {return 1;} break;}
			for(int i=ys+1;i<9;i++)
				if(map[xs][i][0]!='0')
					{if(map[xs][i][1]=='k'&&map[xs][ys][0]!=map[xs][i][0]) {return 1;} break;}
		}else if(map[xs][ys][1]=='d'){//鸭 
			if((map[xs+3][ys+2][1]=='k'&&map[xs][ys][0]!=map[xs+3][ys+2][0]
			&&map[xs+1][ys][0]=='0'&&map[xs+2][ys+1][0]=='0')
			||(map[xs+3][ys-2][1]=='k'&&map[xs][ys][0]!=map[xs+3][ys-2][0]
			&&map[xs+1][ys][0]=='0'&&map[xs+2][ys-1][0]=='0')
			||(map[xs-3][ys+2][1]=='k'&&map[xs][ys][0]!=map[xs-3][ys+2][0]
			&&map[xs-1][ys][0]=='0'&&map[xs-2][ys+1][0]=='0')
			||(map[xs-3][ys-2][1]=='k'&&map[xs][ys][0]!=map[xs-3][ys-2][0]
			&&map[xs-1][ys][0]=='0'&&map[xs-2][ys-1][0]=='0')
			||(map[xs+2][ys+3][1]=='k'&&map[xs][ys][0]!=map[xs+2][ys+3][0]
			&&map[xs][ys+1][0]=='0'&&map[xs+1][ys+2][0]=='0')
			||(map[xs+2][ys-3][1]=='k'&&map[xs][ys][0]!=map[xs+2][ys-3][0]
			&&map[xs][ys-1][0]=='0'&&map[xs+1][ys-2][0]=='0')
			||(map[xs-2][ys+3][1]=='k'&&map[xs][ys][0]!=map[xs-2][ys+3][0]
			&&map[xs][ys+1][0]=='0'&&map[xs-1][ys+2][0]=='0')
			||(map[xs-2][ys-3][1]=='k'&&map[xs][ys][0]!=map[xs-2][ys-3][0]
			&&map[xs][ys-1][0]=='0'&&map[xs-1][ys-2][0]=='0')) return 1;
		}else if(map[xs][ys][1]=='s'){//兵 
			if((map[xs-1][ys][1]=='k'&&map[xs][ys][0]!=map[xs-1][ys][0])
			||(map[xs][ys-1][1]=='k'&&map[xs][ys][0]!=map[xs][ys-1][0])
			||(map[xs+1][ys][1]=='k'&&map[xs][ys][0]!=map[xs+1][ys][0])
			||(map[xs][ys+1][1]=='k'&&map[xs][ys][0]!=map[xs][ys+1][0])
			||(map[xs-1][ys-1][1]=='k'&&map[xs][ys][0]!=map[xs-1][ys-1][0])
			||(map[xs+1][ys-1][1]=='k'&&map[xs][ys][0]!=map[xs+1][ys-1][0])
			||(map[xs+1][ys+1][1]=='k'&&map[xs][ys][0]!=map[xs+1][ys+1][0])
			||(map[xs-1][ys+1][1]=='k'&&map[xs][ys][0]!=map[xs-1][ys+1][0])) return 1;
		} 
	}
	return 0;
} 
bool genecheck(){
	for(int i=0;i<10;i++)
		for(int j=0;j<9;j++)
			if(general(i,j)) {return 1;}
	return 0; 
}

int main(){
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&Q);
	side='r'; 
	for(int xxx=1;xxx<=Q;xxx++){
		scanf("%d%d%d%d",&xs,&ys,&xt,&yt);
		if(gameover) {printf("Invalid command\n");continue;}//是否已(fu)经(yu)结(wan)束(kang) 
		if(!checkpoint(xs,ys,xt,yt)) {printf("Invalid command\n");continue;}//检查输入的两点是否合法 
		if(!checkpath(xs,ys,xt,yt)) {printf("Invalid command\n");continue;}//判断移动路径是否合法 
		print(xs,ys);printf(";");//输出移动棋子 
		if(map[xt][yt][0]=='0') printf("NA;"); else {print(xt,yt);printf(";");}//输出被吃棋子
		move(xs,ys,xt,yt);//下棋 
		if(gameover) {printf("no;yes\n");continue;}//结束~啦 
		if(genecheck()) {printf("yes;");}//判断是否将军 
		else printf("no;");
		printf("no\n");
		side=(side=='r'?'b':'r');//更换下棋方 
	}
	return 0;
}

标签:map,int,鸭棋,THUPC2019,P5380,&&,xs,ys,xt
来源: https://www.cnblogs.com/qzhwlzy/p/15849224.html