ACM/ICPC Fuzhou 2011 && HDU 4121 下象棋 (超详细思路 全局判断)

 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.

 判断是否死局,对逻辑判断和全局变量的应用有着较高要求,函数Check用于检验当黑方将军位于(x,y)是否能存活,遍历黑方将所可能到达的每一位置(细节:若该位置有敌方棋子,则暂时将其“吃掉”,在下一次Cheak()之前恢复),一旦有一次能存活,则flag变为0,输出NO\n,若均无法存活,输出YES\n。开辟map二维数组,用于记录地图状态,0为空,1为红方将军,2为车,3为炮,4为马。hmove二维数组用于模拟马的移动,hfeet二维数组用于模拟马脚位置,gmove二维数组用于模拟黑方将军移动。
 以下为实现代码,较为详细,方便新手察看理解。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int map[11][10];
int hmove[8][2]={2,1,1,2,-1,2,-2,1,-2,-1,-1,-2,1,-2,2,-1};
int hfeet[8][2]={1,0,0,1, 0,1,-1,0,-1, 0, 0,-1,0,-1,1, 0};
int gmove[4][2]={1,0,0,1,-1,0,0,-1};
int gx,gy;
int Cheak(int x ,int y){
    int hx,hy,fx,fy;
    for(int i=1;i<=10;i++){
        for(int j=1;j<=9;j++){
            if(map[i][j]==1)//G
            {
                if(j==y){
                    int f=1;
                    for(int k=x+1;k<=i-1;k++){
                        if(map[k][y]!=0) f=0;
                        //
                    }
                    if(f!=0) {
                        //printf("x=%d y=%d处被将军击杀\n",x,y);
                        return 1;
                    }
                }
            }
            else if(map[i][j]==2){//car
                int count;
                if(i==x ){
                    if(y<=j){
                        count=0;
                        for(int k=y+1;k<=j-1;k++){
                            if(map[i][k]!=0) {
                                count++;

                            }
                        }
                        if(count ==0){
                            //printf("x=%d y=%d处被che击杀\n",x,y);
                            return 1;

                        }
                    }
                    else if(y>=j){
                        count=0;
                        for(int k=j+1;k<=y-1;k++){
                            if(map[i][k]!=0) {
                                count++;
                            }
                        }
                        if(count==0){
                            //printf("x=%d y=%d处被che击杀\n",x,y);
                            return 1;
                        }
                    }
                }
                else if(j==y){
                    if(i>=x){
                        count=0;
                        for(int k=x+1;k<=i-1;k++){
                            if(map[k][j]!=0){
                                count++;
                            }
                        }
                        if(count==0){
                            //printf("x=%d y=%d处被che击杀\n",x,y);
                            return 1;
                        }
                    }
                    else if(i<=x){
                        count=0;
                        for(int k=i+1;k<=x-1;k++){
                            if(map[k][j]!=0) {
                                count++;
                            }
                        }
                        if(count==0){
                            //printf("x=%d y=%d处被che击杀\n",x,y);
                            return 1;
                        }
                    }
                }

            }
            else if(map[i][j]==3){//canon
                int count;
                if(i==x ){
                    if(y<=j){
                        count=0;
                        for(int k=y+1;k<=j-1;k++){
                            if(map[i][k]!=0) {
                                count++;

                            }
                        }
                        if(count ==1 ){
                            // printf("x=%d y=%d处被炮击杀\n",x,y);
                            return 1;

                        }
                    }
                    else if(y>=j){
                        count=0;
                        for(int k=j+1;k<=y-1;k++){
                            if(map[i][k]!=0) {
                                count++;
                            }
                        }
                        if(count==1){
                            // printf("x=%d y=%d处被炮击杀\n",x,y);
                            return 1;
                        }
                    }
                }
                else if(j==y){
                    if(i>=x){
                        count=0;
                        for(int k=x+1;k<=i-1;k++){
                            if(map[k][j]!=0){
                                count++;
                            }
                        }
                        if(count==1){
                            // printf("x=%d y=%d处被炮击杀\n",x,y);
                            return 1;
                        }
                    }
                    else if(i<=x){
                        count=0;
                        for(int k=i+1;k<=x-1;k++){
                            if(map[k][j]!=0) {
                                count++;
                            }
                        }
                        if(count==1){
                            //printf("x=%d y=%d处被炮击杀\n",x,y);
                            return 1;
                        }
                    }
                }
            }
            else if(map[i][j]==4){//horse
                for(int d=0;d<8;d++){
                    hx=i+hmove[d][0];
                    hy=j+hmove[d][1];
                    fx=i+hfeet[d][0];
                    fy=j+hfeet[d][1];
                    if(hx<=0 || hy<=0 || hx>10 || hy>9 || map[fx][fy]!=0) continue;
                    if(hx==x && hy==y){
                        //printf("x=%d y=%d处被马击杀\n",x,y);
                        return 1;
                    }
                }

            }
        }
    }

    return 0;
}
int main(){
    int n,x,y,t;
    int pgx,pgy;
    char s;

    while(scanf("%d %d %d",&n,&gx,&gy)){
        int flag=1;
        memset(map, 0, sizeof(map));
        if(n==0 && gx==0 && gy==0 ) break;
        getchar();
        for(int i=0;i<n;i++){
            cin>>s>>x>>y;
            if(s=='G') {
                map[x][y]=1;

            }
            else if(s=='R') {
                map[x][y]=2;

            }
            else if(s=='C') map[x][y]=3;
            else if(s=='H') map[x][y]=4;

        }
        for(int d=0;d<4;d++){
            t=0;
            pgx=gx+gmove[d][0];
            pgy=gy+gmove[d][1];
            if(pgx<=0 || pgy<4 || pgx>3 || pgy>6) continue;
            if(map[pgx][pgy]!=0) {
                t=map[pgx][pgy];
                map[pgx][pgy]=0;
            }
            if(Cheak(pgx,pgy)==0) flag=0;
            map[pgx][pgy]=t;
        }
        printf("%s\n",flag?"YES":"NO");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值