Title Description:
Luogu Title portal pokes him!!!
Problem solving ideas:
-
Algorithm Introduction:
The algorithm label clearly says that this problem is a proper big simulation. We can stabilize this problem without any algorithm optimization in the program. Don't ask me how I know
As we all know, the topic of large-scale simulation is a body with high programming complexity. The method of writing large-scale simulation varies from person to person. Here is my method:
Do you have to add notes everywhere in the program, so that you can clearly recognize the function of super long program modules, and help you not even understand what this function is doing when you look at the program in the future (don't give your program the feeling of reading program questions in the preliminary competition like I did before!!!)
-
Problem solving method:
First, type out several key points in the topic:
- After the game is over, no steps can be performed
- There is no need to judge the general at the end of the game
- Your chess pieces cannot kill your king
First preprocess the layout of the chessboard and the layout of the color of the chessboard pieces:
char map[10][9]={'C','M','X','S','W','S','X','M','C', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'Y',' ',' ',' ',' ',' ',' ',' ','Y', 'B',' ','B',' ','B',' ','B',' ','B', ' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'B',' ','B',' ','B',' ','B',' ','B', 'Y',' ',' ',' ',' ',' ',' ',' ','Y', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'C','M','X','S','W','S','X','M','C'}; //Initial layout of chessboard: Car C, horse M, elephant X, soldier S, King W, duck Y, soldier B char color[10][9]={'R','R','R','R','R','R','R','R','R', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'R',' ',' ',' ',' ',' ',' ',' ','R', 'R',' ','R',' ','R',' ','R',' ','R', ' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'B',' ','B',' ','B',' ','B',' ','B', 'B',' ',' ',' ',' ',' ',' ',' ','B', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'B','B','B','B','B','B','B','B','B'}; //Initial color layout of chessboard: R red, B blue string name[26]={"","soldier","car","","","","","","","","","","horse","","","","","","guard","","","","captain","elephant","duck",""}; //Save the names of all the pieces
Starting with each question that I can ask for data, first consider the first question in the question: whether the chess in the current position can go to the target position.
This problem has several limitations:
- If there are no pieces in the current position, it is obvious that you can't go
- If the current position is the same as the target position, it means that there is no movement at all and it is not allowed.
- If the color of the chess pieces in the current position does not match the color of the movable chess pieces in the current turn, neither can it.
- If the pieces in the current position can't reach the target position at all, of course not.
Obviously, the first three points are a matter of special judgment, so what really needs to knock the code is to judge where the current chess piece can go, and make a pure simulation of the movement mode of each chess piece.
- Wang:
bool Wgo(int sx,int sy,int fx,int fy,char col) //SX and sy are the coordinates of the current chess piece, FX and FY are the coordinates of the specified position, and col is the color of the current chess piece { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0}; for(int i=0;i<4;i++) //Give the coordinates that the current king can reach { int nx=sx+dx[i],ny=sy+dy[i]; if(nx<0||nx>9||ny<0||ny>8) continue; if(fx==nx&&fy==ny) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } return false; }
The boom in the function is used to judge the general, that is, if there is a legal movement of chess pieces that can eat the opponent's king, it is regarded as the general situation
- Scholar:
bool Sgo(int sx,int sy,int fx,int fy,char col) //SX and sy are the coordinates of the current chess piece, FX and FY are the coordinates of the specified position, and col is the color of the current chess piece { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int dx[4]={1,1,-1,-1},dy[4]={1,-1,1,-1}; for(int i=0;i<4;i++) //Enumerate the coordinates that can be reached by the current user { int nx=sx+dx[i],ny=sy+dy[i]; if(nx<0||nx>9||ny<0||ny>8) continue; if(fx==nx&&fy==ny) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } return false; }
- Like:
Pay attention to the situation of elephant blocking feet here
bool Xgo(int sx,int sy,int fx,int fy,char col) //SX and sy are the coordinates of the current chess piece, FX and FY are the coordinates of the specified position, and col is the color of the current chess piece { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int zx[4]={1,-1,-1,1},zy[4]={-1,-1,1,1}; //Will block the current position like the position of the foot const int dx[4]={2,-2,-2,2},dy[4]={-2,-2,2,2}; //Where the elephant can reach for(int i=0;i<4;i++) { int vx=sx+zx[i],vy=sy+zy[i],nx=sx+dx[i],ny=sy+dy[i]; //vx,vy are the positions of the elephant blocking feet, and nx,ny are the reachable positions if(nx<0||nx>9||ny<0||ny>8) continue; if(map[vx][vy]!=' ') continue; if(fx==nx&&fy==ny) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } return false; }
- horse:
bool Mgo(int sx,int sy,int fx,int fy,char col) { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass int zx[4]={1,0,-1,0},zy[4]={0,1,0,-1}; //Position coordinates that will block the horse's feet int dx[2][4]={2,-1,-2,1, 2,1,-2,-1}; int dy[2][4]={1,2,-1,-2, -1,2,1,-2}; //The horse can go in eight directions for(int i=0;i<4;i++) { int vx=sx+zx[i],vy=sy+zy[i]; //The position of the horse's foot if(vx<0||vx>9||vy<0||vy>8) continue; if(map[vx][vy]!=' ') continue; for(int j=0;j<=1;j++) { int nx=sx+dx[j][i],ny=sy+dy[j][i]; if(nx<0||nx>9||ny<0||ny>8) continue; if(fx==nx&&fy==ny) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } } return false; }
- Car:
bool Cgo(int sx,int sy,int fx,int fy,char col) { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(sx!=fx&&sy!=fy) return false; //If the current position and the target position are not in the same row and column at all, it is impossible to go there if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; int hx=sx-1,lx=sx+1,ly=sy-1,ry=sy+1; bool hx_,lx_,ly_,ry_; hx_=lx_=ly_=ry_=true; //Judge whether there are obstacles on this road while(hx_||lx_||ly_||ry_) { if(sy==fy&&hx==fx&&hx_) return true; if(sy==fy&&lx==fx&&lx_) return true; if(sx==fx&&ly==fy&&ly_) return true; if(sx==fx&&ry==fy&&ry_) return true; if(map[sx][ry]=='W'&&color[sx][ry]!=col) boom=true; if(map[sx][ly]=='W'&&color[sx][ly]!=col) boom=true; if(map[lx][sy]=='W'&&color[lx][sy]!=col) boom=true; if(map[hx][sy]=='W'&&color[hx][sy]!=col) boom=true; //It is necessary to judge whether it is the target point before judging the obstacle situation if(hx<0||map[hx][sy]!=' ') hx_=false; if(ly<0||map[sx][ly]!=' ') ly_=false; if(lx>9||map[lx][sy]!=' ') lx_=false; if(ry>8||map[sx][ry]!=' ') ry_=false; if(hx_) hx--; if(ly_) ly--; if(lx_) lx++; if(ry_) ry++; } return false; }
- Duck:
bool Ygo(int sx,int sy,int fx,int fy,char col) { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int zx1[4]={1,0,-1,0},zy1[4]={0,1,0,-1}; //Coordinates that block the duck's feet around the current position const int zx2[2][4]={2,-1,-2,1, 2,1,-2,-1}; const int zy2[2][4]={1,2,-1,-2, -1,2,1,-2}; //The coordinates of duck feet will be blocked around the sun character of the current position const int dx[2][4]={3,-2,-3,2, 3,2,-3,-2}; const int dy[2][4]={2,3,-2,-3, -2,3,2,-3}; //Where ducks can reach for(int i=0;i<4;i++) { int vx=sx+zx1[i],vy=sy+zy1[i]; //The first place to block the duck's feet if(vx<0||vx>9||vy<0||vy>8) continue; if(map[vx][vy]!=' ') continue; for(int j=0;j<=1;j++) { vx=sx+zx2[j][i],vy=sy+zy2[j][i]; //The second place to block the duck's feet if(vx<0||vx>9||vy<0||vy>8) continue; if(map[vx][vy]!=' ') continue; int nx=sx+dx[j][i],ny=sy+dy[j][i]; //Where you can walk if(nx<0||nx>9||ny<0||ny>8) continue; if(nx==fx&&ny==fy) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } } return false; }
- Soldier:
bool Bgo(int sx,int sy,int fx,int fy,char col) { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int dx[8]={1,-1,0,0,1,1,-1,-1}; //Eight directions soldiers can go const int dy[8]={0,0,1,-1,-1,1,1,-1}; for(int i=0;i<8;i++) { int nx=sx+dx[i],ny=sy+dy[i]; if(nx<0||nx>9||ny<0||ny>8) continue; if(nx==fx&&ny==fy) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } return false; }
Then deal with the second question, whether it will be by the general:
In fact, it is to facilitate the whole chessboard and see if the legal movement scheme in the chessboard will eat each other's king
void jiangjun() { int x,y; for(x=0;x<10;x++) for(y=0;y<9;y++) if(map[x][y]!=' ') { boom=false; if(map[x][y]=='W') Wgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='S') Sgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='X') Xgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='C') Cgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='M') Mgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='B') Bgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='Y') Ygo(x,y,-1,-1,color[x][y]); if(boom) { if(gameover==0) cout<<"yes;"; return ; } } cout<<"no;"; }
The rest of the questions are very simple, so I won't repeat them here.
CODE:
#include <iostream> #include <cstdio> using namespace std; char map[10][9]={'C','M','X','S','W','S','X','M','C', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'Y',' ',' ',' ',' ',' ',' ',' ','Y', 'B',' ','B',' ','B',' ','B',' ','B', ' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'B',' ','B',' ','B',' ','B',' ','B', 'Y',' ',' ',' ',' ',' ',' ',' ','Y', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'C','M','X','S','W','S','X','M','C'}; //Initial layout of chessboard: Car C, horse M, elephant X, soldier S, King W, duck Y, soldier B char color[10][9]={'R','R','R','R','R','R','R','R','R', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'R',' ',' ',' ',' ',' ',' ',' ','R', 'R',' ','R',' ','R',' ','R',' ','R', ' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'B',' ','B',' ','B',' ','B',' ','B', 'B',' ',' ',' ',' ',' ',' ',' ','B', ' ',' ',' ',' ',' ',' ',' ',' ',' ', 'B','B','B','B','B','B','B','B','B'}; //Initial color layout of chessboard: R red, B blue string name[26]={"","soldier","car","","","","","","","","","","horse","","","","","","guard","","","","captain","elephant","duck",""}; //Save the names of all the pieces bool boom=false; // Judge whether the general int gameover=0; // Judge whether the game is over. If it is not over, it is 0, if it is completed, it is 1, and if it is over, it is 2 char Color='R'; // Round color storage int n,SX,SY,FX,FY; //Wgo judges whether Wang can go to the current specified position //Sgo judges whether the person can go to the current specified position //Xgo determines whether the image can go to the current specified position //Mgo judges whether the horse can walk to the current specified position //Cgo judges whether the car can go to the current specified position //Ygo judges whether the duck can go to the current specified position //Bgo determines whether the soldier can go to the current specified position //getout move chess pieces //change round color rotation //jiangjun judges whether there is a general situation void getout(int sx,int sy,int fx,int fy) { if(map[fx][fy]!=' ') { if(color[fx][fy]=='R') cout<<"red "; else cout<<"blue "; cout<<name[map[fx][fy]-'A']<<";"; //Output eaten pieces if(map[fx][fy]=='W') gameover++; } else cout<<"NA;"; map[fx][fy]=map[sx][sy]; map[sx][sy]=' '; color[fx][fy]=color[sx][sy]; color[sx][sy]=' '; } void change(char col) { if(col=='R') Color='B'; else Color='R'; } bool Wgo(int sx,int sy,int fx,int fy,char col) //SX and sy are the coordinates of the current chess piece, FX and FY are the coordinates of the specified position, and col is the color of the current chess piece { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0}; for(int i=0;i<4;i++) //Give the coordinates that the current king can reach { int nx=sx+dx[i],ny=sy+dy[i]; if(nx<0||nx>9||ny<0||ny>8) continue; if(fx==nx&&fy==ny) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } return false; } bool Sgo(int sx,int sy,int fx,int fy,char col) //SX and sy are the coordinates of the current chess piece, FX and FY are the coordinates of the specified position, and col is the color of the current chess piece { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int dx[4]={1,1,-1,-1},dy[4]={1,-1,1,-1}; for(int i=0;i<4;i++) //Enumerate the coordinates that can be reached by the current user { int nx=sx+dx[i],ny=sy+dy[i]; if(nx<0||nx>9||ny<0||ny>8) continue; if(fx==nx&&fy==ny) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } return false; } bool Xgo(int sx,int sy,int fx,int fy,char col) //SX and sy are the coordinates of the current chess piece, FX and FY are the coordinates of the specified position, and col is the color of the current chess piece { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int zx[4]={1,-1,-1,1},zy[4]={-1,-1,1,1}; //Will block the current position like the position of the foot const int dx[4]={2,-2,-2,2},dy[4]={-2,-2,2,2}; //Where the elephant can reach for(int i=0;i<4;i++) { int vx=sx+zx[i],vy=sy+zy[i],nx=sx+dx[i],ny=sy+dy[i]; //vx,vy are the positions of the elephant blocking feet, and nx,ny are the reachable positions if(nx<0||nx>9||ny<0||ny>8) continue; if(map[vx][vy]!=' ') continue; if(fx==nx&&fy==ny) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } return false; } bool Mgo(int sx,int sy,int fx,int fy,char col) { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass int zx[4]={1,0,-1,0},zy[4]={0,1,0,-1}; //Position coordinates that will block the horse's feet int dx[2][4]={2,-1,-2,1, 2,1,-2,-1}; int dy[2][4]={1,2,-1,-2, -1,2,1,-2}; //The horse can go in eight directions for(int i=0;i<4;i++) { int vx=sx+zx[i],vy=sy+zy[i]; //The position of the horse's foot if(vx<0||vx>9||vy<0||vy>8) continue; if(map[vx][vy]!=' ') continue; for(int j=0;j<=1;j++) { int nx=sx+dx[j][i],ny=sy+dy[j][i]; if(nx<0||nx>9||ny<0||ny>8) continue; if(fx==nx&&fy==ny) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } } return false; } bool Cgo(int sx,int sy,int fx,int fy,char col) { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(sx!=fx&&sy!=fy) return false; //If the current position and the target position are not in the same row and column at all, it is impossible to go there if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; int hx=sx-1,lx=sx+1,ly=sy-1,ry=sy+1; bool hx_,lx_,ly_,ry_; hx_=lx_=ly_=ry_=true; //Judge whether there are obstacles on this road while(hx_||lx_||ly_||ry_) { if(sy==fy&&hx==fx&&hx_) return true; if(sy==fy&&lx==fx&&lx_) return true; if(sx==fx&&ly==fy&&ly_) return true; if(sx==fx&&ry==fy&&ry_) return true; if(map[sx][ry]=='W'&&color[sx][ry]!=col) boom=true; if(map[sx][ly]=='W'&&color[sx][ly]!=col) boom=true; if(map[lx][sy]=='W'&&color[lx][sy]!=col) boom=true; if(map[hx][sy]=='W'&&color[hx][sy]!=col) boom=true; //It is necessary to judge whether it is the target point before judging the obstacle situation if(hx<0||map[hx][sy]!=' ') hx_=false; if(ly<0||map[sx][ly]!=' ') ly_=false; if(lx>9||map[lx][sy]!=' ') lx_=false; if(ry>8||map[sx][ry]!=' ') ry_=false; if(hx_) hx--; if(ly_) ly--; if(lx_) lx++; if(ry_) ry++; } return false; } bool Ygo(int sx,int sy,int fx,int fy,char col) { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int zx1[4]={1,0,-1,0},zy1[4]={0,1,0,-1}; //Coordinates that block the duck's feet around the current position const int zx2[2][4]={2,-1,-2,1, 2,1,-2,-1}; const int zy2[2][4]={1,2,-1,-2, -1,2,1,-2}; //The coordinates of duck feet will be blocked around the sun character of the current position const int dx[2][4]={3,-2,-3,2, 3,2,-3,-2}; const int dy[2][4]={2,3,-2,-3, -2,3,2,-3}; //Where ducks can reach for(int i=0;i<4;i++) { int vx=sx+zx1[i],vy=sy+zy1[i]; //The first place to block the duck's feet if(vx<0||vx>9||vy<0||vy>8) continue; if(map[vx][vy]!=' ') continue; for(int j=0;j<=1;j++) { vx=sx+zx2[j][i],vy=sy+zy2[j][i]; //The second place to block the duck's feet if(vx<0||vx>9||vy<0||vy>8) continue; if(map[vx][vy]!=' ') continue; int nx=sx+dx[j][i],ny=sy+dy[j][i]; //Where you can walk if(nx<0||nx>9||ny<0||ny>8) continue; if(nx==fx&&ny==fy) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } } return false; } bool Bgo(int sx,int sy,int fx,int fy,char col) { if(fx>=0&&fx<=9&&fy>=0&&fy<=8) if(map[fx][fy]!=' '&&color[fx][fy]==col) return false; //If the target position is your own chess, you can't pass const int dx[8]={1,-1,0,0,1,1,-1,-1}; //Eight directions soldiers can go const int dy[8]={0,0,1,-1,-1,1,1,-1}; for(int i=0;i<8;i++) { int nx=sx+dx[i],ny=sy+dy[i]; if(nx<0||nx>9||ny<0||ny>8) continue; if(nx==fx&&ny==fy) return true; if(map[nx][ny]=='W'&&color[nx][ny]!=col) boom=true; } return false; } void jiangjun() { int x,y; for(x=0;x<10;x++) for(y=0;y<9;y++) if(map[x][y]!=' ') { boom=false; if(map[x][y]=='W') Wgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='S') Sgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='X') Xgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='C') Cgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='M') Mgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='B') Bgo(x,y,-1,-1,color[x][y]); if(map[x][y]=='Y') Ygo(x,y,-1,-1,color[x][y]); if(boom) { if(gameover==0) cout<<"yes;"; return ; } } cout<<"no;"; } int main() { cin>>n; for(int i=1;i<=n;i++) { cin>>SX>>SY>>FX>>FY; if(color[SX][SY]!=Color) //Judge whether the currently moving pieces belong to the color of the current turn { cout<<"Invalid command"<<endl; continue; } if(SX==FX&&SY==FY||gameover>1||SX<0||SX>9||SY<0||SY>8||FX<0||FX>9||FY<0||FY>8) //If the target position is the current position, it does not move, so it is illegal. If the game is over, there is no operation { cout<<"Invalid command"<<endl; continue; } bool flag=false; if(map[SX][SY]=='W') flag=Wgo(SX,SY,FX,FY,Color); //Judge whether Wang can go if(map[SX][SY]=='S') flag=Sgo(SX,SY,FX,FY,Color); //Judge whether the judge can go if(map[SX][SY]=='M') flag=Mgo(SX,SY,FX,FY,Color); //Judge whether the horse can walk if(map[SX][SY]=='Y') flag=Ygo(SX,SY,FX,FY,Color); //Judge whether the duck can walk if(map[SX][SY]=='B') flag=Bgo(SX,SY,FX,FY,Color); //Judge whether the soldiers can go if(map[SX][SY]=='X') flag=Xgo(SX,SY,FX,FY,Color); //Judge whether the elephant can walk if(map[SX][SY]=='C') flag=Cgo(SX,SY,FX,FY,Color); //Judge whether the car can go if(flag==false) //not able to go over { cout<<"Invalid command"<<endl; continue; } if(Color=='R') cout<<"red "; else cout<<"blue "; cout<<name[map[SX][SY]-'A']<<";"; getout(SX,SY,FX,FY); //Chess move operation jiangjun(); //Judge whether there is a general situation //Go to the right place, cover the color and pieces, and change the chessboard layout if(gameover==1) { cout<<"yes"; gameover++; } else cout<<"no"; //If the game is over cout<<endl; change(Color); //Round rotation if you can go } return 0; }