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

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

  1. ACM YTU 《挑战编程》第一章 入门 Problem E: Graphical Editor

    Description Graphical editors such as Photoshop allow us to alter bit-mapped images in the same way ...

  2. Windows核心编程第一章.错误处理

    Windows核心编程第一章,错误处理. 一丶错误处理 1.核心编程学习总结 不管是做逆向,开始做开发.在Windows下.你都需要看一下核心编程这本书.这本书确实写得很好.所以自己在学习这本书的同时 ...

  3. .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来,首先,请允许我长吸一口气!真没想到一份来自28岁老程序员 ...

  4. net core体系-web应用程序-4asp.net core2.0 项目实战(CMS)-第一章 入门篇-开篇及总体规划

    .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划   原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来 ...

  5. Java 面向对象编程——第一章 初识Java

      第一章    初识Java 1.  什么是Java? Java是一种简单的.面向对象的.分布式的.解释的.安全的.可移植的.性能优异的多线程语言.它以其强安全性.平台无关性.硬件结构无关性.语言简 ...

  6. Java学习笔记 第一章 入门<转>

    第一章 JAVA入门 一.基础常识 1.软件开发 什么是软件? 软件:一系列按照特定顺序组织的计算机数据和指令的集合 系统软件:DOS,Windows,Linux 应用软件:扫雷.QQ.迅雷 什么是开 ...

  7. windows核心编程-第一章 对程序错误的处理

    第一章-对程序错误的处理 在开始介绍Microsoft Windows 的特性之前,必须首先了解 Wi n d o w s的各个函数是如何进行错误处理的. 当调用一个Wi n d o w s函数时,它 ...

  8. UNIX环境高级编程--第一章 UNIX基础知识

    第一章 UNIX基础知识 1.2 UNIX体系结构   从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境.我们将这种软件称为内核(kernel),因为 它相对较小,且 ...

  9. 读高性能JavaScript编程 第一章

    草草的看完第一章,虽然看的是译文也是感觉涨姿势了, 我来总结一下: 由于 大多数浏览器都是 single process 处理 ui updatas and js execute 于是产生问题: js ...

随机推荐

  1. strstr()

    char * __cdecl strstr ( const char * str1, const char * str2 ) { char *cp = (char *) str1; char *s1, ...

  2. WAF与IPS的区别总结

    谁是最佳选择? Web应用防护无疑是一个热门话题.由于技术的发展成熟和人们对便利性的期望越来越高,Web应用成为主流的业务系统载体.在Web上“安家”的关键业务系统中蕴藏的数据价值引起攻击者的青睐,网 ...

  3. C++_Eigen函数库用法笔记——Advanced Initialization

    The comma initializer a simple example  join and block initialize  join two row vectors together ini ...

  4. hibernate criteria中Restrictions的用法

    方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge ...

  5. Android Studio 设置不自动缩进匿名内部类

    Android Studio 会默认缩进匿名内部类代码,这让人感觉有些不大适应,可以使用下面的方法进行取消. 取消选中橙色框前的几个复选框即可.

  6. Usermod 命令详解 ------工作中修改shell时用 usermod -s /bin/csh home

     Usermod 命令详解 2012-09-11 11:01:36 标签:usermod 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.htt ...

  7. C语言异常处理和连接数据库

    #include <stdio.h> #include <setjmp.h> jmp_buf j; void Exception(void); double diva(doub ...

  8. C++编程思想重点笔记(上)

    C和C++指针的最重要的区别在于:C++是一种类型要求更强的语言.就void *而言,这一点表现得更加突出.C虽然不允许随便地把一个类型的指针指派给另一个类型,但允许通过void *来实现.例如: b ...

  9. Excel导入导出,生成和下载Excel报表、附件等操作--ASP.NET

    public class OutExcel { public static void OutExcel_bb(DataTable dt, string thepath, string temppath ...

  10. Android Activity模拟dialog

    Android项目中很多地方,都会弹出一个弹出框.类似于自己定义的alertDialog,比如微信的退出提示,但由于Dialog的限制,可能不能很完美的实现你的想要的功能,所有研究发现他们这种实现其实 ...