类型:简单模拟

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

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

原题:

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

  1. The Problem
  2.  
  3. 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).
  4.  
  5. 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.
  6.  
  7. For those unfamiliar with chess, here are the movements of each piece:
  8.  
  9. 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).
  10. 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.
  11. Bishop (b or B): can move any number of squares diagonally (forward or backward).
  12. Rook (r or R): can move any number of squares vertically or horizontally (forward or backward).
  13. Queen (q or Q): can move any number of squares in any direction (diagonally, horizontally or vertically, forward or backward).
  14. King (k or K): can move one square at a time, in any direction (diagonally, horizontally or vertically, forward or backward).
  15.  
  16. Movements examples ('*' indicates where the piece can take another pieces):
  17.  
  18. Pawn
  19. ........
  20. ........
  21. ........
  22. ........
  23. ...p....
  24. ..*.*...
  25. ........
  26. ........
  27.  
  28. Rook
  29. ...*....
  30. ...*....
  31. ...*....
  32. ...*....
  33. ***r****
  34. ...*....
  35. ...*....
  36. ...*....
  37.  
  38. Bishop
  39. .......*
  40. *.....*.
  41. .*...*..
  42. ..*.*...
  43. ...b....
  44. ..*.*...
  45. .*...*..
  46. *.....*.
  47.  
  48. Queen
  49. ...*...*
  50. *..*..*.
  51. .*.*.*..
  52. ..***...
  53. ***q****
  54. ..***...
  55. .*.*.*..
  56. *..*..*.
  57.  
  58. King
  59. ........
  60. ........
  61. ........
  62. ..***...
  63. ..*k*...
  64. ..***...
  65. ........
  66. ........
  67.  
  68. Knight
  69. ........
  70. ........
  71. ..*.*...
  72. .*...*..
  73. ...n....
  74. .*...*..
  75. ..*.*...
  76. ........
  77.  
  78. 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).
  79. The Input
  80.  
  81. 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).
  82. The Output
  83.  
  84. For each board configuration read you must output one of the following answers:
  85.  
  86. Game #d: white king is in check.
  87. Game #d: black king is in check.
  88. Game #d: no king is in check.
  89.  
  90. Where d stands for the game number (starting from 1).
  91. Sample Input
  92.  
  93. ..k.....
  94. ppp.pppp
  95. ........
  96. .R...B..
  97. ........
  98. ........
  99. PPPPPPPP
  100. K.......
  101.  
  102. rnbqkbnr
  103. pppppppp
  104. ........
  105. ........
  106. ........
  107. ........
  108. PPPPPPPP
  109. RNBQKBNR
  110.  
  111. rnbqk.nr
  112. ppp..ppp
  113. ....p...
  114. ...p....
  115. .bPP....
  116. .....N..
  117. PP..PPPP
  118. RNBQKB.R
  119.  
  120. ........
  121. ........
  122. ........
  123. ........
  124. ........
  125. ........
  126. ........
  127. ........
  128.  
  129. Sample Output
  130.  
  131. Game #1: black king is in check.
  132. Game #2: no king is in check.
  133. Game #3: white king is in check.

代码:

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

  1. // 大量相同代码,用宏定义 少用复制。
  2. // 宏定义特点:写了两段发现除了个别参数不一样,其他完全一样,想复制。
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. char chessboard[][];
  7. char tmpboard[][];
  8. #define IN_BOARD(I,J) (0<=(I) && (I)<8 && 0<=(J) && (J)<8)
  9.  
  10. #define CK(A,B) if (IN_BOARD(A,B) && (tmpboard[A][B] == 'K')) return true;
  11. bool checkP(int i, int j) {
  12. CK(i+,j-);
  13. CK(i+,j+);
  14. return false;
  15. }
  16.  
  17. #define CK_LINE(A,B) \
  18. for (int k = ; IN_BOARD(A,B); k++) { \
  19. if(tmpboard[A][B] == 'K') return true; \
  20. else if (tmpboard[A][B] != '.') break; \
  21. }
  22. bool checkR(int i, int j) {
  23. CK_LINE(i+k,j);
  24. CK_LINE(i-k,j);
  25. CK_LINE(i,j+k);
  26. CK_LINE(i,j-k);
  27. return false;
  28. }
  29.  
  30. bool checkB(int i, int j) {
  31. CK_LINE(i+k,j+k);
  32. CK_LINE(i+k,j-k);
  33. CK_LINE(i-k,j+k);
  34. CK_LINE(i-k,j-k);
  35. return false;
  36. }
  37.  
  38. bool checkQ(int i, int j) {
  39. if (checkB(i,j)) return true;
  40. if (checkR(i,j)) return true;
  41. return false;
  42. }
  43.  
  44. bool checkK(int i, int j) {
  45. //检查k 是否将军K
  46. CK(i-,j-);
  47. CK(i-,j);
  48. CK(i-,j+);
  49.  
  50. CK(i,j-);
  51. CK(i,j);
  52. CK(i,j+);
  53.  
  54. CK(i+,j-);
  55. CK(i+,j);
  56. CK(i+,j+);
  57.  
  58. return false;
  59. }
  60.  
  61. bool checkN(int i, int j) {
  62. CK(i-,j-);
  63. CK(i-,j+);
  64. CK(i-,j-);
  65. CK(i-,j+);
  66.  
  67. CK(i+,j-);
  68. CK(i+,j+);
  69. CK(i+,j-);
  70. CK(i+,j+);
  71.  
  72. return false;
  73. }
  74.  
  75. // 检查 小写字母 是否将军 大写字母
  76. bool isInCheck(){
  77. for (int i = ; i < ; i++) {
  78. for (int j = ; j < ; j++) {
  79. if ('a' <= tmpboard[i][j] && tmpboard[i][j] <= 'z') {
  80. switch (tmpboard[i][j]) {
  81. case 'p': if(checkP(i,j))return true;break;
  82. case 'r': if(checkR(i,j))return true;break;
  83. case 'b': if(checkB(i,j))return true;break;
  84. case 'q': if(checkQ(i,j))return true;break;
  85. case 'k': if(checkK(i,j))return true;break;
  86. case 'n': if(checkN(i,j))return true;break;
  87. default: puts("Error low char");break;
  88. }
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94.  
  95. //读入棋盘,如果是空的返回false
  96. bool read() {
  97. for (int i = ; i < ; i++) {
  98. scanf("%s", chessboard[i]);
  99. }
  100. bool notEmpty = false;
  101. for (int i = ; i < ; i++) {
  102. for (int j = ; j < ; j++) {
  103. if (chessboard[i][j] != '.') notEmpty = true;
  104. }
  105. }
  106. return notEmpty;
  107. }
  108.  
  109. int main() {
  110. int cas = ;
  111. while (read())
  112. {
  113. printf("Game #%d: ", cas++);
  114.  
  115. for (int i = ; i < ; i++)
  116. {
  117. sprintf(tmpboard[i], "%s", chessboard[i]);
  118. }
  119. if (isInCheck())
  120. {
  121. puts("white king is in check.");
  122. continue;
  123. }
  124.  
  125. for (int i = ; i < ; i++)
  126. {
  127. sprintf(tmpboard[i], "%s", chessboard[-i]);
  128. for (int j = ; j < ; j++)
  129. {
  130. if ('a' <= tmpboard[i][j] && tmpboard[i][j] <= 'z')
  131. {
  132. tmpboard[i][j] = tmpboard[i][j] -'a'+'A';
  133. } else if ('A' <= tmpboard[i][j] && tmpboard[i][j] <= 'Z')
  134. {
  135. tmpboard[i][j] = tmpboard[i][j] - 'A' + 'a';
  136. }
  137. }
  138. }
  139. if (isInCheck())
  140. {
  141. puts("black king is in check.");
  142. continue;
  143. }
  144. puts("no king is in check.");
  145. }
  146. return ;
  147. }

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. 字节跳动后端开发实习生面试(Python)

    一面: 1.自我介绍. 2.介绍“工大小美”项目相关. 3.Python中的GIL(全局解释器锁),以及哪种情况下使用python的多线程性能有较大的提升. 4.项目中用到了SQLite数据库,如果有 ...

  2. C++输入密码不显示明文

    之前有遇到需求说输入密码不显示明文,但同时会有一些其他问题,暂时没做,如今经过尝试可以实现,但是得先知道要输入的是密码.主要利用的getch()函数的不回显特点.需要注意的是这个函数不是标准函数,而且 ...

  3. 【网络基础】【TCP/IP】私有IP地址段

    私有IP地址段 Class A:10.0.0.0    - 10.255.255.255 Class B:172.16.0.0  - 172.31.255.255 Class C:192.168.0. ...

  4. selenium+phantomjs爬取bilibili

    selenium+phantomjs爬取bilibili 首先我们要下载phantomjs 你可以到 http://phantomjs.org/download.html 这里去下载 下载完之后解压到 ...

  5. POJ - 3660 Cow Contest(传递闭包)

    题意: n个点,m条边. 若A 到 B的边存在,则证明 A 的排名一定在 B 前. 最后求所有点中,排名可以确定的点的个数. n <= 100, m <= 4500 刚开始还在想是不是拓扑 ...

  6. Linux操作系统启动流程

    一般来说,所有的操作系统的启动流程基本就是: 总的来说,linux系统启动流程可以简单总结为以下几步:1)开机BIOS自检,加载硬盘.2)读取MBR,进行MBR引导.3)grub引导菜单(Boot L ...

  7. dubbo doc入门文档

    dubbo document http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-protocol.html

  8. 光学字符识别OCR-5 文本切割

    经过前面文字定位得到单行的文本区域之后,我们就可以想办法将单行的文本切割为单个的字符了.因为第三步的模型是针对单个的字符建立的,因此这一步也是必须的. 均匀切割 基于方块汉字的假设,事实上最简单的切割 ...

  9. 用qemu+gdb tcp server+CDT调试linux内核启动-起步

    用qemu+gdb tcp server+CDT调试linux内核启动-起步 说明: 环境信息与 用virtualbox+模拟串口+CDT调试linux内核 TCP IP协议栈-起步 提到的一样,并且 ...

  10. 03-python进阶-爬虫入门-正则

    [urllib and urllib2] 这是两个python的网络模块 内置的 提供很好的网络访问的功能. #!coding:utf-8 import urllib2 res = urllib2.u ...