类型:简单模拟

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

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

原题:

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 ;
}

UVA - 10196:Check The Check的更多相关文章

  1. uva 10196 Check The Check

    题目:10196 - Check The Check 思路:水题..模拟 这个代码,前半部分是在数统机房上课的时候写的,挫了点,懒得改了. #include <cstdio> #inclu ...

  2. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem G: Check The Check(模拟国际象棋)

    Problem G: Check The Check Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 10  Solved: 3[Submit][Statu ...

  3. JAVA使用HttpClient时报错:Algorithm constraints check failed on signature algorithm: MD5withRSA

    今天使用httpClient.executeMethod时抛出异常:java.security.cert.CertPathValidatorException: Algorithm constrain ...

  4. Perl的特殊代码块:BEGIN、CHECK、INIT、END和UNITCHECK

    这是5个特殊的代码块.要理解这几个块,关键在于几个时间点: (1).程序编译期间 (2).程序执行期间 (3).程序执行结束但还未退出期间 BEGIN块 BEGIN块是在程序编译期间执行的,也就是上面 ...

  5. 项目启动报错:Redis health check failed

    最近是重新开发整个项目,在上线测试的时候发现这个问题. 项目环境:SpringBoot2.x+Consul+Redission+Maven 报错的信息如下: o.s.b.a.redis.RedisHe ...

  6. 单例模式的两种实现方式对比:DCL (double check idiom)双重检查 和 lazy initialization holder class(静态内部类)

    首先这两种方式都是延迟初始化机制,就是当要用到的时候再去初始化. 但是Effective Java书中说过:除非绝对必要,否则就不要这么做. 1. DCL (double checked lockin ...

  7. 转自虫师:性能测试的 Check List

    原文地址:http://www.cnblogs.com/jackei/archive/2006/03/24/357372.html 1. 开发人员是否提交了测试申请? 2. 测试对象是否已经明确? 3 ...

  8. 手机应用:非功能需求 Check List

    服务状态防止并发 网络保持:无线网络,GPRS 网络连接:https,手机助手代理 电量 屏幕保持防止休眠 下载重试机制 定时检查XML 限速下载,线程休眠 下载出错反馈机制 消息广播 状态栏通知 进 ...

  9. UVA 12950 : Even Obsession(最短路Dijkstra)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. 初涉manacher

    一直没有打过……那么今天来找几道题打一打吧 manacher有什么用 字符串的题有一类是专门关于“回文”的.通常来说,这类问题要么和一些dp结合在一起:要么是考察对于manacher(或其他如回文自动 ...

  2. 02 Django框架基础(APP的创建访问)

    一.创建项目 1.命令:django-admin startproject sitename 2.IDLE环境:本质上都是执行上述命令 常用命令: python manage.py runserver ...

  3. Voyager的数据库操作与Bread Builder,解决国内打开网速超级慢的问题

    Products表的创建: Bread Builder 伟大的XX封了谷哥,所以有关网站实在是打不开,正准备放弃的时候,突然发现问题了,对就是这个网站ajax.googleapis.com,由于调用的 ...

  4. windows server 服务器 环境配置

    自动备份 xcopy d:\web\zhiku\*.* d:\bak\web\zhiku\%date:~,4%%date:~5,2%%date:~8,2%\ /S /I

  5. 【android】android对位图文件的支持

    Android 支持以下三种格式的位图文件:.png(首选)..jpg(可接受)..gif(不建议).

  6. Python头脑风暴1

    发个致富脑洞:我就在想本人虽然单身,但本人恋爱经历很多,追女生技术十足,女朋友漂亮又贤惠.如果本人开个平台帮人诚心介绍女朋友,男女成男女朋友经男方同意我收2.5万(IT界平均月收入的1.5倍不到),双 ...

  7. LeetCode(232) Implement Queue using Stacks

    题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...

  8. C#语言入门

    1.基础知识 2.数据类型 3.控制语句 4.

  9. Linux学习-Tarball 的管理与建议

    使用原始码管理软件所需要的基础软件 从原始码的说明我们晓得要制作一个 binary program 需要很多咚咚的呢!这包括底下这些基础的软件: gcc 或 cc 等 C 语言编译程序 (compil ...

  10. Linux学习-延伸正则表达式

    grep 默认仅支持基础正则表达式,如果要使用延伸型正则 表达式,你可以使用 grep -E , 不过更建议直接使用 egrep !直接区分指令比较好记忆!其 实 egrep 与 grep -E 是类 ...