HDU 4121 Xiangqi

Xiangqi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6175 Accepted Submission(s): 1487

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.

第一次写博客,写的巨丑。一道模拟题,之前WA了好久,因为没有考虑到在一开始就发生飞将的情况,听到小伙伴讲完也是惊呆了。剩下的部分,就将每个棋子的规则对应着写。看帅向上下左右四个方向有没有能够不被吃掉的就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 15
using namespace std;
char mp[maxn][maxn];
int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int main(){
     int n, a, b;
     while(cin>>n>>b>>a && (n + a + b)){
          bool flag = false;
          memset(mp, '.', sizeof(mp));
          for(int i = 1; i <= n; i++){
               int x, y;
               char c;
               cin>>c>>y>>x;
               mp[x][y] = c;
          }
          for(int i = 1; i <= 10; i++){//首先判断是否会在一开始就出现飞将的情况
               if(i == b)
                    continue;
               if(mp[a][b] == '.')
                    continue;
               if(mp[a][b] == 'G'){
                    cout<<"NO"<<endl;
                    flag = true;
                    break;
               }
          }
          if(flag)
               continue;
          int k;
          int cnt = 0;
          for(k = 0; k < 4; k++){//向四个方向看能不能被吃掉
               flag = false;       //初始时都当作不能被吃掉
               int x = a + d[k][0];
               int y = b + d[k][1];
               if(x < 4 || x > 6 || y < 1 || y > 3){//帅出宫殿,不讨论
                    cnt++;
                    continue;
               }
               int t = 0;          //与帅x相同,在同一条直线上的初始个数
               for(int i = y + 1; i <= 10; i++){//从帅的位置往前面直线找
                    if(t > 1)      //如果t的数量大于1, 那么没有任何一个棋子可以吃掉帅,退出讨论
                         break;
                    if(mp[x][i] == 'R' && t == 0){//如果遇到的是车,且之前没有任何棋子
                         flag = true;             //帅会被吃掉,进行下一个方向的讨论
                         break;
                    }
                    else if(mp[x][i] == 'R' && t == 1)//如果遇到的是车,且之前有棋子了,那么车没有作用,累加t
                         t++;
                    else if(mp[x][i] == 'H') //如果遇到的是马,没有作用,直接累加t
                         t++;
                    else if(mp[x][i] == 'C' && t == 0) //如果遇到的是炮,前面没有棋子,没有作用,累加t
                         t++;
                    else if(mp[x][i] == 'C' && t == 1){//如果遇到的是炮,前面有一个棋子,可以打到帅,进行下一个方向的讨论
                         flag = true;
                         break;
                    }
                    else if(mp[x][i] == 'G' && t == 0){//如果遇到的是对方老帅,中间没有棋子,飞将,进行下一个方向的讨论
                         flag = true;
                         break;
                    }
               }
               if(flag){//前面出现被吃掉的情况,直接进行下一个方向的讨论
                    cnt++;
                    continue;
               }
               t = 0;
               for(int i = y - 1; i >= 1; i--){//从帅的位置往后面直线找
                    if(t > 1)      //如果t的数量大于1, 那么没有任何一个棋子可以吃掉帅,退出讨论
                         break;
                    if(mp[x][i] == 'R' && t == 0){//如果遇到的是车,且之前没有任何棋子
                         flag = true;             //帅会被吃掉,进行下一个方向的讨论
                         break;
                    }
                    else if(mp[x][i] == 'R' && t == 1)//如果遇到的是车,且之前有棋子了,那么车没有作用,累加t
                         t++;
                    else if(mp[x][i] == 'H') //如果遇到的是马,没有作用,直接累加t
                         t++;
                    else if(mp[x][i] == 'C' && t == 0) //如果遇到的是炮,前面没有棋子,没有作用,累加t
                         t++;
                    else if(mp[x][i] == 'C' && t == 1){//如果遇到的是炮,前面有一个棋子,可以打到帅,进行下一个方向的讨论
                         flag = true;
                         break;
                    }
                    else if(mp[x][i] == 'G' && t == 0){//如果遇到的是对方老帅,中间没有棋子,飞将,进行下一个方向的讨论
                         flag = true;
                         break;
                    }
               }
               if(flag){//前面出现被吃掉的情况,直接进行下一个方向的讨论
                    cnt++;
                    continue;
               }
               t = 0;    //t重新置零,判断与帅y相同,在同一条直线上的情况
               for(int i = x - 1; i >= 1; i--){//讨论方式与前面相同,但是不会出现飞将的情况
                    if(i == x)
                         continue;
                    if(t > 1)
                         break;
                    if(mp[i][y] == 'R' && t == 0){
                         flag = true;
                         break;
                    }
                    else if(mp[i][y] == 'R' && t == 1)
                         t++;
                    else if(mp[i][y] == 'H')
                         t++;
                    else if(mp[i][y] == 'C' && t == 0)
                         t++;
                    else if(mp[i][y] == 'C' && t == 1){
                         flag = true;
                         break;
                    }
               }
               if(flag){//前面出现被吃掉的情况,直接进行下一个方向的讨论
                    cnt++;
                    continue;
               }
               t = 0;    //t重新置零,判断与帅y相同,在同一条直线上的情况
               for(int i = x + 1; i <= 10; i++){//讨论方式与前面相同,但是不会出现飞将的情况
                    if(i == x)
                         continue;
                    if(t > 1)
                         break;
                    if(mp[i][y] == 'R' && t == 0){
                         flag = true;
                         break;
                    }
                    else if(mp[i][y] == 'R' && t == 1)
                         t++;
                    else if(mp[i][y] == 'H')
                         t++;
                    else if(mp[i][y] == 'C' && t == 0)
                         t++;
                    else if(mp[i][y] == 'C' && t == 1){
                         flag = true;
                         break;
                    }
               }
               if(flag){//前面出现被吃掉的情况,直接进行下一个方向的讨论
                    cnt++;
                    continue;
               }
               //对帅周围八个方向是否出现马,以及马是否别腿进行判断
               if(   ((y - 2) > 0 && mp[x-1][y-2] == 'H' && mp[x-1][y-1] == '.')
                  || ((y - 2) > 0 && mp[x+1][y-2] == 'H' && mp[x+1][y-1] == '.')
                  || ((y - 1) > 0 && mp[x-2][y-1] == 'H' && mp[x-1][y-1] == '.')
                  || ((y - 1) > 0 && mp[x+2][y-1] == 'H' && mp[x+1][y-1] == '.')
                  || (mp[x-2][y+1] == 'H' && mp[x-1][y+1] == '.')
                  || (mp[x+2][y+1] == 'H' && mp[x+1][y+1] == '.')
                  || (mp[x-1][y+2] == 'H' && mp[x-1][y+1] == '.')
                  || (mp[x+1][y+2] == 'H' && mp[x+1][y+1] == '.')
                  ){
                       flag = true;//任意出现被吃掉的情况,进行下一个方向的判断
                       cnt++;
                       continue;
                  }
          }
          if(cnt >= 4)
               cout<<"YES"<<endl;
          else
               cout<<"NO"<<endl;
     }
     return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值