题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=13&problem=1137&mosmsg=Submission+received+with+ID+8744125
题意很简单,就是判断king有木有被攻击
可以直接模拟
我的代码:(写的又臭又长。。)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int x; int y; }; node k1,k2; char map[10][10]; bool unat[10][10]; char map1[10][10],map2[10][10]; bool at1[10][10],at2[10][10]; int dx[8]={1,2,-1,-2,1,2,-1,-2}; int dy[8]={2,1,2,1,-2,-1,-2,-1}; int n,m; void slove1() { int i,j,a,nx,ny; for(i=1;i<=8;i++) for(j=1;j<=8;j++) { if(map1[i][j]=='p') { at1[i+1][j-1]=true; at1[i+1][j+1]=true; } if(map1[i][j]=='r') { for(a=1;a<=8;a++) { if(!unat[i][a+j]&&a+j<=8) at1[i][a+j]=true; else if(unat[i][a+j]==true) { at1[i][a+j]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i][j-a]&&j-a>=1) at1[i][j-a]=true; else if(unat[i][j-a]==true) { at1[i][j-a]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i+a][j]&&i+a<=8) at1[i+a][j]=true; else if(unat[i+a][j]==true) { at1[i+a][j]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i-a][j]&&i-a<=8) at1[i-a][j]=true; else if(unat[i-a][j]==true) { at1[i-a][j]=true; break; } else break; } } if(map1[i][j]=='b') { for(a=1;a<=8;a++) { if(i+a<=8&&j+a<=8&&!unat[i+a][j+a]) at1[i+a][j+a]=true; else if(unat[i+a][j+a]==true) { at1[i+a][j+a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i+a<=8&&j-a>=1&&!unat[i+a][j-a]) at1[i+a][j-a]=true; else if(unat[i+a][j-a]==true) { at1[i+a][j-a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i-a>=1&&j+a<=8&&!unat[i-a][j+a]) at1[i-a][j+a]=true; else if(unat[i-a][j+a]==true) { at1[i-a][j+a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i-a>=1&&j-a>=1&&!unat[i-a][j-a]) at1[i-a][j-a]=true; else if(unat[i-a][j-a]==true) { at1[i-a][j-a]=true; break; } else break; } } if(map1[i][j]=='q') { for(a=1;a<=8;a++) { if(!unat[i][a+j]&&a+j<=8) at1[i][a+j]=true; else if(unat[i][a+j]==true) { at1[i][a+j]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i][j-a]&&j-a>=1) at1[i][j-a]=true; else if(unat[i][j-a]==true) { at1[i][j-a]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i+a][j]&&i+a<=8) at1[i+a][j]=true; else if(unat[i+a][j]==true) { at1[i+a][j]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i-a][j]&&i-a<=8) at1[i-a][j]=true; else if(unat[i-a][j]==true) { at1[i-a][j]=true; break; } else break; } for(a=1;a<=8;a++) { if(i+a<=8&&j+a<=8&&!unat[i+a][j+a]) at1[i+a][j+a]=true; else if(unat[i+a][j+a]==true) { at1[i+a][j+a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i+a<=8&&j-a>=1&&!unat[i+a][j-a]) at1[i+a][j-a]=true; else if(unat[i+a][j-a]==true) { at1[i+a][j-a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i-a>=1&&j+a<=8&&!unat[i-a][j+a]) at1[i-a][j+a]=true; else if(unat[i-a][j+a]==true) { at1[i-a][j+a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i-a>=1&&j-a>=1&&!unat[i-a][j-a]) at1[i-a][j-a]=true; else if(unat[i-a][j-a]==true) { at1[i-a][j-a]=true; break; } else break; } } if(map1[i][j]=='k') { k1.x=i; k1.y=j; at1[i][j+1]=true; at1[i+1][j]=true; at1[i+1][j+1]=true; at1[i-1][j+1]=true; at1[i-1][j]=true; at1[i+1][j-1]=true; at1[i][j-1]=true; at1[i-1][j-1]=true; at1[i][j]=false; } if(map1[i][j]=='n') { for(a=0;a<8;a++) { nx=i+dx[a]; ny=j+dy[a]; if(nx>=1&&nx<=8&&ny>=1&&ny<=8) at1[nx][ny]=true; } } } } void slove2() { int i,j,a,nx,ny; for(i=1;i<=8;i++) for(j=1;j<=8;j++) { if(map2[i][j]=='P') { at2[i-1][j-1]=true; at2[i-1][j+1]=true; } if(map2[i][j]=='R') { for(a=1;a<=8;a++) { if(!unat[i][a+j]&&a+j<=8) at2[i][a+j]=true; else if(unat[i][a+j]==true) { at2[i][a+j]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i][j-a]&&j-a>=1) at2[i][j-a]=true; else if(unat[i][j-a]==true) { at2[i][j-a]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i+a][j]&&i+a<=8) at2[i+a][j]=true; else if(unat[i+a][j]==true) { at2[i+a][j]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i-a][j]&&i-a<=8) at2[i-a][j]=true; else if(unat[i-a][j]==true) { at2[i-a][j]=true; break; } else break; } } if(map2[i][j]=='B') { for(a=1;a<=8;a++) { if(i+a<=8&&j+a<=8&&!unat[i+a][j+a]) at2[i+a][j+a]=true; else if(unat[i+a][j+a]==true) { at2[i+a][j+a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i+a<=8&&j-a>=1&&!unat[i+a][j-a]) at2[i+a][j-a]=true; else if(unat[i+a][j-a]==true) { at2[i+a][j-a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i-a>=1&&j+a<=8&&!unat[i-a][j+a]) at2[i-a][j+a]=true; else if(unat[i-a][j+a]==true) { at2[i-a][j+a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i-a>=1&&j-a>=1&&!unat[i-a][j-a]) at2[i-a][j-a]=true; else if(unat[i-a][j-a]==true) { at2[i-a][j-a]=true; break; } else break; } } if(map2[i][j]=='Q') { for(a=1;a<=8;a++) { if(!unat[i][a+j]&&a+j<=8) at2[i][a+j]=true; else if(unat[i][a+j]==true) { at2[i][a+j]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i][j-a]&&j-a>=1) at2[i][j-a]=true; else if(unat[i][j-a]==true) { at2[i][j-a]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i+a][j]&&i+a<=8) at2[i+a][j]=true; else if(unat[i+a][j]==true) { at2[i+a][j]=true; break; } else break; } for(a=1;a<=8;a++) { if(!unat[i-a][j]&&i-a<=8) at2[i-a][j]=true; else if(unat[i-a][j]==true) { at2[i-a][j]=true; break; } else break; } for(a=1;a<=8;a++) { if(i+a<=8&&j+a<=8&&!unat[i+a][j+a]) at2[i+a][j+a]=true; else if(unat[i+a][j+a]==true) { at2[i+a][j+a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i+a<=8&&j-a>=1&&!unat[i+a][j-a]) at2[i+a][j-a]=true; else if(unat[i+a][j-a]==true) { at2[i+a][j-a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i-a>=1&&j+a<=8&&!unat[i-a][j+a]) at2[i-a][j+a]=true; else if(unat[i-a][j+a]==true) { at2[i-a][j+a]=true; break; } else break; } for(a=1;a<=8;a++) { if(i-a>=1&&j-a>=1&&!unat[i-a][j-a]) at2[i-a][j-a]=true; else if(unat[i-a][j-a]==true) { at2[i-a][j-a]=true; break; } else break; } } if(map2[i][j]=='K') { k2.x=i; k2.y=j; at2[i][j+1]=true; at2[i+1][j]=true; at2[i+1][j+1]=true; at2[i-1][j+1]=true; at2[i-1][j]=true; at2[i+1][j-1]=true; at2[i][j-1]=true; at2[i-1][j-1]=true; at2[i][j]=false; } if(map2[i][j]=='N') { for(a=0;a<8;a++) { nx=i+dx[a]; ny=j+dy[a]; if(nx>=1&&nx<=8&&ny>=1&&ny<=8) at2[nx][ny]=true; } } } } int main() { int i,j,t=1; while(true) { m=0,n=0; memset(map,'.',sizeof(map)); memset(map1,'.',sizeof(map1)); memset(map2,'.',sizeof(map2)); memset(unat,0,sizeof(unat)); memset(at1,0,sizeof(at1)); memset(at2,0,sizeof(at2)); for(i=1;i<=8;i++) scanf("%s",map[i]+1); for(i=1;i<=8;i++) for(j=1;j<=8;j++) { if(map[i][j]>='a'&&map[i][j]<='z') { map1[i][j]=map[i][j]; unat[i][j]=true; n++; } if(map[i][j]>='A'&&map[i][j]<='Z') { map2[i][j]=map[i][j]; unat[i][j]=true; m++; } } if(n==0&&m==0) break; slove1(); slove2(); /* printf("/n"); for(i=1;i<=8;i++) { for(j=1;j<=8;j++) { if(at1[i][j]) printf("1"); else printf("0"); } printf("/n"); } printf("/n"); for(i=1;i<=8;i++) { for(j=1;j<=8;j++) { if(at2[i][j]) printf("1"); else printf("0"); } printf("/n"); } // if(at1[k2.x][k2.y]) // printf("white/n"); // if(at2[k1.x][k1.y]) // printf("black/n");*/ if(at1[k2.x][k2.y]==false&&at2[k1.x][k1.y]==true) printf("Game #%d: black king is in check./n",t++); if(at1[k2.x][k2.y]==true&&at2[k1.x][k1.y]==false) printf("Game #%d: white king is in check./n",t++); if(at1[k2.x][k2.y]==false&&at2[k1.x][k1.y]==false) printf("Game #%d: no king is in check./n",t++); } return 0; }