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. 批处理:echo的用法

    批处理:echo的用法 若要用 echo 命令显示一条命令,可用下述语法:  echo [message] 参数 ON|OFF   指定是否允许命令的回显.若要显示当前的 ECHO 的设置,可使用不带 ...

  2. Eigenvectors and eigenvalues

    http://setosa.io/ev/eigenvectors-and-eigenvalues/ Explained Visually Tweet  By Victor Powell and Lew ...

  3. Python socket编程之五:更新分时图

    f1.py # -*- coding: utf-8 -*- import socket import struct import sqlalchemy import pandas ########## ...

  4. NC反弹CMDSHELL提权总结

    Server-U等都不可以用的情况下.   一般都可思考用此方法不过这种方法, 只要对方装了防火墙, 或是屏蔽掉了除常用的那几个端口外的所有端口…   那么这种方法也失效了…. 1:通过shell将上 ...

  5. js通过alert查看对象或数组内容

    var arr=new Array("Saab","Volvo","BMW"); for(i in arr ){ alert(i); //获 ...

  6. 安装 RPM 包或者安装源码包

    安装 RPM 包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下. ...

  7. PQ格式化虚拟机硬盘如何生效

    用pq格式化虚拟机硬盘后,安装时,总是从dhcp的网卡启动,没有从硬盘启动 但是用ghost是可以拷贝镜像文件的 这就是说,硬盘有了,但是没有将硬盘"激活",没有将硬盘设为acti ...

  8. win7 电脑能上网,但是下面的图标显示红叉的解决方法

    win7 电脑能上网,但是下面的图标显示红叉的解决方法 任务管理器里面结束explorer.exe进程,然后,点击文件,新建任务,输入explorer回车看看

  9. 超详细cordova环境配置(windows)及实例

    摘要: 最近闲来无事就把以前做的cordova项目整理了下,发现网上很少有详细完整的配置教程,所以自己就总结了下分享给大家. 项目地址:https://github.com/baixuexiyang/ ...

  10. Android自定义遮罩层设计

    在做网页设计时,前端设计人员会经常用到基于JS开发的遮罩层,并且背景半透明.这样的效果怎么样在Android上实现呢?这个实现并不困难,先来上效果图: <ignore_js_op> 201 ...