[算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释

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”.

[算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释

We only use 4 kinds of pieces introducing as follows:
[算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释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.
[算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释Chariot: the chariots can move and capture vertically and horizontally by any distance, but may not jump over intervening pieces
[算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释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.
[算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释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”.

[算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释

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

[算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释
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);
}
}

点击"+"查看代码

                              

上一篇:【6】JAVA---地址App小软件(QueryPanel.class)(表现层)


下一篇:用smack+openfire做即时通讯