类型:简单模拟

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

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

原题:

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. c++ 函数指针应用,定义一个方法,传入两个参数和一个函数指针,并返回结果

    #include <iostream> #include <string> using namespace std; double add(double x, double y ...

  2. vc文件操作汇总—支持wince

    一.判断文件及文件夹是否存在 // 判断文件是否存在 BOOL IsFileExist(const CString& csFile) { DWORD dwAttrib = GetFileAtt ...

  3. 状态压缩dp 状压dp 详解

    说到状压dp,一般和二进制少不了关系(还常和博弈论结合起来考,这个坑我挖了还没填qwq),二进制是个好东西啊,所以二进制的各种运算是前置知识,不了解的话走下面链接进百度百科 https://baike ...

  4. 示例vue 的keep-alive缓存功能的实现

    本篇文章主要介绍了vue 的keep-alive缓存功能的实现,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下.如有不足之处,欢迎批评指正. Vue 实现组件信息的缓存 当我们 ...

  5. django开发之model篇-Field类型讲解

    今天介绍一下django开发中,定义模型时用到的相关字段类型和字段选项. 先说说常用的字段类型:1) AutoField: 自增字段类型,当自定义自增类型的id时,可以使用此类型:2) BigAuto ...

  6. uvm transaction modeling

    1.what is transaction? network transactions tcp/ip wifi 3g/4g bus transactions amba-ahb/apb/axi pci/ ...

  7. python爬虫基础12-selenium大全6/8-等待

    Selenium笔记(6)等待 本文集链接:https://www.jianshu.com/nb/25338984 简介 在selenium操作浏览器的过程中,每一次请求url,selenium都会等 ...

  8. CodeForces - 899E Segments Removal (优先队列 + 链表)

    给定一个序列,每次从序列中找一个长度最大的元素相同的片段,删除它. 如果长度相同,删除最靠左边的那个片段. 问,需要删几次. 用链表处理删除片段.对于删除之后两边又能连成一个片段的那种情况,用set记 ...

  9. cento命令之which、whereis、locate、find

    [which] 查看可执行文件的位置 语法: [root@localhost ~]# which 可执行文件名称 例如: [root@localhost ~]# which passwd /usr/b ...

  10. HTTPS的请求与响应

    HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法. HTTPS(Hypertext Transfer ...