Problem G: Check The Check
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 10 Solved: 3
[Submit][Status][Web Board]
Description
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
Output
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