UVA - 10196:Check The Check
类型:简单模拟
大致题意:已知国际象棋行棋规则,给你一个局面,问是否将军?谁将谁的军?(保证不会同时将军)
思路:都以小写字母 测试 是否将 大写字母。 然后一个局面测两次(一次直接测,一次反转棋盘,同时大小写互换,测)
原题:
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的更多相关文章
- uva 10196 Check The Check
题目:10196 - Check The Check 思路:水题..模拟 这个代码,前半部分是在数统机房上课的时候写的,挫了点,懒得改了. #include <cstdio> #inclu ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem G: Check The Check(模拟国际象棋)
Problem G: Check The Check Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 10 Solved: 3[Submit][Statu ...
- JAVA使用HttpClient时报错:Algorithm constraints check failed on signature algorithm: MD5withRSA
今天使用httpClient.executeMethod时抛出异常:java.security.cert.CertPathValidatorException: Algorithm constrain ...
- Perl的特殊代码块:BEGIN、CHECK、INIT、END和UNITCHECK
这是5个特殊的代码块.要理解这几个块,关键在于几个时间点: (1).程序编译期间 (2).程序执行期间 (3).程序执行结束但还未退出期间 BEGIN块 BEGIN块是在程序编译期间执行的,也就是上面 ...
- 项目启动报错:Redis health check failed
最近是重新开发整个项目,在上线测试的时候发现这个问题. 项目环境:SpringBoot2.x+Consul+Redission+Maven 报错的信息如下: o.s.b.a.redis.RedisHe ...
- 单例模式的两种实现方式对比:DCL (double check idiom)双重检查 和 lazy initialization holder class(静态内部类)
首先这两种方式都是延迟初始化机制,就是当要用到的时候再去初始化. 但是Effective Java书中说过:除非绝对必要,否则就不要这么做. 1. DCL (double checked lockin ...
- 转自虫师:性能测试的 Check List
原文地址:http://www.cnblogs.com/jackei/archive/2006/03/24/357372.html 1. 开发人员是否提交了测试申请? 2. 测试对象是否已经明确? 3 ...
- 手机应用:非功能需求 Check List
服务状态防止并发 网络保持:无线网络,GPRS 网络连接:https,手机助手代理 电量 屏幕保持防止休眠 下载重试机制 定时检查XML 限速下载,线程休眠 下载出错反馈机制 消息广播 状态栏通知 进 ...
- UVA 12950 : Even Obsession(最短路Dijkstra)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- 初涉网络流[EK&dinic]
主要还是板子 Edmonds-Karp 从S开始bfs,直到找到一条到达T的路径后将该路径增广,并重复这一过程. 在处理过程中,为了应对“找到的一条路径把其他路径堵塞”的情况,采用了建反向弧的方式来实 ...
- Ajax请求出现406的原因
一般出现406错误有两种可能: 1.如果后缀是html是不能响应json数据的.需要修改后缀名. 在做伪静态化过程中,以.html结尾的后缀,做post请求时,不能响应json格式,这是spring官 ...
- crm项目之整体内容(一)
一.项目背景 YW公司是一家电池供应商,目前由于公司内部的需要,需要做一个CRM项目,需要每一个不同角色的员工登陆系统后处理自己的事情.其流程大致如下: 其项目包括三部分内容: 1.权限分配组件(rb ...
- hdu-2544 最短路(最短路)
Time limit1000 ms Memory limit32768 kB 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到 ...
- HBase0.94.2-cdh4.2.0需求评估测试报告1.0之一
hbase是bigtable的开源山寨版本.是建立的hdfs之上,提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.它介于nosql和RDBMS之间,仅能通过主键(row key)和主键的r ...
- UML结构与解析——BUAA OO第四单元作业总结
UML与解析架构 UML是什么 统一建模语言(英语:Unified Modeling Language,缩写 UML)是非专利的第三代建模和规约语言.UML是一种开放的方法,用于说明.可视化.构建和编 ...
- HDU 2852 KiKi's K-Number 主席树
题意: 要求维护一个数据结构,支持下面三种操作: \(0 \, e\):插入一个值为\(e\)的元素 \(1 \, e\):删除一个值为\(e\)的元素 \(2 \, a \, k\):查询比\(a\ ...
- CornerStone使用教程(配置SVN,HTTP及svn简单使用)
1.SVN配置 假设你公司svn地址为:svn://192.168.1.111/svn/ios,用户名:svnserver,密码:123456 1:填写主机地址 2:如果你的主机地址中有端口号,如为1 ...
- 令人惊叹的Visual Studio Code插件
vscode是一款开源且优秀的编辑器,接下来让我吐血推荐一下我工作使用过的令人惊叹的Visual Studio Code插件. 代码编辑插件 vscode-color-highlight ------ ...
- C++ STL 的初步认知
学无止境!!! 尊重他人劳动,尊重出处:http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html 我已经做了4年的MFC ...