象棋判断绝杀
参考https://blog.csdn.net/qq_34731703/article/details/54608740
同样的思路可以判断五子棋绝杀和和棋
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int n,x1,y1; int fx[4][2]={1,0,-1,0,0,1,0,-1}; //上下左右四个方向 int hr[8][2]={1,2,1,-2,2,1,2,-1,-1,2,-1,-2,-2,1,-2,1}; //马走的八个方向 int ff[8][2]={1,1,1,-1,1,1,1,-1,-1,1,-1,-1,-1,1,-1,-1}; //分别对应马的八个方向上胖脚的方向 struct st{ int x,y; }bk[5]; char mp[15][15]; bool check1(int x,int y){ //判断是否将和帅面对面 for(int i=x+1;i<=10;i++){ if(mp[i][y]=='\0')continue; if(mp[i][y]!='G')return false; return true; } return false; } bool check(int x,int y){ if(check1(x,y))return false; for(int i=0;i<4;i++){ int h = x+fx[i][0]; int l = y+fx[i][1]; while(h>=1&&h<=10&&l>=1&&l<=9){ if(mp[h][l]=='\0') { h+=fx[i][0]; l+=fx[i][1]; continue; } if(mp[h][l]=='R')return false; //判軍 int hh = h+fx[i][0]; int ll = l+fx[i][1]; while(hh>=1&&hh<=10&&ll>=1&&ll<=9){ if(mp[hh][ll]=='\0') { hh+=fx[i][0]; ll+=fx[i][1]; continue; } if(mp[hh][ll]=='C')return false; //判炮 break; } break; } } for(int i=0;i<8;i++){ int h = x+hr[i][0]; int l = y+hr[i][1]; int hh =x+ff[i][0]; int ll = y+ff[i][1]; if(h>=1&&h<=10&&l>=1&&l<=9&&mp[h][l]=='H'&&mp[hh][ll]=='\0') { return false; //判马 } } return true; } int main() { while(~scanf("%d%d%d",&n,&x1,&y1)&&(n+x1+y1)){ memset(mp,0,sizeof(mp)); int cnt = 0; char ch[5]; int a,b; for(int i=0;i<n;i++){ scanf("%s%d%d",ch,&a,&b); mp[a][b]=ch[0]; } for(int i=0;i<4;i++){ int x = x1+fx[i][0]; int y = y1+fx[i][1]; if(x>=1&&x<=3&&y>=4&&y<=6){ bk[cnt].x = x; bk[cnt++].y = y; } } if(check1(x1,y1))cout<<"NO"<<endl; else{ int flag = 1; for(int i=0;i<cnt;i++){ char cc = mp[bk[i].x][bk[i].y]; if(cc!='\0')mp[bk[i].x][bk[i].y]='\0'; if(check(bk[i].x,bk[i].y)){ flag=0; cout<<"NO"<<endl; break; } mp[bk[i].x][bk[i].y] = cc; } if(flag)cout<<"YES"<<endl; } } return 0; }