Original title: http://acm.hdu.edu.cn/showproblem.php?pid=4121
Main topic:
For a given chessboard, there is only one black chess, red chess has n, now the Black will be able to walk a step, if how to walk can not live, is to be dead, output yes.
This problem we only need to simulate each red's attack range, and finally see if the black will be able to go where the scope of the attack can be.
#include <iostream>#include "Cstdio"#include "stdlib.h"#include "string.h"using namespace STD;intMain () {//freopen ("In.txt", "R", stdin); intmp[ A][ One];intT,sx,sy; while(scanf(" %d%d%d", &t,&sx,&sy)!=eof) {if(t==0&&sx==0&&sy==0) Break;memset(MP,0,sizeof(MP)); while(t--) {intx, y;Chars[Ten];scanf("%s%d%d", s,&x,&y);//Because it is the same as the attack range of the car, so we consider the handsome and the car as the same kind of chess if(s[0]==' G ')//Handsomemp[x][y]=2;Else if(s[0]==' R ')//Carmp[x][y]=2;Else if(s[0]==' H ')//Horsemp[x][y]=3;Else if(s[0]==' C ')//Cannonmp[x][y]=4; }//used to store attack range BOOLflag[ One][Ten];memset(Flag,false,sizeof(flag));//Traverse each point for(intI=1; i<=Ten; i++) { for(intj=1; j<=9; J + +) {//Enumerate each kind of chess if(mp[i][j]==2) {//Right for(intk=j+1; k<=9; k++) {//All points in this direction should be the attacking areaflag[i][k]=1;//Unless a piece is blocked, but the position of the piece can be attacked, ///back position will not be attacked, so here first mark and then judge, //Prevent Black from going to the next can eat your pieces there, but //The place where the pawn was taken before is not within your range of attack. if(Mp[i][k]) Break; }//Left for(intk=j-1; k>=1; k--) {flag[i][k]=1;if(Mp[i][k]) Break; }//Down for(intk=i+1; k<=Ten; k++) {flag[k][j]=1;if(Mp[k][j]) Break; }//Up for(intk=i-1; k>=1; k--) {flag[k][j]=1;if(Mp[k][j]) Break; } }//Take into account the problem of out-of-bounds, so this is divided into 8 situations //actually move the array to the lower-right corner and then shift one more grid without worrying about it. //But it's a bit painful to write code like that Else if(mp[i][j]==3) {//Left up if(mp[i][j-1]==0&&j>=3&&i>=2) {flag[i-1][j-2]=1; }//Left down if(mp[i][j-1]==0&&j>=3&&i<=9) {flag[i+1][j-2]=1; }//Right up if(mp[i][j+1]==0&&j<=8&&i>=2) {flag[i-1][j+2]=1; }//Right down if(mp[i][j+1]==0&&j<=8&&i<=9) {flag[i+1][j+2]=1; }//Up left if(mp[i-1][j]==0&&i>=3&&j>=2) {flag[i-2][j-1]=1; }//Up right if(mp[i-1][j]==0&&i>=3&&j<=8) {flag[i-2][j+1]=1; }//Down left if(mp[i+1][j]==0&&i<=7&&j>=2) {flag[i+2][j-1]=1; }//Down right if(mp[i+1][j]==0&&i<=7&&j<=8) {flag[i+2][j+1]=1; } }Else if(mp[i][j]==4) {inttflag=0;//Right for(intk=j+1; k<=9; k++) {//There is no gun shelf at the moment and scan to have chess can be a shelf if(tflag==0&&mp[i][k]!=0) tflag=1;//There is an attack range behind the shelves. Else if(tflag==1) {flag[i][k]=1;//Once there is a chess place, the chess is within the range of the attack, //But the back is gone, so jump out of the loop if(Mp[i][k]) Break; }} tflag=0;//Left for(intk=j-1; k>=1; k--) {if(tflag==0&&mp[i][k]!=0) tflag=1;Else if(tflag==1) {flag[i][k]=1;if(Mp[i][k]) Break; }} tflag=0;//Down for(intk=i+1; k<=Ten; k++) {if(tflag==0&&mp[k][j]!=0) tflag=1;Else if(tflag==1) {flag[k][j]=1;if(Mp[k][j]) Break; }} tflag=0;//Up for(intk=i-1; k>=1; k--) {if(tflag==0&&mp[k][j]!=0) tflag=1;Else if(tflag==1) {flag[k][j]=1;if(Mp[k][j]) Break; } } } } }//Map The black Fangcheng outside of the maps as the attack area, that is, out of bounds is within the scope of the attack for(intI=4; i<=6; i++) {flag[0][i]=1; flag[4][i]=1; } for(intI=1; i<=3; i++) {flag[i][3]=1; flag[i][7]=1; }//for (int i=0; i<=11; i++)// {//for (int j=0; j<=10; j + +)// {//printf ("%d", flag[i][j]);// }//printf ("\ n");// } //When the 4 sides are in attack range, GG. if(flag[sx+1][sy]&&flag[sx-1][sy]&&flag[sx][sy+1]&&flag[sx][sy-1])printf("yes\n");Else printf("no\n"); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4121 Xiangqi Simulation