Xiangqi
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1791   Accepted: 444

Description

Xiangqi is one of the most popular two-player board games in China. The game represents a battle between two armies with the goal of capturing the enemy’s “general” piece. In this problem, you are given a situation of later stage in the game. Besides, the red side has already “delivered a check”. Your work is to check whether the situation is “checkmate”.

Now we introduce some basic rules of Xiangqi. Xiangqi is played on a 10×9 board and the pieces are placed on the intersections (points). The top left point is (1,1) and the bottom right point is (10,9). There are two groups of pieces marked by black or red Chinese characters, belonging to the two players separately. During the game, each player in turn moves one piece from the point it occupies to another point. No two pieces can occupy the same point at the same time. A piece can be moved onto a point occupied by an enemy piece, in which case the enemy piece is "captured" and removed from the board. When the general is in danger of being captured by the enemy player on the enemy player’s next move, the enemy player is said to have "delivered a check". If the general's player can make no move to prevent the general's capture by next enemy move, the situation is called “checkmate”.

We only use 4 kinds of pieces introducing as follows:

  • General: the generals can move and capture one point either vertically or horizontally and cannot leave the “palace” unless the situation called “flying general” (see the right figure). “Flying general” means that one general can “fly” across the board to capture the enemy general if they stand on the same line without intervening pieces.
  • Chariot: the chariots can move and capture vertically and horizontally by any distance, but may not jump over intervening pieces
  • Cannon: the cannons move like the chariots, horizontally and vertically, but capture by jumping exactly one piece (whether it is friendly or enemy) over to its target.
  • Horse: the horses have 8 kinds of jumps to move and capture shown in the left figure. However, if there is any pieces lying on a point away from the horse horizontally or vertically it cannot move or capture in that direction (see the left figure), which is called “hobbling the horse’s leg”.


Hobbling the horse’s leg

Now you are given a situation only containing a black general, a red general and several red chariots, cannons and horses, and the red side has delivered a check. Now it turns to black side’s move. Your job is to determine that whether this situation is “checkmate”.

Input

The input contains no more than 40 test cases. For each test case, the first line contains three integers representing the number of red pieces N (2<=N<=7) and the position of the black general. The following n lines contain details of N red pieces. For each line, there are a char and two integers representing the type and position of the piece (type char ‘G’ for general, ‘R’ for chariot, ‘H’ for horse and ‘C’ for cannon). We guarantee that the situation is legal and the red side has delivered the check.
There is a blank line between two test cases. The input ends by 0 0 0.

Output

For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’.

Sample Input

2 1 4
G 10 5
R 6 43 1 5
H 4 5
G 10 5
C 7 50 0 0

Sample Output

YES
NO

Hint

In the first situation, the black general is checked by chariot and “flying general”. In the second situation, the black general can move to (1, 4) or (1, 6) to stop check. See the figure above.


Situation 1


Situation 2

Source

Fuzhou 2011

Regionals 2011 >> Asia - Fuzhou

题链接:POJ4001 HDU4121 UVA1589 UVALive5829 Xiangqi

问题简述:

模拟题,判断中国象棋棋盘上的局面是否被将军死。

问题分析:

按照题意进行模拟即可,需要程序的细节,尽可能把程序写简洁。

程序说明:

  有个坑,如果开始就已经对将了,红方不赢,程序需要处理。

题记:(略)

参考链接:(略)

AC的C++语言程序如下:

/* POJ4001 HDU4121 UVA1589 UVALive5829 Xiangqi */#include <iostream>
#include <string.h>
#include <stdio.h>using namespace std;const int drow[] = {1, 1, -1, -1,   /*R*/1, 0, -1, 0,   /*H*/1, 1, -1, -1, 2, 2, -2, -2,   1, 1, -1, -1, 1, 1, -1, -1};
const int dcol[] = {1, -1, 1, -1,   /*R*/0, 1, 0, -1,   /*H*/2, -2, 2, -2, 1, -1, 1, -1,   1, -1, 1, -1, 1, -1, 1, -1};const int N = 10;
const int M = 9;
char board[N + 1][M + 1];int grow, gcol;
bool flag;// 循环判断:将(General),如果开始就对将了,则红方不赢
bool general( int row, int col, int start, int end )
{for ( int i = start ; i <= end ; i++ ) {int nextrow = row+drow[i];int nextcol = col+dcol[i];while(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M) {if(board[nextrow][nextcol] == 'G')return true;if(board[nextrow][nextcol] != ' ')break;nextrow += drow[i];nextcol += dcol[i];}}return false;
}// 循环判断:车(Chariot, Rook),将(General)
bool chariot( int row, int col, int start, int end )
{for ( int i = start ; i <= end ; i++ ) {int nextrow = row+drow[i];int nextcol = col+dcol[i];while(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M) {if(board[nextrow][nextcol] == 'R' || board[nextrow][nextcol] == 'G')return true;if(board[nextrow][nextcol] != ' ')break;nextrow += drow[i];nextcol += dcol[i];}}return false;
}// 循环判断:炮(Cannon)
bool cannon( int row, int col, int start, int end )
{for ( int i = start ; i <= end ; i++ ) {int nextrow = row+drow[i];int nextcol = col+dcol[i];// 找炮台while(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M) {if(board[nextrow][nextcol] != ' ')break;nextrow += drow[i];nextcol += dcol[i];}// 循环判断nextrow += drow[i];nextcol += dcol[i];while(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M) {if(board[nextrow][nextcol] == 'C')return true;if(board[nextrow][nextcol] != ' ')break;nextrow += drow[i];nextcol += dcol[i];}}return false;
}// 马(Horse)
bool horse(int row, int col, int start, int end)
{for(int i = start; i <= end; i++) {int nextrow = row + drow[i];int nextcol = col + dcol[i];if(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M)if(board[nextrow][nextcol] == 'H' && board[row +drow[i + 8]][col + dcol[i + 8]] == ' ')return true;}return false;
}void deal()
{for(int i = 4; i <= 7; i++) {int nextrow = grow + drow[i];int nextcol = gcol + dcol[i];if(nextrow < 1 || nextrow > 3 || nextcol < 4 || nextcol > 6)continue;// 车(Chariot, Rook),将(General)flag = chariot(nextrow, nextcol, 4, 7);if(flag)continue;// 炮(Cannon)flag = cannon(nextrow, nextcol, 4, 7);if(flag)continue;// 马(Horse)flag = horse(nextrow, nextcol, 8, 15);if(flag)continue;flag = false;return;}flag = true;
}inline char mygetchar()
{char ret;while ((ret = getchar()) == ' ' || ret == '\t' || ret == '\n');return ret;
}int main()
{int n;while(scanf("%d%d%d", &n, &grow, &gcol) == 3 && (n || grow || gcol)) {memset(board, ' ', sizeof(board));char piece;int row, col;while(n--) {piece = mygetchar();scanf("%d%d", &row, &col);board[row][col] = piece;}flag = false;if(!general(grow, gcol, 4, 4)) {flag = false;deal();}printf("%s\n", flag ? "YES" : "NO");}return 0;
}

POJ4001 HDU4121 UVA1589 UVALive5829 Xiangqi【模拟+回溯】相关推荐

  1. HDU 4121 Xiangqi 模拟题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=4121 首先对标题赞一个,非要叫 "Xiangqi" 而不是 "中国象棋&q ...

  2. 【习题4-1 Uva1589】Xiangqi

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 车是可以被吃掉的... 注意这个情况. 其他的模拟即可. [代码] #include <bits/stdc++.h> u ...

  3. UVA10196 Check The Check【模拟+回溯】

    Your task is to write a program that reads a chess board configuration and answers if there's a king ...

  4. UVA 1589 Xiangqi——模拟

    模拟题,我的做法是先让黑将分别上下左右移动,对每一种情况  先判断是否能被马将死(列举8种情况),然后判断是否能被 将 车 炮 将死(以黑将为中心十字展开,判断是否有 将 车 炮) 注意: 1.开局两 ...

  5. HDU 4121 Xiangqi --模拟

    题意: 给一个象棋局势,问黑棋是否死棋了,黑棋只有一个将,红棋可能有2~7个棋,分别可能是车,马,炮以及帅. 解法: 开始写法是对每个棋子,都处理处他能吃的地方,赋为-1,然后判断将能不能走到非-1的 ...

  6. 第三十四题 UVA1589 象棋 Xiangqi 说来惭愧

    PDF 题意翻译 [输入] 输入数据≤40组,对于每组数据,第一行有三个数:第一个数代表红方棋数 N(2≤N≤7),第二三个数代表黑将的坐标 接下来N行每行一个字符两个数,代表每个红子的详细信息,字符 ...

  7. UVA10284 POJ2512 Chessboard in FEN【国际象棋】

    In the FEN (Forsyth-Edwards Notation), a chessboard is described as follows: • The Board-Content is ...

  8. ICPC程序设计题解书籍系列之一:刘汝佳:《算法竞赛入门经典》(第2版)

    题是书中的题,部分解法参照了书中的解法,不少解法都做了简化和改进. 做程序,就要努力做到自己的程序是最好的! 第3章 数组和字符串(例题) POJ1488 UVA272 UVALive5381 TEX ...

  9. 回溯法(其实是递归)

    关于回溯算法,你该了解这些!别看回溯法很难,但回溯法就是暴力解法​https://mp.weixin.qq.com/s?__biz=MzUxNjY5NTYxNA==&mid=224748523 ...

最新文章

  1. pymatgen读/写各种文件
  2. android点击视频使用固定应用打开,Android 使用intent打开手机自带应用播放视频,音频,文档,还有打开应用市场...
  3. 解读电感和电容在交流电路中的作用
  4. 多线程 java 实例_Java多线程实例学习
  5. Java 的单例模式
  6. 使用JUnitParams简化Parameterized tests
  7. UGUI学习笔记之渲染顺序
  8. java并发编程之HappenBefore
  9. ~~试除法判定质数 (附模板题)
  10. 读源代码学Asp.net Ajax(一)
  11. 贵州大数据崛起背后的阿里云力量
  12. java js 打开摄像头_js调用网络摄像头
  13. 利用rancher轻松构建pass平台
  14. 单片机上的8位数码管显示数字12345678
  15. 第十五周博客作业西北师范大学|李晓婷
  16. win10安装navisworks失败,怎么强力卸载删除注册表并重新安装
  17. 图片传输(APP端将图片传至服务器端存储)
  18. 小红书赞助和广告有什么区别,小红书赞助和广告一样吗?
  19. 网络工程师每日练习(5)
  20. java如何将网页表格导出为excel

热门文章

  1. python给定dna等分成两个序列_分析DNA序列中的串联重复序列
  2. iOS应用支持IPV6及阿里云相关配置
  3. Windows7安装PowerShell5.1方法(Flutter新版本需要)
  4. Java中的List、Set、Map
  5. coco数据集大小分类_【数据集】LVIS:大规模细粒度词汇级标记数据集 ,出自FAIR ,连披萨里的菠萝粒都能完整标注...
  6. mac mysql语句_Mac 下MySQL使用group by 语句报错解决方法
  7. id 怎么获取jira 评论_【JIRA】如何快速地批量查找各迭代的SprintID
  8. 2 snippets vue 修改配置_教你发布vue+.netCore项目到服务器
  9. safari only css hack,css hack同时针对Safari和Chrome进行攻击
  10. android 手机工具箱,Android超级工具箱,你的手机可能缺一个!