类型:简单模拟

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

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

原题:

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++后台开发应该读的书

    转载自http://www.cnblogs.com/balloonwj/articles/9094905.html 作者 左雪菲 根据我的经验来谈一谈,先介绍一下我的情况,坐标上海,后台开发(也带团队 ...

  2. NOIP模拟赛 czy的后宫

    [题目描述] czy要妥善安排他的后宫,他想在机房摆一群妹子,一共有n个位置排成一排,每个位置可以摆妹子也可以不摆妹子.有些类型妹子如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看了.假定每种 ...

  3. Java AES加密解密工具 -- GUI 、在线传输文件

    原理 对于任意长度的明文,AES首先对其进行分组,每组的长度为128位.分组之后将分别对每个128位的明文分组进行加密. 对于每个128位长度的明文分组的加密过程如下:     (1)将128位AES ...

  4. Mybatis 循环 foreach, 批量操作

    mapper.java: int modifySortOfGoods(@Param("idlist") List<String> goodsIds, @Param(&q ...

  5. 公用表表达式CTE简单递归使用-简单树形结构

    1.建表脚本 CREATE TABLE [dbo].[tb_tree]( ,) NOT NULL, [ParentId] [int] NULL, ) NULL, CONSTRAINT [PK_tb_t ...

  6. hdu 6354

    Problem Description Edward is a worker for Aluminum Cyclic Machinery. His work is operating mechanic ...

  7. 使用sprunge粘贴文字

    在irc里面请教的时候,需要输出很多文本,irc禁止输入多行文字. 使用sprunge可以返回一个网址,省去复制粘贴的麻烦. 1> 简单使用: command | curl -F "s ...

  8. flask-博客文章

    提交和显示博客文章 文章模型 class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key= ...

  9. BZOJ 5336: [TJOI2018]party

    状压最长公共子序列的DP数组,一维最多K(15)个数,且相邻两个数的差不超过1,2^15种状态,预处理转移 #include<cstdio> #include<algorithm&g ...

  10. JAVA 基础--开发环境Sublime Text 3 搭建

    方法一  打开Sublime Text 3,依次点击Preference, Browse Packages,在打开的窗口中双击User文件夹,新建文件JavaC.sublime-build,用记事本打 ...