题意:黑棋只有一个“将”,红棋有“马”,“炮”,“车”,“帅”中的几种,问黑棋是否没有被红棋将军。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #define repu(i,a,b) for(int i=a;i<b;i++) #define mem(a) memset(a,0,sizeof(a)) using namespace std; char graph[12][12],ch[3]; int nx,ny; int go[8][2] = {{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}}; int inBlackPalace(int x,int y) { if(x>=1&&x<=3&&y>=4&&y<=6) return 1; return 0; } int goG(int x,int y) { int a,b; if(y==ny) { if(x>nx) a=nx,b=x; else a=x,b=nx; repu(i,a,b) if(graph[i][y])///帅与将之间有间隔 return 0; return 1;///被帅将军 } return 0; } int goH(int x,int y)///蹩马腿 { ///与马能构成“日”字 if(abs(nx-x)==2&&abs(ny-y)==1) { if(graph[(nx+x)/2][y]) return 0; return 2;///被马将军 } if(abs(ny-y)==2&&abs(nx-x)==1) { if(graph[x][(ny+y)/2]) return 0; return 2;///被马将军 } return 0; } int goR(int x,int y) { int a,b; if(x==nx) { if(y>ny) a=ny,b=y; else a=y,b=ny; repu(i,a,b) if(graph[x][i]) return 0; return 3;///被车将军 } if(y==ny) { if(x>nx) a=nx,b=x; else a=x,b=nx; repu(i,a,b) if(graph[i][y]) return 0; return 3;///被车将军 } return 0; } int goC(int x,int y) { int sum = 0,a,b; if(x==nx&&y>ny) { repu(i,y,ny) { if(graph[x][i]) sum++; } if(sum==1) return 4;///被炮将军 return 0; } if(y==ny) { if(x>nx) a=nx,b=x; else a=x,b=nx; repu(i,a,b) { if(graph[i][y]) sum++; } if(sum==1) return 4; return 0; } return 0; } int goo(int x, int y) { // printf("%d-%d\n",nx,ny); switch(graph[x][y]) { case ‘G‘: return goG(x,y); case ‘R‘: return goR(x,y); case ‘H‘: return goH(x,y); case ‘C‘: return goC(x,y); } return false; } int main() { int n; while (scanf("%d", &n) != EOF) { int bx, by,temp = false,o = 0; scanf("%d%d", &bx, &by); if (n == 0 && bx == 0 && by == 0) break; memset(graph, 0, sizeof(graph)); for (int i = 0; i < n; i++) { int x, y; scanf("%s%d%d", ch, &x, &y); graph[x][y] = ch[0]; } if((bx==1&&by==4)||(bx==1&&by==6)||(bx==3&&by==4)||(bx==3&&by==6)) o = 8; else o = 4; for (int way = 0; way < o; way++) { nx = bx + go[way][0]; ny = by + go[way][1]; if (!inBlackPalace(nx, ny)) continue; char tep = graph[nx][ny]; graph[nx][ny] = 0; // printf("way is--%d\n",way); int ok = 1; for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 9; j++) { if(graph[i][j]) { // printf("%d-%d:%c\n",i,j,graph[i][j]); if(goo(i,j))///将军成功了,再换条路 { ok =0; break; } } } if(ok==0) break; } // printf("****\n"); graph[nx][ny] = tep; if(ok)///此路可以不被将军 { temp = true; break; } } if(temp) puts("NO"); else puts("YES"); } return 0; }
时间: 2024-11-18 02:15:39