我是個對電腦科學有興趣的學生,我會貼上我的學習歷程及生活心情,也請大大們多多指教。 :)

2015年5月10日 星期日

[UVa] 10196 - Check The Check

題目網址: http://goo.gl/Ffghil

題意:
(from luckycat)


解法: 直接做囉。

TAG: ad hoc

注意:

程式碼:
/**
* 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;
}

沒有留言:

張貼留言

任何意見都樂意傾聽