2015
04-16

# Xiangqi

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

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.

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.

2 1 4
G 10 5
R 6 4

3 1 5
H 4 5
G 10 5
C 7 5

0 0 0

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.


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>

#define pb push_back
#define cl(a) memset(a,0,sizeof(a))

using namespace std;

struct zb
{
int x;
int y;
};

const int fy[4]={-1,0,1,0};
const int fx[4]={0,-1,0,1};
const int hy[8]={-2,-2,-1,1,2,2,1,-1};
const int hx[8]={1,-1,-2,-2,-1,1,2,2};

vector<zb>c,h,r;
zb g;
int map[13][13];

void init()
{
cl(map);
c.clear();
h.clear();
r.clear();
}

int lie_pd(int y1,int y2,int x0,int t)
{
int i,z;
if (y1>y2)
{
int tmp=y1;
y1=y2;
y2=tmp;
}
z=0;
for (i=y1+1;i<y2;i++)
if (map[x0][i]) z++;
if (z==t) return 1;
return 0;
}

int hang_pd(int x1,int x2,int y0,int t)
{
int i,z;
if (x1>x2)
{
int tmp=x1;
x1=x2;
x2=tmp;
}
z=0;
for (i=x1+1;i<x2;i++)
if (map[i][y0]) z++;
if (z==t) return 1;
return 0;
}

int pd_general(int x0,int y0)
{
int i;
if (g.y==y0)
{
for (i=x0+1;i<g.x;i++)
if (map[i][y0]) return 1;
return 0;
}
return 1;
}

int pd_chariot(int x0,int y0)
{
int i,j;
for (j=0;j<r.size();j++)
{
if (r[j].x==x0&&r[j].y==y0) continue;
else if (r[j].x==x0)
{
if (lie_pd(r[j].y,y0,x0,0)) return 0;
}
else if (r[j].y==y0)
{
if (hang_pd(r[j].x,x0,y0,0)) return 0;
}
}
return 1;
}

int pd_connon(int x0,int y0)
{
int i,j;
for (j=0;j<c.size();j++)
{
if (c[j].x==x0)
{
if (lie_pd(c[j].y,y0,x0,1)) return 0;
}
if (c[j].y==y0)
{
if (hang_pd(c[j].x,x0,y0,1)) return 0;
}
}
return 1;
}

int pd_horse(int x0,int y0)
{
int i,j,x11,y11,x1,y1;
for (j=0;j<h.size();j++)
{
for (i=0;i<8;i++)
{
x1=h[j].x+hx[i];
y1=h[j].y+hy[i];
x11=h[j].x+fx[i/2];
y11=h[j].y+fy[i/2];
if (x1==x0&&y1==y0&&!map[x11][y11]) return 0;
}
}
return 1;
}

int pd(int x0,int y0)
{
return pd_horse(x0,y0)*pd_chariot(x0,y0)*pd_connon(x0,y0)*pd_general(x0,y0);
}

int main()
{
int n,i,x0,y0;
while (cin>>n)
{
init();
cin>>x0>>y0;
if (!n&&!x0&&!y0) break;
for (i=1;i<=n;i++)
{
char ch;
zb t;
cin>>ch;
cin>>t.x>>t.y;
if (ch=='G') g=t;
if (ch=='H') h.pb(t);
if (ch=='C') c.pb(t);
if (ch=='R') r.pb(t);
map[t.x][t.y]=1;
}
for (i=0;i<4;i++)
{
int x1,y1;
x1=x0+fx[i];
y1=y0+fy[i];
if ((x1>=1&&x1<=3)&&(y1>=4&&y1<=6))
{
if (pd(x1,y1))
{
cout<<"NO"<<endl;
break;
}
}
}
if (i==4) cout<<"YES"<<endl;
}
return 0;
}


1. 思路二可以用一个长度为k的队列来实现，入队后判断下队尾元素的next指针是否为空，若为空，则出队指针即为所求。

2. 学算法中的数据结构学到一定程度会乐此不疲的，比如其中的2－3树，类似的红黑树，我甚至可以自己写个逻辑文件系统结构来。