uva 1589Xiangqi




Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4754    Accepted Submission(s): 1111


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

明明是照着ac代码来的,就是超时,检查了好几遍也没找到不同,没想到这题这么简单,

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;

char map[20][20];

int judge(int x,int y)
{
    int sign=0;
    for(int i=x+1;i<=10;i++)
    {
        if(map[i][y])
            sign++;
        if(sign==1 && map[i][y]=='G')       //只有一个棋,将死。
            return 1;
        else if(sign>1)
             break;
    }
    sign=0;
    for(int i=x+1;i<=10;i++)
    {
        if(map[i][y])
            sign++;
        if(sign==1 && map[i][y]=='R')     //上方的车 将死
            return 1;
        else if(sign>1)
             break;
    }
    sign=0;
    for(int i=x-1;i>0;i--)
    {
         if(map[i][y])
            sign++;
        if(sign==1 && map[i][y]=='R')     //下方的车 将死
            return 1;
        else if(sign>1)
             break;
    }
    sign=0;
    for(int i=y-1;i>0;i--)
    {
        if(map[x][i])
            sign++;
        if(sign==1 && map[x][i]=='R')
            return 1;
        else
            if(sign>1)
            break;
    }
    sign=0;
    for(int i=y+1;i<10;i++)
    {
        if(map[x][i])
            sign++;
        if(sign==1 && map[x][i]=='R')        //右边的车将死
            return 1;
        else if(sign>1)
              break;
    }
    sign=0;
    for(int i=x+1;i<=10;i++)
    {
        if(map[i][y])
            sign++;
        if(sign==2 && map[i][y]=='C')     //炮在上面
            return 1;
        else if(sign>2)
             break;
    }
    sign=0;
    for(int i=x-1;i>0;i--)
    {
        if(map[i][y])
            sign++;
        if(sign==2 && map[i][y]=='C')     //炮在下面
            return 1;
        else if(sign>2)
             break;
    }
    sign=0;
    for(int i=y-1;i>0;i--)
    {
        if(map[x][i])
            sign++;
        if(sign==2 && map[x][i]=='C')    //炮在左边
            return 1;
        else if(sign>2)
             break;
    }
    sign=0;
    for(int i=y+1;i<10;i++)
    {
        if(map[x][i])
            sign++;
        if(sign==2 && map[x][i]=='C')   //炮在右边
            return 1;
        else if(sign>2)
             break;
    }
     if(x - 1)
    {                                    //黑将上方马能否将军
        if(!map[x - 1][y - 1])                       //黑将左上马能否将军
            if(map[x - 1][y - 2] == 'H')
            return 1;
        if(!map[x - 1][y + 1])                       //黑将右上马能否将军
            if(map[x - 1][y + 2] == 'H')
            return 1;
    }
    if(x - 2)
    {                                    //黑将上方马能否将军
        if(!map[x - 1][y - 1])                       //黑将左上马能否将军
            if(map[x - 2][y - 1] == 'H')
            return 1;
        if(!map[x - 1][y + 1])                       //黑将右上马能否将军
            if(map[x - 2][y + 1] == 'H')
            return 1;
    }
    if(!map[x + 1][y - 1]) {                           //黑将左下马能否将军
        if(map[x + 1][y - 2] == 'H')
            return 1;
        if(map[x + 2][y - 1] == 'H')
            return 1;
    }
    if(!map[x + 1][y + 1]) {                           //黑将右下马能否将军
        if(map[x + 1][y + 2] == 'H')
            return 1;
        if(map[x + 2][y + 1] == 'H')
            return 1;
    }


    /*
    if(x-1>0 && map[x-1][y-2]==4 && !map[x-1][y-1])
        return 1;
    if(map[x+1][y-2]==4 && !map[x+1][y-1])
        return 1;
    if(x-2>0 && map[x-2][y-1]==4 && !map[x-1][y-1])
        return 1;
    if(map[x+2][y-1]==4 && !map[x+1][y-1])
        return 1;
    if(x-1>0 && map[x-1][y+2]==4 && !map[x-1][y+1])
        return 1;
    if(map[x+1][y+2]==4 && !map[x+1][y+1])
        return 1;
    if(x-2>0 && map[x-2][y+1]==4 && !map[x-1][y+1])
        return 1;
    if(map[x+2][y+1]==4 && !map[x+1][y+1])
        return 1;
    */
    return 0;
}
int black_move(int x,int y)
{
    if(x-1>0)
        if(!judge(x-1,y))
            return 0;
    if(y-1>=4)
        if(!judge(x,y-1))
            return 0;
    if(x+1<=3)
        if(!judge(x+1,y))
            return 0;
    if(y+1<=6)
        if(!judge(x,y+1))
            return 0;
    return 1;
}



int main()
{
    int n,x,y;
    while(scanf("%d%d%d",&n,&x,&y)!=EOF)
    {
        char kind;
        int x1,y1;
        if(!n && !x && !y) break;
        memset(map,0,sizeof(map));
        map[x][y]='G';
        getchar();
        while(n--)
        {
            scanf("%c%d%d",&kind,&x1,&y1);
            map[x1][y1]=kind;
         /*   if(kind == 'G') map[x1][y1]=2;
            else if(kind == 'R') map[x1][y1]=3;
                 else if(kind == 'H') map[x1][y1]=4;
                      else if(kind == 'C') map[x1][y1]=5;
        */
            getchar();
        }

   /*     for(int i=0;i<=10;i++)
        {
            for(int j=0;j<=10;j++)
                printf("%d ",map[i][j]);
            printf("\n");
        } */
        if(black_move(x,y))
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值