#include<bits/stdc++.h>
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
#define getchar()(p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
int read()
{
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0' && c<='9')	x=(x<<1)+(x<<3)+(c^'0'),c=getchar();
	return x*f;
}
void write(int x)
{
	if(x<0)	putchar('-'),x=-x;
	if(x>9)	write(x/10);
	putchar(x%10+'0');
}
const char chessName[10][15]={"","captain","guard","elephant","horse","car","duck","soldier"};
const char colorOutput[2][10]={"red","blue"};
const char checkOutput[2][10]={"no","yes"};
const int sx[3]={0,1,-1},sy[3]={0,1,-1};
class Duckchess{
	bool Occupy(int x,int y,int c){return bool(board[x][y][c]);}
	bool Occupy(int x,int y){return bool(board[x][y][0] || board[x][y][1]);}
	bool Outside(int x,int y){return !(x>=0 && y>=0 && x<=9 && y<=8);}
	bool captainMoveCor(int s,int t,int x,int y){return abs(s-x)+abs(t-y)==1;}
	bool guardMoveCor(int s,int t,int x,int y){return abs(s-x)==1 && abs(t-y)==1;}
	bool elephantMoveCor(int s,int t,int x,int y)
	{
		for(int dx=1;dx<=2;++dx)
			for(int dy=1;dy<=2;++dy)
			{
				int p=s+sx[dx]*2,q=t+sy[dy]*2;
				if(Outside(p,q))continue;
				if(p==x&&q==y)
				{
					if(!Occupy(s+sx[dx],t+sy[dy]))return true;
					return false;
				}
			}
		return false;
	}
	bool horseMoveCor(int s,int t,int x,int y)
	{
		for(int dx=1;dx<=2;++dx)
			for(int dy=1;dy<=2;++dy)
			{
				int p=s+sx[dx]*2,q=t+sy[dy];
				if(Outside(p,q))continue;
				if(p==x&&q==y)
				{
					if(!Occupy(s+sx[dx],t))return true;
					return false;
				}
			}
		for(int dx=1;dx<=2;++dx)
			for(int dy=1;dy<=2;++dy)
			{
				int p=s+sx[dx],q=t+sy[dy]*2;
				if(Outside(p,q))continue;
				if(p==x && q==y)
				{
					if(!Occupy(s,t+sy[dy]))return true;
					return false;
				}
			}
		return false;
	}
	bool carMoveCor(int s,int t,int x,int y)
	{
		if(s!=x && t!=y)return false;
		if(s==x)
		{
			if(t>y)
			{
				for(int i=t-1;i>y;--i)if(Occupy(s,i))return false;
			}
			else
			{
				for(int i=t+1;i<y;++i)if(Occupy(s,i))return false;
			}
			return true;
		}
		else
		{
			if(s>x)
			{
				for(int i=s-1;i>x;--i)
					if(Occupy(i,t))return false;
			}
			else
			{
				for(int i=s+1;i<x;++i)
					if(Occupy(i,t))return false;
			}
			return true;
		}
	}
	bool duckMoveCor(int s,int t,int x,int y)
	{
		for(int dx=1;dx<=2;++dx)
			for(int dy=1;dy<=2;++dy)
			{
				int p=s+sx[dx]*3,q=t+sy[dy]*2;
				if(Outside(p,q))continue;
				if(p==x && q==y)
				{
					if(Occupy(s+2*sx[dx],t+sy[dy])||Occupy(s+sx[dx],t))	return false;
					return true;
				}
			}
		for(int dx=1;dx<=2;++dx)
			for(int dy=1;dy<=2;++dy)
			{
				int p=s+sx[dx]*2,q=t+sy[dy]*3;
				if(Outside(p,q))continue;
				if(p==x&&q==y)
				{
					if(Occupy(s+sx[dx],t+2*sy[dy])||Occupy(s,t+sy[dy]))return false;
					return true;
				}
			}
		return false;
	}
	bool soldierMoveCor(int s,int t,int x,int y){return abs(s-x)<=1 && abs(t-y)<=1;}
	bool moveCorrect(int op,int s,int t,int x,int y)
	{
		if(op==1)return captainMoveCor(s,t,x,y);
		else if(op==2)return guardMoveCor(s,t,x,y);
		else if(op==3)return elephantMoveCor(s,t,x,y);
		else if(op==4)return horseMoveCor(s,t,x,y);
		else if(op==5)return carMoveCor(s,t,x,y);
		else if(op==6)return duckMoveCor(s,t,x,y);
		else return soldierMoveCor(s,t,x,y);
	}
	bool attack(int x,int y,int c)
	{
		for(int i=0;i<=9;++i)
			for(int j=0;j<=8;++j)
				if(board[i][j][c]&&moveCorrect(board[i][j][c],i,j,x,y))
					return true;
		return false;
	}
public:
	int board[12][12][2];
	bool gameOver;
	Duckchess()
	{
		gameOver=false;
		board[0][4][0]=board[9][4][1]=1;
		board[0][3][0]=board[9][3][1]=board[0][5][0]=board[9][5][1]=2;
		board[0][2][0]=board[9][2][1]=board[0][6][0]=board[9][6][1]=3;
		board[0][1][0]=board[9][1][1]=board[0][7][0]=board[9][7][1]=4;
		board[0][0][0]=board[9][0][1]=board[0][8][0]=board[9][8][1]=5;
		board[2][0][0]=board[7][0][1]=board[2][8][0]=board[7][8][1]=6;
		board[3][0][0]=board[3][2][0]=board[3][4][0]=board[3][6][0]=board[3][8][0]=7;
		board[6][0][1]=board[6][2][1]=board[6][4][1]=board[6][6][1]=board[6][8][1]=7;
	}
	~Duckchess(){memset(board,0,sizeof board);}
	bool isMoveSuc(int s,int t,int x,int y,int c)
	{
		if(!Occupy(s,t,c))	return false;
		if(Occupy(x,y,c))	return false;
		if(Outside(x,y))	return false;
		int chessid=board[s][t][c];
		return moveCorrect(chessid,s,t,x,y);
	}
	void moveChess(int s,int t,int x,int y,int c)
	{
		board[x][y][c]=board[s][t][c];
		board[s][t][c]=0;
	}
	void chessDelete(int s,int t,int c)
	{
		if(board[s][t][c^1])
		{
			printf("%s %s",colorOutput[c^1],chessName[board[s][t][c^1]]);
			if(board[s][t][c^1]==1)gameOver=true;
			board[s][t][c^1]=0;
		}
		else printf("NA");
		putchar(';');
	}
	bool isGeneral()
	{
		if(gameOver)return false;
		for(int i=0;i<=9;++i)
			for(int j=0;j<=8;++j)
			{
				if(board[i][j][0]==1 && attack(i,j,1))return true;
				if(board[i][j][1]==1 && attack(i,j,0))return true;
			}
		return false;
	}
}game;
int main()
{
	int control=0,T=read();
	while(T-->0)
	{
		int s=read(),t=read(),x=read(),y=read();
		if(game.isMoveSuc(s,t,x,y,control) && !game.gameOver)
		{
			int k=game.board[s][t][control];
			printf("%s %s;",colorOutput[control],chessName[k]);
			game.moveChess(s,t,x,y,control);
			game.chessDelete(x,y,control);
			printf("%s;%s\n",checkOutput[game.isGeneral()],checkOutput[game.gameOver]);
			control^=1;
		}
		else puts("Invalid command");
	}
	return 0;
}

1 条评论

  • @ 2024-2-15 7:52:05

    e……你可以发题解

    • 1

    信息

    ID
    4306
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    16
    已通过
    10
    上传者