Xiangqi
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1529   Accepted: 389

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


Situation 1



Situation 2

Source



题目大意:

                     给你一个象棋棋盘,其中只含有一个黑子的将,和n个红子的棋子,只能是帅,軍,炮,马,他们的位置均给出下一步该黑子走,问红子下一步可不可以直接将死 黑子


题目思路:

                    大模拟水题,首先判断黑子可不可以直接将死红子,然后在记录下黑子下一步可以走的位置,然后再判断走的位置是否安全,可以先判断四个方向有没有軍,如果第一个出现的不是軍然后继续搜看是否下一个出现的是否是炮,最后在判断马当然还要考虑马可不可以走!


AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

int n,x1,y1;
int fx[4][2]={1,0,-1,0,0,1,0,-1};   //上下左右四个方向
int hr[8][2]={1,2,1,-2,2,1,2,-1,-1,2,-1,-2,-2,1,-2,1};     //马走的八个方向
int ff[8][2]={1,1,1,-1,1,1,1,-1,-1,1,-1,-1,-1,1,-1,-1};    //分别对应马的八个方向上胖脚的方向
struct st{
    int x,y;
}bk[5];
char mp[15][15];

bool check1(int x,int y){     //判断是否将和帅面对面
   for(int i=x+1;i<=10;i++){
       if(mp[i][y]=='\0')continue;
       if(mp[i][y]!='G')return false;
       return true;
   }
   return false;
}

bool check(int x,int y){
    if(check1(x,y))return false;
    for(int i=0;i<4;i++){
        int h = x+fx[i][0];
        int l = y+fx[i][1];
        while(h>=1&&h<=10&&l>=1&&l<=9){
            if(mp[h][l]=='\0')
            {
                h+=fx[i][0];
                l+=fx[i][1];
                continue;
            }
            if(mp[h][l]=='R')return false;   //判軍
            int hh = h+fx[i][0];
            int ll = l+fx[i][1];
            while(hh>=1&&hh<=10&&ll>=1&&ll<=9){
               if(mp[hh][ll]=='\0')
               {
                hh+=fx[i][0];
                ll+=fx[i][1];
                continue;
                }
                if(mp[hh][ll]=='C')return false;  //判炮
                break;
            }
            break;

        }
    }

    for(int i=0;i<8;i++){
        int h = x+hr[i][0];
        int l = y+hr[i][1];
        int hh =x+ff[i][0];
        int ll = y+ff[i][1];
        if(h>=1&&h<=10&&l>=1&&l<=9&&mp[h][l]=='H'&&mp[hh][ll]=='\0')
           {
               return false;               //判马
           }
    }

    return true;
}

int main()
{
    while(~scanf("%d%d%d",&n,&x1,&y1)&&(n+x1+y1)){
        memset(mp,0,sizeof(mp));
       int cnt = 0;

       char ch[5];
       int a,b;
       for(int i=0;i<n;i++){
        scanf("%s%d%d",ch,&a,&b);
        mp[a][b]=ch[0];
       }
        for(int i=0;i<4;i++){
        int x = x1+fx[i][0];
        int y = y1+fx[i][1];
        if(x>=1&&x<=3&&y>=4&&y<=6){
            bk[cnt].x = x;
            bk[cnt++].y = y;
        }
       }

       if(check1(x1,y1))cout<<"NO"<<endl;
       else{
            int flag = 1;
            for(int i=0;i<cnt;i++){
                char cc = mp[bk[i].x][bk[i].y];
                if(cc!='\0')mp[bk[i].x][bk[i].y]='\0';
                if(check(bk[i].x,bk[i].y)){
                    flag=0;
                    cout<<"NO"<<endl;
                    break;
                }
               mp[bk[i].x][bk[i].y] = cc;
            }
            if(flag)cout<<"YES"<<endl;
       }

    }

    return 0;
}