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 figure above). “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 figure below), which is called “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 4
 
3 1 5
H 4 5
G 10 5
C 7 5
 
0 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.

 
 /* G是主将,R是车,H是马,C是炮,考虑蹩马腿,将帅不能照面 , 题目暗示下一步走棋的是黑方
* validMoveH 函数中的参数direct的值代表的马移动的方位,如下图
*
* (1)* (2)*
*
* (3)* (4)*
*
* (5)* (6)*
*
* (7)* (8)*
*/ #include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
enum TRAVERSE_TYPE{ X,Y }; // 区间遍历枚举类型
enum MOVE_TYPE{ UP,DOWN,LEFT,RIGHT }; // 移动方向枚举类型
struct Pos{
Pos(int nX,int nY):x(nX),y(nY) { }
Pos() = default;
int x;
int y;
};
vector<Pos> R,H,C; // 储存棋盘上红⽅存在的⼦
int map[][]; // 模拟棋盘
char mmap[][]; // 存储红方除主将外落⼦的类型
Pos general_b,general_r; // ⿊⽅、红⽅主将位置
int others(int begin,int end,int index,TRAVERSE_TYPE type); // 返回某个区间段上落⼦的个数
vector<MOVE_TYPE> validMoveG(); // 返回⼀个⿊将可以⾛的⽅向的stack
bool gensfight(Pos t); // 两主将照⾯的情况
bool RfightG(); // 红⽅⻋⾯对⿊⽅将情况
bool HfightG(); // 红⽅⻢⾯对⿊⽅将情况
bool CfightG(); // 红⽅炮⾯对⿊⽅将情况
bool RUN(int x,int y); // 将所有情况跑⼀遍
bool validMoveH(int x,int y,int direct); // 判断⻢⾛的情况是否合法
void erasing(int x,int y); // ⿊主将⾛过的地⽅如果有落⼦会被抹掉 int main()
{
int n;
while(cin >> n >> general_b.x >> general_b.y
&& n && general_b.x && general_b.y){ // 主循环
R.clear(); H.clear(); C.clear();
memset(map,,sizeof(map));memset(mmap,,sizeof(mmap));
map[general_b.x][general_b.y] = ; // 先将黑主将录入棋盘
while(n--)
{
char type; int posX; int posY;
cin >> type >> posX >> posY;
map[posX][posY] = ; // 在每次询问中,不管输入什么类型落子,先录入棋盘
if(type == 'G'){ // 类型为红方主将
general_r.x = posX;
general_r.y = posY;
}else if(type == 'R'){ // 类型为红方车
R.emplace_back(Pos(posX,posY));
mmap[posX][posY] = 'R'; // mmap数组的作用可在之后的erasing函数中清楚
}else if(type == 'H'){ // 类型为红方马
H.emplace_back(Pos(posX,posY));
mmap[posX][posY] = 'H';
}else{ // 类型为红方炮
C.emplace_back(Pos(posX,posY));
mmap[posX][posY] = 'C';
}
}
if(gensfight({general_b.x,general_b.y})){ // 分支1:直接出现主将对⾯情况
cout <<"NO" << endl; continue;
}
/* 分支2 (不出现主将直接面对)*/
auto op = validMoveG(); // 获得⿊⽅主将可⾛⽅向的vector数组
bool yes; // yes若为true表示红方已将死黑方
for(auto &e : op){ // 只要⿊⽅主将有⼀种⾛法不会被将死就输出NO
yes = false;
// general_b 与 general_r 分别代表黑方主将与红方主将,见17行声明
if(e == MOVE_TYPE::UP && RUN(general_b.x-,general_b.y)) yes = true;
else if(e == MOVE_TYPE::DOWN && RUN(general_b.x+,general_b.y)) yes = true;
else if(e == MOVE_TYPE::LEFT && RUN(general_b.x,general_b.y-)) yes = true;
else if(e == MOVE_TYPE::RIGHT && RUN(general_b.x,general_b.y+)) yes = true;
if(!yes) break; // 对于黑主将每一种走法,只要此走法不会被将死,那么黑方目前未被将死
}
yes ? printf("YES\n") : printf("NO\n");
}
return ;
} bool gensfight(Pos t){
if(t.y != general_r.y) return false; // 如果两主将不在⼀条直线上直接否定
return others(general_r.x,t.x,t.y,TRAVERSE_TYPE::X) == ;
}
bool RfightG(Pos t){for(auto &e : R){ // 将所有⻋遍历,看看它们上下左右是否直接与⿊⽅主将照⾯
bool equalsX = ( e.x == t.x ), equalsY = ( e.y == t.y ); //直接照⾯的前提是它们⾄少在同⼀条直线上
if(!(equalsX || equalsY)) continue; // 如果此⻋不满⾜条件考虑下⼀个⻋
if(equalsX){
if(others(e.y,t.y,e.x,TRAVERSE_TYPE::Y) == ) return true;
}else{
if(others(e.x,t.x,e.y,TRAVERSE_TYPE::X) == ) return true;
}
}
return false;
}
bool HfightG(Pos t){ // 将所有⻢遍历,看看它们是否直接踩到⿊⽅主将
for(auto &e : H){ // 只要有⼀匹⻢移动合法且直接踩到⿊主将则返回true
// validMoveH检查马的移动是否合法(包括蹩马脚、棋盘越界)
if(e.y- == t.y && e.x- == t.x && validMoveH(e.x,e.y,)) return true;
if(e.y+ == t.y && e.x- == t.x && validMoveH(e.x,e.y,)) return true;
if(e.y- == t.y && e.x- == t.x && validMoveH(e.x,e.y,)) return true;
if(e.y+ == t.y && e.x- == t.x && validMoveH(e.x,e.y,)) return true;
if(e.y- == t.y && e.x+ == t.x && validMoveH(e.x,e.y,)) return true;
if(e.y+ == t.y && e.x+ == t.x && validMoveH(e.x,e.y,)) return true;
if(e.y- == t.y && e.x+ == t.x && validMoveH(e.x,e.y,)) return true;
if(e.y+ == t.y && e.x+ == t.x && validMoveH(e.x,e.y,)) return true;
}
return false; // 程序控制流到达这里当且仅当所有存在的马均不能杀死黑主将
}
bool CfightG(Pos t){ // 将所有炮遍历,看看它们是否直接轰到⿊⽅主将
for(auto &e : H){
bool equalsX = ( e.x == t.x ), equalsY = ( e.y == t.y ); // 炮打到⿊将的前提是它们⾄少在同⼀条直线上
if(!(equalsX || equalsY)) continue; // 如果此炮不满⾜条件考虑下⼀个炮
if(equalsX){
int count = others(e.y,t.y,e.x,TRAVERSE_TYPE::Y);
if(count != ) continue; // 考虑炮杀死黑主将的前提是它们之间仅有一个落子
else return true; // 只要有一个炮能做到即返回true
}else{
int count = others(e.x,t.x,e.y,TRAVERSE_TYPE::X);
if(count == || count > ) continue;else return true;
}
}
return false;
}
int others(int begin,int end,int index,TRAVERSE_TYPE type){
// 该函数计算同一直线上两落子之间落子的个数,index是指在遍历中固定不动的下标
if(begin > end) swap(begin,end);
int cnt = ;
if(type == TRAVERSE_TYPE::X){
for(int i = begin + ;i < end;i ++) if(map[i][index]) cnt++;
}else{ // (type == TRAVERSE_TYPE::Y)
for(int i = begin + ;i < end;i ++) if(map[index][i]) cnt++;
}
return cnt;
}
vector<MOVE_TYPE> validMoveG(){
// 该函数将黑主将可走的方向放进一个临时vector并返回
vector<MOVE_TYPE> temp;
if(general_b.y != ) temp.push_back(MOVE_TYPE::LEFT);
if(general_b.y != ) temp.push_back(MOVE_TYPE::RIGHT);
if(general_b.x != ) temp.push_back(MOVE_TYPE::UP);
if(general_b.x != ) temp.push_back(MOVE_TYPE::DOWN);
return temp;
}
inline
bool RUN(int x,int y){
// 该函数负责检查黑主将在每一种走法下是否会被一种存在类型的红方落子杀死
erasing(x,y); // 由于黑主将的下一个落点完全可能有一个红方棋子挡住,那么它可以直接吃掉也就是删除此落子
Pos t(x,y);
return RfightG(t) || HfightG(t) || CfightG(t) || gensfight(t);
}
bool validMoveH(int x,int y,int direct){
if(direct==){ // 这些map都代表会蹩马脚的位置,必须保证此位置没有落子
if(!(y- >= && x- >= && !map[x-][y])) return false;
}else if(direct==){
if(!(y+ <= && x- >= && !map[x-][y])) return false;
}else if(direct==){
if(!(y- >= && x- >= && !map[x][y-])) return false;
}else if(direct==){
if(!(y+ <= && x- >= && !map[x][y+])) return false;
}else if(direct==){
if(!(y- >= && x+ <= && !map[x][y-])) return false;
}else if(direct==){
if(!(y+ <= && x+ <= && !map[x][y+])) return false;
}else if(direct==){
if(!(y- >= && x+ <= && !map[x+][y])) return false;
}else if(direct==){
if(!(y+ <= && x+ <= && !map[x+][y])) return false;
}
return true;
}
void erasing(int x,int y){
// 检查棋盘上(x,y)处落子的类型,如果存在即将它从它的集合中删除
if(mmap[x][y]=='R'){
for(vector<Pos>::const_iterator it = R.begin();it != R.end();it
++)if(it->x==x && it->y==y) R.erase(it);
}else if(mmap[x][y]=='H'){
for(vector<Pos>::const_iterator it = H.begin();it != H.end();it
++)
if(it->x==x && it->y==y) H.erase(it);
}else if(mmap[x][y]=='C'){
for(vector<Pos>::const_iterator it = C.begin();it != C.end();it
++)
if(it->x==x && it->y==y) C.erase(it);
}
}

点击"+"查看代码

                              

[算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释的更多相关文章

  1. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  2. [刷题]算法竞赛入门经典 3-12/UVa11809

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...

  3. [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...

  4. [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...

  5. [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...

  6. [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...

  7. 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth

    A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...

  8. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  9. 算法竞赛入门经典 LA 4329(树状数组)

    题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...

随机推荐

  1. SQLServer删除登录帐户

    删除登陆账户注意事项 不能删除正在登录的登录名. 也不能删除拥有任何安全对象.服务器级对象或 SQL Server 代理作业的登录名. 可以删除数据库用户映射到的登录名,但是这会创建孤立用户. 有关详 ...

  2. Raneto中文搜索支持

    背景 因业务部门需要在线软件使用说明文档,但我们资源不足,故我想找一个开源的知识库,发现 Raneto不错,决定使用. 官方文档相当清晰,部署完成,发布一些文章,启动项目,交由业务同事测试使用,于是我 ...

  3. 【转】C# 定时器事件(设置时间间隔,间歇性执行某一函数,控制台程序)

    using System.Timers;定时器事件代码 static void Main(string[] args) { Method(); #region 定时器事件 Timer aTimer = ...

  4. AOP 还在配置吗改用打标签模式吧!

    为什么我喜欢打标签来配置AOP 1. 配置多很混乱,代码里面很难分辨出来哪些是AOP容器(比如属性注入) 2. 对于代码生成器生成的代码里面还需要手动加到配置里面 3. 连java spring现在都 ...

  5. HTTP 1.1状态代码及其含义说明

    100  Continue  初始的请求已经接受,客户应当继续发送请求的其余部分.(HTTP 1.1新)   101  Switching Protocols  服务器将遵从客户的请求转换到另外一种协 ...

  6. Google Chrome即将开始警告—停止支持Flash Player

    Adobe 计划在 2020 年让 Flash Player 彻底退休,整个科技行业都在为这个关键时刻做准备,包括浏览器开发机构,Google 作为最主要的一员,试图尽可能顺利地完成 Flash Pl ...

  7. Tomcat配置实例

    转自:https://www.cnblogs.com/kismetv/p/7228274.html 目录 一.一个server.xml配置实例 二.server.xml文档的元素分类和整体结构 1.整 ...

  8. Java代理模式之Cglib代理

    Cglib代理,也叫做子类代理.在内存中构建一个子类对象从而实现对目标对象功能的扩展. CGLIB包的底层是通过使用一个小而快的字节码处理框架ASM,来转换字节码并生成新的类.不鼓励直接使用ASM,因 ...

  9. hdu-1054(二分图最大匹配)

    题意:给你一个图,图里有墙壁和空地,空地可以放置一台机关枪,机关枪可以朝着四个方向发射,子弹不能穿透墙壁,但是射程无限,机关枪会被损坏如果被另一台机关枪的子弹打到,问你最多能放置多少台机关枪: 解题思 ...

  10. 内置函数(sorted、map、enumerate、filter、reduce)

    1.sorted() 语法: sorted(iterable, cmp=None, key=None, reverse=False) 把iterable中的items进行排序之后,返回一个新的列表,原 ...