Created
May 10, 2015 08:46
-
-
Save mob5566/4dcf572c44339b52e9fc to your computer and use it in GitHub Desktop.
10196 - Check The Check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tittle: 10196 - Check The Check | |
* Author: Cheng-Shih, Wong | |
* Date: 2015/05/10 | |
*/ | |
// include files | |
#include <iostream> | |
#include <cstdio> | |
#include <cstring> | |
using namespace std; | |
// definitions | |
#define FOR(i,a,b) for( int i=(a),_n=(b); i<=_n; ++i ) | |
#define clr(x,v) memset( x, v, sizeof(x) ) | |
#define N 10 | |
struct Point { | |
int x, y; | |
Point( int _x=0, int _y = 0 ): x(_x), y(_y) {} | |
const Point operator+( const Point &op ) const { | |
return Point(x+op.x,y+op.y); | |
} | |
}; | |
// declarations | |
int gi = 1; | |
int s = 8; | |
char cb[N][N]; | |
// functions | |
bool input( void ) | |
{ | |
FOR( i, 1, s ) gets( cb[i]+1 ); | |
FOR( i, 1, s ) | |
if( strcmp( cb[i]+1, "........" ) ) | |
return true; | |
return false; | |
} | |
bool valid( const Point &p ) | |
{ | |
return (1<=p.x && p.x<=s && 1<=p.y && p.y<=s); | |
} | |
char *(tcb[N]); | |
const char who( const Point &p, const Point &v, int times ) | |
{ | |
Point cur = p; | |
while( valid(cur=(cur+v)) && times-- ) { | |
if( tcb[cur.x][cur.y] != '.' ) { | |
// printf( "(%d,%d) = %c\n", cur.x, cur.y, tcb[cur.x][cur.y] ); | |
return tcb[cur.x][cur.y]; | |
} | |
} | |
return 0; | |
} | |
bool check( Point P ) | |
{ | |
char p, r, b, q, n; | |
char ch; | |
if( cb[P.x][P.y] == 'K' ) { | |
FOR( i, 1, s ) tcb[i] = cb[i]; | |
p = 'p'; r = 'r'; | |
b = 'b'; q = 'q'; | |
n = 'n'; | |
} else { | |
FOR( i, 1, s ) tcb[i] = cb[s-i+1]; | |
P.x = s-P.x+1; | |
p = 'P'; r = 'R'; | |
b = 'B'; q = 'Q'; | |
n = 'N'; | |
} | |
if( who(P,Point(-1,-1),1) == p ) return true; | |
if( who(P,Point(-1,1),1) == p ) return true; | |
ch = who(P,Point(-1,0),s); | |
if( ch==r || ch==q ) return true; | |
ch = who(P,Point(1,0),s); | |
if( ch==r || ch==q ) return true; | |
ch = who(P,Point(0,1),s); | |
if( ch==r || ch==q ) return true; | |
ch = who(P,Point(0,-1),s); | |
if( ch==r || ch==q ) return true; | |
ch = who(P,Point(1,1),s); | |
if( ch==b || ch==q ) return true; | |
ch = who(P,Point(1,-1),s); | |
if( ch==b || ch==q ) return true; | |
ch = who(P,Point(-1,1),s); | |
if( ch==b || ch==q ) return true; | |
ch = who(P,Point(-1,-1),s); | |
if( ch==b || ch==q ) return true; | |
if( who(P,Point(-1,-2),1) == n ) return true; | |
if( who(P,Point(-2,-1),1) == n ) return true; | |
if( who(P,Point(-2, 1),1) == n ) return true; | |
if( who(P,Point(-1, 2),1) == n ) return true; | |
if( who(P,Point( 1,-2),1) == n ) return true; | |
if( who(P,Point( 2,-1),1) == n ) return true; | |
if( who(P,Point( 2, 1),1) == n ) return true; | |
if( who(P,Point( 1, 2),1) == n ) return true; | |
return false; | |
} | |
// main function | |
int main( void ) | |
{ | |
Point king; | |
// input | |
while( input() ) { | |
while( getchar()!='\n' ); | |
printf( "Game #%d: ", gi++ ); | |
// solve | |
FOR( r, 1, s ) FOR( c, 1, s ) | |
if( cb[r][c] == 'K' ) | |
king = Point(r,c); | |
if( check(king) ) { | |
puts("white king is in check."); | |
continue; | |
} | |
FOR( r, 1, s ) FOR( c, 1, s ) | |
if( cb[r][c] == 'k' ) | |
king = Point(r,c); | |
if( check(king) ) { | |
puts("black king is in check."); | |
continue; | |
} | |
puts("no king is in check."); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment