时隔一年的再开 紫书Uva1589 象棋 模拟

Xiangqi
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1668 Accepted: 421

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



时隔一年,把这题wa了一年的题A了,感觉还是需要总结自己的题目好整理。因此又开始写题解啦

逐梦逐梦逐梦演艺圈圈圈圈圈圈圈圈圈圈圈圈圈圈圈圈圈

下面为AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
using namespace std;
const int maxn = 50;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int xHorse[8] = {-2,-2,2,2,1,-1,-1,1};
int yHorse[8] = {1,-1,-1,1,-2,-2,2,2};
struct unit{
    int type;
    pair<int, int> pos;
}save[maxn];
bool checkChariot(int kingx, int kingy, int index, int n){
    if(kingx==save[index].pos.first&&kingy==save[index].pos.second) return false;
    
    if(kingx!=save[index].pos.first&&kingy!=save[index].pos.second) return false;
    int i,j;
    int littleX = min(kingx,save[index].pos.first);
    int bigX = max(kingx,save[index].pos.first);
    int littleY = min(kingy,save[index].pos.second);
    int bigY = max(kingy,save[index].pos.second);
    bool haveThing = false;
    if(kingx==save[index].pos.first){
        
        for(i=littleY+1;i<bigY;i++){
            for(j=1;j<=n;j++){
                if(j==index) continue;
                if(save[j].pos.first==kingx&&save[j].pos.second==i){
                    haveThing = true;
                    break;
                }
            }
        }
    }
    if(kingy==save[index].pos.second){
        
        for(i=littleX+1; i<bigX; i++){
            for(j=1;j<=n;j++){
                if(j==index) continue;
                if(save[j].pos.second==kingy&&save[j].pos.first==i){
                    haveThing = true;
                    break;
                }
            }
        }
    }
    if(haveThing){
        return false;
    }else{
        return true;
    }
}
bool checkCannon(int kingx, int kingy, int index, int n){
    //cout<<"\nCannon start\n";
    if(kingx==save[index].pos.first&&kingy==save[index].pos.second) return false;
    if(kingx!=save[index].pos.first&&kingy!=save[index].pos.second) return false;
    int i,j;
    int littleX = min(kingx,save[index].pos.first);
    int bigX = max(kingx,save[index].pos.first);
    int littleY = min(kingy,save[index].pos.second);
    int bigY = max(kingy,save[index].pos.second);
    int haveThing = 0;
    if(kingx==save[index].pos.first){
        
        for(i=littleY+1;i<bigY;i++){
            for(j=1;j<=n;j++){
                if(j==index) continue;
                if(save[j].pos.first==kingx&&save[j].pos.second==i){
                    ++haveThing;
                }
            }
        }
    }
    if(haveThing==1){
        return true;
    }
    haveThing = 0;
    if(kingy==save[index].pos.second){
        
        for(i=littleX+1; i<bigX; i++){
            for(j=1;j<=n;j++){
                if(j==index) continue;
                if(save[j].pos.second==kingy&&save[j].pos.first==i){
                    ++haveThing;
                }
            }
        }
    }
    if(haveThing==1){
        return true;
    }
    
    return false;
}
int killme(int x){
    return 0.5 * x *(abs(x)-1);
}
bool checkHorse(int kingx, int kingy, int index, int n){
    if(kingx==save[index].pos.first&&kingy==save[index].pos.second) return false;
    
    int cantX, cantY;
    bool flag = false;
    for(int i=0; i<8; i++){
        bool cantGo = false;
        if(abs(xHorse[i])==2){
            cantX = save[index].pos.first + killme(xHorse[i]);
            cantY = save[index].pos.second ;
        }else{
            cantY = save[index].pos.second + killme(yHorse[i]);
            cantX = save[index].pos.first ;
        }
        for(int j=1;j<=n;j++){
            if(j==index) continue;
            if(cantX==save[j].pos.first&&cantY==save[j].pos.second){
                cantGo = true;
                break;
            }
        }
        if(cantGo) continue;
        
        if(save[index].pos.first+xHorse[i]==kingx&&save[index].pos.second+yHorse[i]==kingy){
            flag = true;
            break;
        }
    }
    return flag;
}
bool check(int kingx, int kingy, int n){
    int i;
    bool flag = false;
    for(i=1;i<=n;i++){
        if(save[i].type==1){
            if(checkChariot(kingx, kingy, i, n)){
                flag = true;
                break;
            }
        }else if(save[i].type==2){
            if(checkChariot(kingx, kingy, i, n)){
                flag = true;
                break;
            }
            
        }else if(save[i].type==3){
            if(checkCannon(kingx, kingy, i, n)){
                flag = true;
                break;
            }
        }else{
            if(checkHorse(kingx, kingy, i, n)){
                flag = true;
                break;
            }
        }
    }
    return flag;
}
int main(){
    int kingx,kingy;
    int n,i;
    char op;
    while(cin>>n>>kingx>>kingy){
        if(n==0&&kingx==0&&kingy==0){
            break;
        }
        bool isFace = false;
        for(i=1;i<=n;i++){
            cin>>op>>save[i].pos.first>>save[i].pos.second;
            if(op=='G'){
                save[i].type = 1;
                if(checkChariot(kingx, kingy, i, n)){
                    isFace = true;
                }
            }else if(op=='R'){
                save[i].type = 2;
            }else if(op=='H'){
                save[i].type = 4;
            }else{
                save[i].type = 3;
            }
        }
        bool flag = false;
        for(i=0;i<4;i++){
            int nx = kingx + dx[i];
            int ny = kingy + dy[i];
            if(1<=nx&&nx<=3&&4<=ny&&ny<=6){
                if(!check(nx, ny, n)){
                    flag = true;
                    break;
                }
            }
        }
        if(isFace) flag = true;
        if(flag){
            printf("NO\n");
        }else{
            printf("YES\n");
        }
    }
    
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值