玄关,马蜂自认良好,注释自认较全,WAon#2求调
查看原帖
玄关,马蜂自认良好,注释自认较全,WAon#2求调
511811
崛起的滑稽楼主2024/10/6 10:32

错误信息:#2:wrong answer On line 23 column 1, read I, expected b.

#include <bits/stdc++.h>
//#define int int64_t
//#define int __int128
//#define MOD (1000000007)
//#define eps (1e-6)
#define endl '\n'
#define debug_endl cout<<endl;
#define debug cout<<"debug"<<endl;
using namespace std;
enum chess{
	CAPTAIN=1,		//王
	GUARD=2,		//士
	ELEPHANT=3,		//象
	HORSE=4,		//马
	CAR=5,			//车
	DUCK=6,			//鸭
	SOLDIER=7,		//兵
};
enum teamColor{
	RED=1,			//红方
	EMPTY=0,		//无棋子
	BLUE=-1,		//蓝方
};
struct Position{//坐标结构体
	int x,y;//第x行第y列
	bool operator==(const Position& other){
		return (other.x==x&&other.y==y);
	}
	bool operator!=(const Position& other){
		return (other.x!=x||other.y!=y);
	}
	Position operator=(const Position& other){
		x=other.x;
		y=other.y;
		return *this;
	}
};
istream &operator>>(istream &in, Position &p){//输入坐标
	int x,y;
	cin>>x>>y;
	p={x,y};
	return in;
}

const int WEIGHT=9;//棋盘宽度
const int HEIGHT=10;//棋盘高度

int nowTeam=1;//当前操作者,红方先手
int q;
int chessboard[HEIGHT+1][WEIGHT+1];//棋盘上的棋子类型
int team[HEIGHT+1][WEIGHT+1];//属于红方还是蓝方
bool gameOver;//游戏是否结束

string team_color[4]={"","blue"," ","red"};
string chess_color[8]={" ","captain","guard","elephant","horse","car","duck","soldier"};

//棋子合法性判断数组
int moveList[7][2][8]={
	{{0,0,1,-1},				//王
	 {1,-1,0,0}},	
	{{1,1,-1,-1},				//士
	 {-1,1,1,-1}},	
	{{2,2,-2,-2},				//象
	 {-2,2,2,-2}},
	{{2,1,-1,-2,-2,-1,1,2},		//马
	 {1,2,2,1,-1,-2,-2,-1}},
	{{},						//车(无法打表)
	 {}},
	{{3,2,-2,-3,-3,-2,2,3},		//鸭
	 {2,3,3,2,-2,-3,-3,-2}},
	{{1,1,0,-1,-1,-1,0,1},		//兵
	 {0,1,1,1,0,-1,-1,-1}},
	
};
int moveListLen[7]={4,4,4,8,0,8,8};//移动数组长度
//蹩脚判断数组
int cannotMoveList[7][2][8]{
	{{},						//王(无蹩脚)
	 {}},
	{{},						//士(无蹩脚)
	 {}},
	{{1,1,-1,-1},				//象
	 {-1,1,1,-1}},
	{{1,0,0,-1,-1,0,0,1},		//马
	 {0,1,1,0,0,-1,-1,0}},
	{{},						//车(无蹩脚)
	 {}},
	{{},						//鸭(两种情况,另行开打表数组)
	 {}},
	{{},						//兵(无蹩脚)
	 {}},
};
int cannotMoveList_duck[2][2][8]{//鸭蹩脚数组
	{{1,0,0,-1,-1,0,0,1},		//蹩脚点1	
	 {0,1,1,0,0,-1,-1,0}},
	{{2,1,-1,-2,-2,-1,1,2},		//蹩脚点2
	 {1,2,2,1,-1,-2,-2,-1}}
};

bool isOutOfTheSize(int x,int y){//判断是否出界
	return (x>HEIGHT||x<1||y>WEIGHT||y<1);
}
bool canMove_(int chessC,Position s,Position e){//判断是否匹配
	int dx=e.x-s.x;//x的变化量
	int dy=e.y-s.y;//y的变化量
	for(int i=0;i<moveListLen[chessC-1];++i){
		if(dx==moveList[chessC-1][0][i]&&dy==moveList[chessC-1][1][i]){
			return (chessC==DUCK?team[s.x+cannotMoveList_duck[0][0][i]][s.y+cannotMoveList_duck[0][1][i]]==EMPTY&&//鸭子要走的地方是否为空(蹩脚情况1)
				team[s.x+cannotMoveList_duck[1][0][i]][s.y+cannotMoveList_duck[1][1][i]]==EMPTY://鸭子蹩脚情况2
				(chessC==ELEPHANT||chessC==HORSE?team[s.x+cannotMoveList[chessC-1][0][i]][s.y+cannotMoveList[chessC-1][1][i]]==EMPTY://象或马走的地方是否为空
				true));//其他不会被蹩脚
		}
	}
	return false;
}
bool canMove(int chessC,Position s,Position e){//对棋子进行分类讨论
	if(chessC!=CAR){
		return canMove_(chessC,s,e);
	}
	else{
		if(e.x-s.x!=0&&e.y-s.y!=0){
			return false;
		}
		Position p=s;
		//计算方向
		int dx=(e.x-s.x)/abs(e.x-s.x);
		int dy=(e.y-s.y)/abs(e.y-s.y);
		for(;p!=e;p.x+=dx,p.y+=dy){//沿着车的路径遍历看看有没有棋子挡着
			if(chessboard[p.x][p.y]){//有
//				cout<<"ERR:车被阻挡 ";
				return false;
			}
		}
		return true;
	}
}
bool isCompliant(Position s,Position e){//检测是否合法
	if(isOutOfTheSize(s.x,s.y)||isOutOfTheSize(e.x,e.y)){//两坐标有任一越界
//		cout<<"ERR:越界 ";
		return false;
	}
	if(team[s.x][s.y]!=nowTeam){//不是己方棋子
//		cout<<"ERR: 非己方棋子 ";
		return false;
	}
	if(team[e.x][e.y]==nowTeam){//不能落在己方棋子上
//		cout<<"ERR:目标地点为己方棋子 ";
		return false;
	}
	if(gameOver){//游戏已经结束
//		cout<<"ERR:游戏已经结束 ";
		return false;
	}
	if(!canMove(chessboard[s.x][s.y],s,e)){//无法移动
		return false;
	}
	return true;
}
bool checkmate(){//判断是否将军
	for(int cx=1;cx<=HEIGHT;++cx){
		for(int cy=1;cy<=WEIGHT;++cy){
			if(team[cx][cy]!=EMPTY){
				int t=team[cx][cy];
				int chessC=chessboard[cx][cy];
				Position pos=(Position){cx,cy};
				if(chessC==CAR){//车需要单独判断
					for(int x=pos.x+1;x<=HEIGHT;++x){//四向暴力循环看看碰到的第一个棋子是否是王
						if(team[x][pos.y]!=EMPTY){
							if(chessboard[x][pos.y]==CAPTAIN&&team[x][pos.y]==-t){//必须是对面的王才是将军
								return true;
							}
							break;//只算碰到的第一个棋子
						}
					}
					for(int x=pos.x-1;x>=1;--x){
						if(team[x][pos.y]!=EMPTY){
							if(chessboard[x][pos.y]==CAPTAIN&&team[x][pos.y]==-t){
								return true;
							}
							break;
						}
					}
					for(int y=pos.y+1;y<=WEIGHT;++y){
						if(team[pos.x][y]!=EMPTY){
							if(chessboard[pos.x][y]==CAPTAIN&&team[pos.x][y]==-t){
								return true;
							}
							break;
						}
					}
					for(int y=pos.y-1;y>=1;--y){
						if(team[pos.x][y]!=EMPTY){
							if(chessboard[pos.x][y]==CAPTAIN&&team[pos.x][y]==-t){
								return true;
							}
							break;
						}
					}
				}
				else{//其他的转一下判断就行
					for(int i=0;i<moveListLen[chessC-1];++i){
						int x=pos.x+moveList[chessC-1][0][i];
						int y=pos.y+moveList[chessC-1][1][i];
						if(isOutOfTheSize(x,y)||!canMove(chessC,pos,(Position){x,y})){//蹩脚了也不能将军
							continue;
						}
						if(chessboard[x][y]==CAPTAIN&&team[x][y]==-t){
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}
void moveChess(Position s,Position e){//移动棋子
	team[e.x][e.y]=team[s.x][s.y];
	team[s.x][s.y]=EMPTY;
	chessboard[e.x][e.y]=chessboard[s.x][s.y];
	chessboard[s.x][s.y]=EMPTY;
}
void init(){
	//初始化棋盘
	chessboard[1][1]=chessboard[1][9]=chessboard[10][1]=chessboard[10][9]=CAR;
	chessboard[1][2]=chessboard[1][8]=chessboard[10][2]=chessboard[10][8]=HORSE;
	chessboard[1][3]=chessboard[1][7]=chessboard[10][3]=chessboard[10][7]=ELEPHANT;
	chessboard[1][4]=chessboard[1][6]=chessboard[10][4]=chessboard[10][6]=GUARD;
	chessboard[1][5]=chessboard[10][5]=CAPTAIN;
	chessboard[3][1]=chessboard[3][9]=chessboard[8][1]=chessboard[8][9]=DUCK;
	chessboard[4][1]=chessboard[4][3]=chessboard[4][5]=chessboard[4][7]=chessboard[4][9]=SOLDIER;
	chessboard[7][1]=chessboard[7][3]=chessboard[7][5]=chessboard[7][7]=chessboard[7][9]=SOLDIER;
	
	//初始化阵营
	for(int i=1;i<=9;++i){
		team[1][i]=RED;
		team[10][i]=BLUE;
	}
	team[3][1]=team[3][9]=team[4][1]=team[4][3]=team[4][5]=team[4][7]=team[4][9]=RED;
	team[8][1]=team[8][9]=team[7][1]=team[7][3]=team[7][5]=team[7][7]=team[7][9]=BLUE;
}
void printChessboard(){
	cout<<endl;
	for(int i=1;i<=HEIGHT;++i){
		for(int j=1;j<=WEIGHT;++j){
			cout<<team[i][j]+2<<chessboard[i][j]<<' ';
		}
		cout<<endl;
	}
}
signed main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	init();
	cin>>q;
	for(int step=1;step<=q;++step){
		Position ps;//起始坐标
		Position pe;//结束坐标
		cin>>ps>>pe;
		++ps.x,++ps.y,++pe.x,++pe.y;//下标从1开始
		
		//判断是否合法
		if(!isCompliant(ps,pe)){//不合法
			cout<<"Invalid command"<<endl;
			continue;
		}
		
		int chessp=chessboard[ps.x][ps.y];
		
		//输出移动的棋子
		cout<<team_color[nowTeam+2]<<' '<<chess_color[chessp]<<';';
		
		//输出吃掉的棋子
		if(team[pe.x][pe.y]!=EMPTY){//吃掉棋子
			cout<<team_color[(-nowTeam)+2]<<' '<<chess_color[chessboard[pe.x][pe.y]]<<';';
		}
		else{
			cout<<"NA;";//没吃掉
		}
		
		
		//判断是否结束
		if(chessboard[pe.x][pe.y]==CAPTAIN){//把对面王吃了
			gameOver=true;
		}
		
		moveChess(ps,pe);//移动棋子,先移动再全局判断将军
		
		//判断是否将军
		if(!gameOver&&checkmate()){//必须全局判断
			cout<<"yes;";
		}
		else{
			cout<<"no;";
		}
		
		if(gameOver){
			cout<<"yes";
		}
		else{
			cout<<"no";
		}
		
		cout<<endl;//行位要换行
		nowTeam=-nowTeam;//变换操作者
	}
	return 0;
}
2024/10/6 10:32
加载中...