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. Database Password Hashes

    SQL Server 2000:- SELECT password from master.dbo.sysxlogins where name=’sa’ 0×010034767D5C0CFA5FDCA ...

  2. SqlServer Link 链接服务器的使用

    --创建linked exec sp_addlinkedserver 'a','','SQLNCLI','your ipaddress' --登陆linked exec sp_addlinkedsrv ...

  3. linux 的终端字体色和背景色的修改方法(一)

    更改Linux系统终端的颜色主题 随着Linux系统在服务器端的崛起,Linux也在慢慢进军个人桌面系统领域.如果在使用Linux系统的终端时,对其颜色主题不是很满意,该怎么修改颜色的主题呢?今天笔者 ...

  4. 百科编辑器ueditor应用笔记

    最近项目上要用到文本编辑器,选了百科开源的ueditor,使用过程中虽然有些问题,但是一个个都解决了,记录如下: 开发的项目环境是vs2012:.net4.0: 1:百度js编辑器,编辑器加载到项目中 ...

  5. Chrome中的Device模块调式响应性设计

    Chrome中的Device模块调式响应性设计 阅读目录 启用Device模块 Device模块设置介绍 自定义预设介绍 查看media queries 触发触摸事件 回到顶部 启用Device模块 ...

  6. bzoj1816 扑克牌

    Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1套 ...

  7. 专注docker安全:Security Scanning

    导读 Docker毫无疑问是近期运维同学们的热点话题,Docker安全也由此倍受重视,Docker Security Scanning 是一款Docker镜像扫描的安全工具,目前已经在Docker C ...

  8. UISerachBar / UISearchDisplayController

    1. UISerachBar 继承与UIView, 包含uitextfield, 并且实现了uitextfielddelegate代理的主要内容 含有取消按钮, 默认不显示 2. UISerachDi ...

  9. javascript return false 详解

    在大多数情况下,为事件处理函数返回false,可以防止默认的事件行为.例如,默认情况下点击一个<a>元素,页面会跳转到该元素href属性指定的页. Return False 就相当于终止符 ...

  10. CH round #55 Streaming #6

    T^T Saffah大神照样刷我这样诚心诚意想做一套NOIP模拟题的蒟蒻. 第一题 九九归一 好diao的名字... 题意就是给定一队$n,q$,求在模$n$意义下一个数$x$自乘的循环节长度. 当$ ...