类型:简单模拟

大致题意:已知国际象棋行棋规则,给你一个局面,问是否将军?谁将谁的军?(保证不会同时将军)

思路:都以小写字母 测试 是否将 大写字母。 然后一个局面测两次(一次直接测,一次反转棋盘,同时大小写互换,测)

原题:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21173

The Problem

Your task is to write a program that reads a chess board configuration and answers if there's a king under attack (i.e. "in check"). A king is in check if it's in a square which is attacked by an oponnet's piece (i.e. it's in square which can be taken by an oponnet's piece in his next move).

White pieces will be represented by uppercase letters whereas black pieces will be represented by lowercase letters. White side will always be on the bottom of the board and black side will always be on the top of the board.

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. But it takes pieces diagonally (and that's what concerns to you in this problem).
Knight (n or N): have a special movement and it's the only piece that can jump over other pieces. The knight movement can be viewed as an "L". See the example bellow.
Bishop (b or B): can move any number of squares diagonally (forward or backward).
Rook (r or R): can move any number of squares vertically or horizontally (forward or backward).
Queen (q or Q): can move any number of squares in any direction (diagonally, horizontally or vertically, forward or backward).
King (k or K): can move one square at a time, in any direction (diagonally, horizontally or vertically, forward or backward). Movements examples ('*' indicates where the piece can take another pieces): Pawn
........
........
........
........
...p....
..*.*...
........
........ Rook
...*....
...*....
...*....
...*....
***r****
...*....
...*....
...*.... Bishop
.......*
*.....*.
.*...*..
..*.*...
...b....
..*.*...
.*...*..
*.....*. Queen
...*...*
*..*..*.
.*.*.*..
..***...
***q****
..***...
.*.*.*..
*..*..*. King
........
........
........
..***...
..*k*...
..***...
........
........ Knight
........
........
..*.*...
.*...*..
...n....
.*...*..
..*.*...
........ Remember that the knight is the only piece that can jumper over other pieces. The pawn movement will depend on its side. If it's a black pawn, it can only move one square diagonally down the board. If it's a white pawn, it can only move one square diagonally up the board. The example above is a black pawn as it's a lowercase p (we say "move" meaning the squares where the pawn can move to when it takes another piece).
The Input There will be an arbitrary number of board configurations on the input. Each board will consist of 8 lines of 8 characters each. A '.' character will represent an empty square. Upper and lower case letters (as defined above) will represent the pieces. There will be no invalid characters (i.e. pieces) and there won't be a configuration where both kings are in check. You must read until you find an empty board (i.e. a board that is formed only of '.' characters) which should not be processed. There will be an empty line between each pair of board configurations. In all boards (except the last one which is empty) will appear both the white king and the black king (one, and only one of each).
The 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....... rnbqkbnr
pppppppp
........
........
........
........
PPPPPPPP
RNBQKBNR rnbqk.nr
ppp..ppp
....p...
...p....
.bPP....
.....N..
PP..PPPP
RNBQKB.R ........
........
........
........
........
........
........
........ Sample Output Game #1: black king is in check.
Game #2: no king is in check.
Game #3: white king is in check.

代码:

(!仅通过样例测试,未提交!)

// 大量相同代码,用宏定义 少用复制。
// 宏定义特点:写了两段发现除了个别参数不一样,其他完全一样,想复制。
#include <cstdio>
#include <cstdlib>
#include <cstring>
char chessboard[][];
char tmpboard[][];
#define IN_BOARD(I,J) (0<=(I) && (I)<8 && 0<=(J) && (J)<8) #define CK(A,B) if (IN_BOARD(A,B) && (tmpboard[A][B] == 'K')) return true;
bool checkP(int i, int j) {
CK(i+,j-);
CK(i+,j+);
return false;
} #define CK_LINE(A,B) \
for (int k = ; IN_BOARD(A,B); k++) { \
if(tmpboard[A][B] == 'K') return true; \
else if (tmpboard[A][B] != '.') break; \
}
bool checkR(int i, int j) {
CK_LINE(i+k,j);
CK_LINE(i-k,j);
CK_LINE(i,j+k);
CK_LINE(i,j-k);
return false;
} bool checkB(int i, int j) {
CK_LINE(i+k,j+k);
CK_LINE(i+k,j-k);
CK_LINE(i-k,j+k);
CK_LINE(i-k,j-k);
return false;
} bool checkQ(int i, int j) {
if (checkB(i,j)) return true;
if (checkR(i,j)) return true;
return false;
} bool checkK(int i, int j) {
//检查k 是否将军K
CK(i-,j-);
CK(i-,j);
CK(i-,j+); CK(i,j-);
CK(i,j);
CK(i,j+); CK(i+,j-);
CK(i+,j);
CK(i+,j+); return false;
} bool checkN(int i, int j) {
CK(i-,j-);
CK(i-,j+);
CK(i-,j-);
CK(i-,j+); CK(i+,j-);
CK(i+,j+);
CK(i+,j-);
CK(i+,j+); return false;
} // 检查 小写字母 是否将军 大写字母
bool isInCheck(){
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
if ('a' <= tmpboard[i][j] && tmpboard[i][j] <= 'z') {
switch (tmpboard[i][j]) {
case 'p': if(checkP(i,j))return true;break;
case 'r': if(checkR(i,j))return true;break;
case 'b': if(checkB(i,j))return true;break;
case 'q': if(checkQ(i,j))return true;break;
case 'k': if(checkK(i,j))return true;break;
case 'n': if(checkN(i,j))return true;break;
default: puts("Error low char");break;
}
}
}
}
return false;
} //读入棋盘,如果是空的返回false
bool read() {
for (int i = ; i < ; i++) {
scanf("%s", chessboard[i]);
}
bool notEmpty = false;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
if (chessboard[i][j] != '.') notEmpty = true;
}
}
return notEmpty;
} int main() {
int cas = ;
while (read())
{
printf("Game #%d: ", cas++); for (int i = ; i < ; i++)
{
sprintf(tmpboard[i], "%s", chessboard[i]);
}
if (isInCheck())
{
puts("white king is in check.");
continue;
} for (int i = ; i < ; i++)
{
sprintf(tmpboard[i], "%s", chessboard[-i]);
for (int j = ; j < ; j++)
{
if ('a' <= tmpboard[i][j] && tmpboard[i][j] <= 'z')
{
tmpboard[i][j] = tmpboard[i][j] -'a'+'A';
} else if ('A' <= tmpboard[i][j] && tmpboard[i][j] <= 'Z')
{
tmpboard[i][j] = tmpboard[i][j] - 'A' + 'a';
}
}
}
if (isInCheck())
{
puts("black king is in check.");
continue;
}
puts("no king is in check.");
}
return ;
}

最新文章

  1. MiniProfiler(MiniProfiler.EF6监控调试MVC5和EF6的性能)
  2. Linux Top
  3. springmvc下上传文件
  4. 2016 - 1 -17 GCD学习总结
  5. storyBoard中切换应用启动的切入点方法
  6. vb6如何将MSHFlexGrid控件中的内容导出为Excel
  7. mysql绿色版在windows系统中的启动
  8. DailyNote
  9. UnicodeEncodeError: &#39;gbk&#39; codec can&#39;t encode character &#39;\xbb&#39; in position 26269: illegal multibyte sequence
  10. 【代码学习】PHP中GD库的使用
  11. mysql版本升级
  12. 学生管理系统(SSM简易版)总结
  13. C#4.5-4.7学习总结
  14. ssm框架中处理json格式的数据步骤
  15. HTML语义化
  16. CH #46A - 磁力块 - [分块]
  17. JavaBeansDataExchange could not instantiate result class
  18. HPU :字符串的统计
  19. Hdfs数据备份
  20. wazhu之agent功能详解

热门文章

  1. 【费用流】bzoj1834: [ZJOI2010]network 网络扩容
  2. Docker DockerFile文件指令 &amp; 构建
  3. CSS基础(一)
  4. 【php】php安全问题
  5. redis+PHP消息队列实现及应用
  6. Python模块(二)(序列化)
  7. Python学习笔记:time模块和datetime模块(时间和日期)
  8. Linux学习-X Server 配置文件解析与设定
  9. 【Linux】tcpdump命令详解
  10. STW Family