烟大 Contest1024 - 《挑战编程》第一章:入门 Problem G: Check The Check(模拟国际象棋)

时间:2024-09-08 13:37:26

Problem G: Check The Check

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 10  Solved: 3
[Submit][Status][Web Board]

Description

Your task is to write a program that reads a chessboard configuration and identifies whether a king is under attack (in check). A king is in check if it is on square which can be taken by the opponent on his next move. White pieces will be represented by uppercase letters, and black pieces by lowercase letters. The white side will always be on the bottom of the board, with the black side always on the top. For those unfamiliar with chess, here are the movements of each piece: Pawn (p or P): can only move straight ahead, one square at a time. However, it takes pieces diagonally, and that is what concerns you in this problem. Knight (n or N) : has an L-shaped movement shown below. It is the only piece that can jump over other pieces. Bishop (b or B) : can move any number of squares diagonally, either forward or backward. Rook (r or R) : can move any number of squares vertically or horizontally, either forward or backward. Queen (q or Q) : can move any number of squares in any direction (diagonally, horizontally, or vertically) either forward or backward. King (k or K) : can move one square at a time in any direction (diagonally, horizontally, or vertically) either forward or backward. Movement examples are shown below, where ``*'' indicates the positions where the piece can capture another piece:
 Pawn          Rook          Bishop        Queen         King          Knight
........ ...*.... .......* ...*...* ........ ........
........ ...*.... *.....*. *..*..*. ........ ........
........ ...*.... .*...*.. .*.*.*.. ........ ..*.*...
........ ...*.... ..*.*... ..***... ..***... .*...*..
...p.... ***r**** ...b.... ***q**** ..*k*... ...n....
..*.*... ...*.... ..*.*... ..***... ..***... .*...*..
........ ...*.... .*...*.. .*.*.*.. ........ ..*.*...
........ ...*.... *.....*. *..*..*. ........ ........

Remember that the knight is the only piece that can jump over other pieces. The pawn movement will depend on its side. If it is a black pawn, it can only move one square diagonally down the board. If it is a white pawn, it can only move one square diagonally up the board. The example above is a black pawn, described by a lowercase ``p''. We use ``move" to indicate the squares where the pawn can capture another piece.

Input

There will be an arbitrary number of board configurations in the input, each consisting of eight lines of eight characters each. A ``.'' denotes an empty square, while upper- and lowercase letters represent the pieces as defined above. There will be no invalid characters and no configurations where both kings are in check. You must read until you find an empty board consisting only of ``.'' characters, which should not be processed. There will be an empty line between each pair of board configurations. All boards, except for the empty one, will contain exactly one white king and one black king.

Output

For each board configuration read you must output one of the following answers: Game #d: white king is in check. Game #d: black king is in check. Game #d: no king is in check. where d stands for the game number starting from 1.

Sample Input

..k.....
ppp.pppp
........
.R...B..
........
........
PPPPPPPP
K....... rnbqk.nr
ppp..ppp
....p...
...p....
.bPP....
.....N..
PP..PPPP
RNBQKB.R ........
........
........
........
........
........
........
........

Sample Output

Game #: black king is in check.
Game #: white king is in check.

HINT


  模拟题。模拟的是国际象棋。通过输入棋子的分布(8*8的棋盘上),判断下一步哪一方会被将军或者是平局。没什么技术含量,写一个switch语句,将所有种类棋子的情况写进去,然后依次检测每一个棋子。检测完白字就检测黑子。如果被将军就跳出输出结果。

  写了300多行,真是麻烦。

My code:

 #include <iostream>

 using namespace std;

 int main()
{
char q[][];
int count = ;
while(true){
//input
int i,j;
bool f=true;
for(i=;i<=;i++)
for(j=;j<=;j++){
cin>>q[i][j];
if(q[i][j]!='.')
f=false;
}
if(f) break; //如果是空棋盘,退出
//不是空棋盘,检测每一个棋子是否有将军的能力。 bool fw=false,fb=false; //判断白子和黑子谁能赢
int n;
int ii,jj;
//先检测白子
for(i=;i<=;i++){
for(j=;j<=;j++){
if(q[i][j]=='.' || ('a'<=q[i][j] && q[i][j]<='z') ) continue; //如果无子或者检测到黑子(小写),则跳过
switch(q[i][j]){
case 'P': //卒
if(q[i-][j-]=='k' || q[i-][j+]=='k')
fw=true;
break;
case 'R': //车 //左
n=j-;
while(--n){
if(q[i][n]=='k') {fw=true;break;}
else if(q[i][n]!='.') break;
}
//右
n=j+;
while(fw!=true && (++n)<= ){
if(q[i][n]=='k') {fw=true;break;}
else if(q[i][n]!='.') break;
}
//上
n=i-;
while(fw!=true && --n){
if(q[n][j]=='k') {fw=true;break;}
else if(q[n][j]!='.') break;
}
//下
n=i+;
while(fw!=true && (++n)<= ){
if(q[n][j]=='k') {fw=true;break;}
else if(q[n][j]!='.') break;
}
break;
case 'B': //象
//左上
ii=i-;
jj=j-;
while(ii>= && jj>=){
if(q[ii][jj]=='k') {fw=true;break;}
else if(q[ii][jj]!='.') break;
--ii,--jj;
}
//右上
ii=i-;
jj=j+;
while(!fw && ii>= && jj<=){
if(q[ii][jj]=='k') {fw=true;break;}
else if(q[ii][jj]!='.') break;
--ii,++jj;
}
//左下
ii=i+;
jj=j-;
while(!fw && ii<= && jj>=){
if(q[ii][jj]=='k') {fw=true;break;}
else if(q[ii][jj]!='.') break;
++ii,--jj;
}
//右下
ii=i+;
jj=j+;
while(!fw && ii<= && jj<=){
if(q[ii][jj]=='k') {fw=true;break;}
else if(q[ii][jj]!='.') break;
++ii,++jj;
}
break;
case 'Q': //后
//左
n=j-;
while(--n){
if(q[i][n]=='k') {fw=true;break;}
else if(q[i][n]!='.') break;
}
//右
n=j+;
while(fw!=true && (++n)<= ){
if(q[i][n]=='k') {fw=true;break;}
else if(q[i][n]!='.') break;
}
//上
n=i-;
while(fw!=true && --n){
if(q[n][j]=='k') {fw=true;break;}
else if(q[n][j]!='.') break;
}
//下
n=i+;
while(fw!=true && (++n)<= ){
if(q[n][j]=='k') {fw=true;break;}
else if(q[n][j]!='.') break;
}
//左上
ii=i-;
jj=j-;
while(ii>= && jj>=){
if(q[ii][jj]=='k') {fw=true;break;}
else if(q[ii][jj]!='.') break;
--ii,--jj;
}
//右上
ii=i-;
jj=j+;
while(!fw && ii>= && jj<=){
if(q[ii][jj]=='k') {fw=true;break;}
else if(q[ii][jj]!='.') break;
--ii,++jj;
}
//左下
ii=i+;
jj=j-;
while(!fw && ii<= && jj>=){
if(q[ii][jj]=='k') {fw=true;break;}
else if(q[ii][jj]!='.') break;
++ii,--jj;
}
//右下
ii=i+;
jj=j+;
while(!fw && ii<= && jj<=){
if(q[ii][jj]=='k') {fw=true;break;}
else if(q[ii][jj]!='.') break;
++ii,++jj;
}
break;
case 'K': //王
//上下左右
if(q[i-][j]=='k'){fw=true;break;}
else if(q[i+][j]=='k'){fw=true;break;}
else if(q[i][j-]=='k'){fw=true;break;}
else if(q[i][j+]=='k'){fw=true;break;}
//左上右上左下右下
else if(q[i-][j-]=='k'){fw=true;break;}
else if(q[i-][j+]=='k'){fw=true;break;}
else if(q[i+][j-]=='k'){fw=true;break;}
else if(q[i+][j+]=='k'){fw=true;break;}
break;
case 'N': //马
//转一圈检测,从正上偏左第一个开始
if(q[i-][j-]=='k'){fw=true;break;}
else if(q[i-][j+]=='k'){fw=true;break;}
else if(q[i-][j+]=='k'){fw=true;break;}
else if(q[i+][j+]=='k'){fw=true;break;} else if(q[i+][j+]=='k'){fw=true;break;}
else if(q[i+][j-]=='k'){fw=true;break;}
else if(q[i+][j-]=='k'){fw=true;break;}
else if(q[i-][j-]=='k'){fw=true;break;}
break;
case '.': //无子
break;
}
if(fw) break;
}
if(fw) break;
}
//检测黑子
for(i=;i<=;i++){
for(j=;j<=;j++){
if(q[i][j]=='.' || ('A'<=q[i][j] && q[i][j]<='Z') ) continue; //如果无子或者检测到白子(大写),则跳过
switch(q[i][j]){
case 'p': //卒
if(q[i-][j-]=='K' || q[i-][j+]=='K')
fb=true;
break;
case 'r': //车 //左
n=j-;
while(--n){
if(q[i][n]=='K') {fb=true;break;}
else if(q[i][n]!='.') break;
}
//右
n=j+;
while(fb!=true && (++n)<= ){
if(q[i][n]=='K') {fb=true;break;}
else if(q[i][n]!='.') break;
}
//上
n=i-;
while(fb!=true && --n){
if(q[n][j]=='K') {fb=true;break;}
else if(q[n][j]!='.') break;
}
//下
n=i+;
while(fb!=true && (++n)<= ){
if(q[n][j]=='K') {fb=true;break;}
else if(q[n][j]!='.') break;
}
break;
case 'b': //象
//左上
ii=i-;
jj=j-;
while(ii>= && jj>=){
if(q[ii][jj]=='K') {fb=true;break;}
else if(q[ii][jj]!='.') break;
--ii,--jj;
}
//右上
ii=i-;
jj=j+;
while(!fb && ii>= && jj<=){
if(q[ii][jj]=='K') {fb=true;break;}
else if(q[ii][jj]!='.') break;
--ii,++jj;
}
//左下
ii=i+;
jj=j-;
while(!fb && ii<= && jj>=){
if(q[ii][jj]=='K') {fb=true;break;}
else if(q[ii][jj]!='.') break;
++ii,--jj;
}
//右下
ii=i+;
jj=j+;
while(!fb && ii<= && jj<=){
if(q[ii][jj]=='K') {fb=true;break;}
else if(q[ii][jj]!='.') break;
++ii,++jj;
}
break;
case 'Q': //后
//左
n=j-;
while(--n){
if(q[i][n]=='K') {fb=true;break;}
else if(q[i][n]!='.') break;
}
//右
n=j+;
while(fb!=true && (++n)<= ){
if(q[i][n]=='K') {fb=true;break;}
else if(q[i][n]!='.') break;
}
//上
n=i-;
while(fb!=true && --n){
if(q[n][j]=='K') {fb=true;break;}
else if(q[n][j]!='.') break;
}
//下
n=i+;
while(fb!=true && (++n)<= ){
if(q[n][j]=='K') {fb=true;break;}
else if(q[n][j]!='.') break;
}
//左上
ii=i-;
jj=j-;
while(ii>= && jj>=){
if(q[ii][jj]=='K') {fb=true;break;}
else if(q[ii][jj]!='.') break;
--ii,--jj;
}
//右上
ii=i-;
jj=j+;
while(!fb && ii>= && jj<=){
if(q[ii][jj]=='K') {fb=true;break;}
else if(q[ii][jj]!='.') break;
--ii,++jj;
}
//左下
ii=i+;
jj=j-;
while(!fb && ii<= && jj>=){
if(q[ii][jj]=='K') {fb=true;break;}
else if(q[ii][jj]!='.') break;
++ii,--jj;
}
//右下
ii=i+;
jj=j+;
while(!fb && ii<= && jj<=){
if(q[ii][jj]=='K') {fb=true;break;}
else if(q[ii][jj]!='.') break;
++ii,++jj;
}
break;
case 'K': //王
//上下左右
if(q[i-][j]=='K'){fb=true;break;}
else if(q[i+][j]=='K'){fb=true;break;}
else if(q[i][j-]=='K'){fb=true;break;}
else if(q[i][j+]=='K'){fb=true;break;}
//左上右上左下右下
else if(q[i-][j-]=='K'){fb=true;break;}
else if(q[i-][j+]=='K'){fb=true;break;}
else if(q[i+][j-]=='K'){fb=true;break;}
else if(q[i+][j+]=='K'){fb=true;break;}
break;
case 'N': //马
//转一圈检测,从正上偏左第一个开始
if(q[i-][j-]=='k'){fb=true;break;}
else if(q[i-][j+]=='K'){fb=true;break;}
else if(q[i-][j+]=='K'){fb=true;break;}
else if(q[i+][j+]=='K'){fb=true;break;} else if(q[i+][j+]=='K'){fb=true;break;}
else if(q[i+][j-]=='K'){fb=true;break;}
else if(q[i+][j-]=='K'){fb=true;break;}
else if(q[i-][j-]=='K'){fb=true;break;}
break;
default:break;
}
if(fb) break;
}
if(fb) break;
} if(fw==true && fb==false)
cout<<"Game #"<<count++<<": "<<"black king is in check."<<endl;
else if(fb==true && fw==false)
cout<<"Game #"<<count++<<": "<<"white king is in check."<<endl;
else if(fw==false && fb==false)
cout<<"Game #"<<count++<<": "<<"no king is in check."<<endl;
else break;
}
return ;
}

Freecode : www.cnblogs.com/yym2013