【模拟专题】HDU-4121[弱校联萌第一场]

A. Xiangqi

Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d      Java class name: Main
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了很多次,主要原因是没有考虑好下面几点:

1、将军和车,炮,马的杀点应包括己方棋子的占点

2、车的占点不为杀点


下面是代码:

#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#include "math.h"
#include "memory.h"

using namespace std;
struct P{
    int x,y;
};

int x[4]={1,-1,0,0},y[4]={0,0,1,-1};//下上右左
int dx[8]={2,2,-2,-2,1,1,-1,-1},dy[8]={1,-1,1,-1,-2,2,2,-2};
P g,r[10],h[10],c[10],bg;//将车马炮
int n;

int main()
{
    while(1){
        cin>>n>>bg.x>>bg.y;
        if(!n&&!bg.x&&!bg.y) return 0;
        int matrix1[11][10];
        int matrix2[11][10];
        memset(matrix1,0,sizeof matrix1);
        memset(matrix2,0,sizeof matrix2);
        int gg=0,rr=0,hh=0,cc=0;
        for(int i=0;i<n;i+=1){
            getchar();
            char C;
            cin>>C;
            switch(C){
                case 'G':cin>>g.x>>g.y;matrix1[g.x][g.y]=-1;break;
                case 'R':cin>>r[rr].x>>r[rr].y;rr++;matrix1[r[rr-1].x][r[rr-1].y]=-1;break;
                case 'H':cin>>h[hh].x>>h[hh].y;hh++;matrix1[h[hh-1].x][h[hh-1].y]=-1;break;
                case 'C':cin>>c[cc].x>>c[cc].y;cc++;matrix1[c[cc-1].x][c[cc-1].y]=-1;break;
            }
        }//读入部分

        for(int i=g.x-1;i==g.x-1||(i>=1&&matrix1[i+1][g.y]!=-1);i-=1){
            matrix2[i][g.y]=1;
        }//标记将的杀点
        for(int i=0;i<rr;i+=1){
            for(int j=r[i].x+1;j==r[i].x+1||(j<=10&&matrix1[j-1][r[i].y]!=-1);j+=1) matrix2[j][r[i].y]=1;
            for(int j=r[i].x-1;j==r[i].x-1||(j>=1&&matrix1[j+1][r[i].y]!=-1);j-=1) matrix2[j][r[i].y]=1;
            for(int j=r[i].y+1;j==r[i].y+1||(j<=9&&matrix1[r[i].x][j-1]!=-1);j+=1) matrix2[r[i].x][j]=1;
            for(int j=r[i].y-1;j==r[i].y-1||(j>=1&&matrix1[r[i].x][j+1]!=-1);j-=1) matrix2[r[i].x][j]=1;
        }//标记车的杀点,分四个方向,为了避免将占点标记为杀点都从占点附近的点开始扫描,第一个判断是因为不加的话无法进入循环
        for(int i=0;i<hh;i+=1){
            for(int j=0;j<4;j+=1){
                if(h[i].x+x[j]>=1&&h[i].x+x[j]<=10&&h[i].y+y[j]>=1&&h[i].y+y[j]<=9&&matrix1[h[i].x+x[j]][h[i].y+y[j]]!=-1){
                    matrix2[h[i].x+dx[2*j]][h[i].y+dy[2*j]]=1;
                    matrix2[h[i].x+dx[2*j+1]][h[i].y+dy[2*j+1]]=1;
                }
            }
        }//标记马的杀点
        for(int i=0;i<cc;i+=1){
            int j=c[i].x-1;
            for(;j>=1&&matrix1[j][c[i].y]!=-1;j-=1);
            if(j)
                for(int k=j-1;k==j-1||(k>=1&&matrix1[k+1][c[i].y]!=-1);k-=1) matrix2[k][c[i].y]=1;
            j=c[i].x+1;
            for(;j<=10&&matrix1[j][c[i].y]!=-1;j+=1);
            if(j<=10)
                for(int k=j+1;k==j+1||(k<=10&&matrix1[k][c[i].y]!=-1);k+=1) matrix2[k][c[i].y]=1;
            j=c[i].y+1;
            for(;j<=9&&matrix1[c[i].x][j]!=-1;j+=1);
            if(j<=9)
                for(int k=j+1;k==j+1||(k<=9&&matrix1[c[i].x][k]!=-1);k+=1) matrix2[c[i].x][k]=1;
            j=c[i].y-1;
            for(;j>=1&&matrix1[c[i].x][j]!=-1;j-=1);
            if(j)
                for(int k=j-1;k==j-1||(k>=1&&matrix1[c[i].x][k]!=-1);k-=1) matrix2[c[i].x][k]=1;
        }//标记炮的杀点 分四个方向

        bool flag=true;
        for(int i=0;i<4;i+=1){
            if(bg.x+x[i]<=3&&bg.x+x[i]>=1&&bg.y+y[i]>=4&&bg.y+y[i]<=6&&matrix2[bg.x+x[i]][bg.y+y[i]]!=1){
                flag=false;
            }
        }
        if(flag) cout<<"YES\n";
        else cout<<"NO\n";
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值