恶心OJ恶心题! 模拟法太恶心了!!
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int chess[100][100]; bool cross1( int x,int y,char aim ) { if( chess[x][y]==aim ) return true; if( chess[x][y]!='.' ) return false; if( x-1>=11 && y-1>=11 ) return cross1( x-1,y-1,aim ); return false; } bool cross2( int x,int y,char aim ) { if( chess[x][y]==aim ) return true; if( chess[x][y]!='.' ) return false; if( x-1>=11 ) return cross2( x-1,y,aim ); return false; } bool cross3( int x,int y,char aim ) { if( chess[x][y]==aim ) return true; if( chess[x][y]!='.' ) return false; if( x-1>=11 && y+1<=18 ) return cross3( x-1,y+1,aim ); return false; } bool cross4( int x,int y,char aim ) { if( chess[x][y]==aim ) return true; if( chess[x][y]!='.' ) return false; if( y+1<=18 ) return cross4( x,y+1,aim ); return false; } bool cross5( int x,int y,char aim ) { if( chess[x][y]==aim ) return true; if( chess[x][y]!='.' ) return false; if( x+1<=18 && y+1<=18 ) return cross5( x+1,y+1,aim ); return false; } bool cross6( int x,int y,char aim ) { if( chess[x][y]==aim ) return true; if( chess[x][y]!='.' ) return false; if( x+1<=18 ) return cross6( x+1,y,aim ); return false; } bool cross7( int x,int y,char aim ) { if( chess[x][y]==aim ) return true; if( chess[x][y]!='.' ) return false; if( x+1<=18 && y-1>=11 ) return cross7( x+1,y-1,aim ); return false; } bool cross8( int x,int y,char aim ) { if( chess[x][y]==aim ) return true; if( chess[x][y]!='.' ) return false; if( y-1>=11 ) return cross8( x,y-1,aim ); return false; } int main() { int N=8; int T=0; //freopen( "in.txt","r",stdin ); //freopen( "out.txt","w",stdout ); while( true ) { bool conti=false; for( int i=11;i<=18;i++ ) for( int j=11;j<=18;j++ ) { scanf( "\n%c",&chess[i][j] ); if( chess[i][j]!='.' ) conti=true; } if( !conti ) break; int sum=0; for( int i=11;i<=18;i++ ) for( int j=11;j<=18;j++ ) { if( chess[i][j]=='.' ) continue; if( sum!=0 ) goto bed; else if( chess[i][j]=='p' && sum==0 ) { if( chess[i+1][j-1]=='K' || chess[i+1][j+1]=='K' ) sum+=(sum==0)?1:0; } else if( chess[i][j]=='n' && sum==0 ) { if( chess[i-1][j-2]=='K' )sum+=(sum==0)?1:0; if( chess[i-2][j-1]=='K' )sum+=(sum==0)?1:0; if( chess[i-2][j+1]=='K' )sum+=(sum==0)?1:0; if( chess[i-1][j+2]=='K' )sum+=(sum==0)?1:0; if( chess[i+1][j+2]=='K' )sum+=(sum==0)?1:0; if( chess[i+2][j+1]=='K' )sum+=(sum==0)?1:0; if( chess[i+2][j-1]=='K' )sum+=(sum==0)?1:0; if( chess[i+1][j-2]=='K' )sum+=(sum==0)?1:0; } else if( chess[i][j]=='b' && sum==0 ) { if( cross1( i-1,j-1,'K' ) ) sum+=(sum==0)?1:0; if( cross3( i-1,j+1,'K' ) ) sum+=(sum==0)?1:0; if( cross5( i+1,j+1,'K' ) ) sum+=(sum==0)?1:0; if( cross7( i+1,j-1,'K' ) ) sum+=(sum==0)?1:0; } else if( chess[i][j]=='r' && sum==0 ) { if( cross2( i-1,j,'K' ) ) sum+=(sum==0)?1:0; if( cross4( i,j+1,'K' ) ) sum+=(sum==0)?1:0; if( cross6( i+1,j,'K' ) ) sum+=(sum==0)?1:0; if( cross8( i,j-1,'K' ) ) sum+=(sum==0)?1:0; } else if( chess[i][j]=='q' && sum==0 ) { if( cross1( i-1,j-1,'K' ) ) sum+=(sum==0)?1:0; if( cross2( i-1,j,'K' ) ) sum+=(sum==0)?1:0; if( cross3( i-1,j+1,'K' ) ) sum+=(sum==0)?1:0; if( cross4( i,j+1,'K' ) ) sum+=(sum==0)?1:0; if( cross5( i+1,j+1,'K' ) ) sum+=(sum==0)?1:0; if( cross6( i+1,j,'K' ) ) sum+=(sum==0)?1:0; if( cross7( i+1,j-1,'K' ) ) sum+=(sum==0)?1:0; if( cross8( i,j-1,'K' ) ) sum+=(sum==0)?1:0; } //------------------------------------------------------------ else if( chess[i][j]=='P' && sum==0 ) { if( chess[i-1][j-1]=='k' || chess[i-1][j+1]=='k' ) sum+=(sum==0)?2:0; } else if( chess[i][j]=='N' && sum==0 ) { if( chess[i-1][j-2]=='k' )sum+=(sum==0)?2:0; if( chess[i-2][j-1]=='k' )sum+=(sum==0)?2:0; if( chess[i-2][j+1]=='k' )sum+=(sum==0)?2:0; if( chess[i-1][j+2]=='k' )sum+=(sum==0)?2:0; if( chess[i+1][j+2]=='k' )sum+=(sum==0)?2:0; if( chess[i+2][j+1]=='k' )sum+=(sum==0)?2:0; if( chess[i+2][j-1]=='k' )sum+=(sum==0)?2:0; if( chess[i+1][j-2]=='k' )sum+=(sum==0)?2:0; } else if( chess[i][j]=='B' && sum==0 ) { if( cross1( i-1,j-1,'k' ) ) sum+=(sum==0)?2:0; if( cross3( i-1,j+1,'k' ) ) sum+=(sum==0)?2:0; if( cross5( i+1,j+1,'k' ) ) sum+=(sum==0)?2:0; if( cross7( i+1,j-1,'k' ) ) sum+=(sum==0)?2:0; } else if( chess[i][j]=='R' && sum==0 ) { if( cross2( i-1,j,'k' ) ) sum+=(sum==0)?2:0; if( cross4( i,j+1,'k' ) ) sum+=(sum==0)?2:0; if( cross6( i+1,j,'k' ) ) sum+=(sum==0)?2:0; if( cross8( i,j-1,'k' ) ) sum+=(sum==0)?2:0; } else if( chess[i][j]=='Q' && sum==0 ) { if( cross1( i-1,j-1,'k' ) ) sum+=(sum==0)?2:0; if( cross2( i-1,j,'k' ) ) sum+=(sum==0)?2:0; if( cross3( i-1,j+1,'k' ) ) sum+=(sum==0)?2:0; if( cross4( i,j+1,'k' ) ) sum+=(sum==0)?2:0; if( cross5( i+1,j+1,'k' ) ) sum+=(sum==0)?2:0; if( cross6( i+1,j,'k' ) ) sum+=(sum==0)?2:0; if( cross7( i+1,j-1,'k' ) ) sum+=(sum==0)?2:0; if( cross8( i,j-1,'k' ) ) sum+=(sum==0)?2:0; } } bed: printf( "Game #%d: ",++T ); if( sum==0 ) printf( "no king is in check.\n" ); else if( sum==2 ) printf( "black king is in check.\n" ); else if( sum==1 ) printf( "white king is in check.\n" ); } return 0; }