UVA1589(HDU4121)

来源:互联网 发布:管家婆软件有哪几种 编辑:程序博客网 时间:2024/05/21 05:41

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”.
InputThe 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.OutputFor each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’. 
Sample Input
2 1 4G 10 5R 6 43 1 5H 4 5G 10 5C 7 50 0 0
Sample Output
YESNO          
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.
 
题意:

 

就是给出一个象棋残局,问是否绝杀,并且黑方只有一个帅(其他规则与象棋走法一致)

坑点:

1.将可以吃掉将军的棋子

2.马的蹩脚

3.炮和将中间有且只有一个棋子的时候才能吃掉将

4.车和将中间没有棋子时才能吃将


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int s1[20],s2[20],n;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char a[20][20];
int check(int x,int y)
{
    if(x<1||x>3)
    return -2;
    if(y<4||y>6)
    return -2;
    int i,j,k,p,q,r,t;
    for(i=0;i<n;i++)
    {
        j=s1[i];
        k=s2[i];
        if(x==j&&y==k)
        continue;
        if(a[i][0]=='R'||a[i][0]=='G')
        {
            if(j==x)
            {
            if(k<y)
            {
            for(r=0;r<n;r++)
            {
                p=s1[r];
                q=s2[r];
                if(p==x&&q>k&&q<y)
                break;
            }
            if(r==n)
            return 0;
            }
            else
            if(k>y)
            {
                for(r=0;r<n;r++)
                {
                    p=s1[r];
                    q=s2[r];
                    if(p==x&&q>y&&q<k)
                    break;
                }
                if(r==n)
                return 0;
            }
            }
            else
            if(k==y)
            {
                if(j<x)
                {
                    for(r=0;r<n;r++)
                    {
                        p=s1[r];
                        q=s2[r];
                        if(q==y&&p>j&&p<x)
                        break;
                    }
                    if(r==n)
                    return 0;
                }
                else
                if(j>x)
                {
                    for(r=0;r<n;r++)
                    {
                        p=s1[r];
                        q=s2[r];
                        if(q==y&&p>x&&p<j)
                        break;
                    }
                    if(r==n)
                    return 0;
                }
            }
        }
        else
        if(a[i][0]=='H')
        {
            for(r=0;r<n;r++)
            {
                p=s1[r];
                q=s2[r];
                if(p==x&&q==y)
                continue;
                if(q==k&&p==j-1)
                break;
            }
            if(r==n)
            {
                if(j-2==x&&k-1==y)
                    return 2;
                if(j-2==x&&k+1==y)
                    return 2;
            }
            for(r=0;r<n;r++)
            {
                if(p==x&&q==y)
                continue;
                p=s1[r];
                q=s2[r];
                if(q==k&&p==j+1)
                break;
            }
            if(r==n)
            {
                if(j+2==x&&k-1==y)
                    return 2;
                if(j+2==x&&k+1==y)
                    return 2;
            }
            for(r=0;r<n;r++)
            {
                if(p==x&&q==y)
                continue;
                p=s1[r];
                q=s2[r];
                if(p==j&&q==k-1)
                break;
            }
            if(r==n)
            {
                if(j-1==x&&k-2==y)
                    return 2;
                if(j+1==x&&k-2==y)
                    return 2;
            }
            for(r=0;r<n;r++)
            {
                if(p==x&&q==y)
                continue;
                p=s1[r];
                q=s2[r];
                if(p==j&&q==k+1)
                break;
            }
            if(r==n)
            {
                if(j-1==x&&k+2==y)
                    return 2;
                if(j+1==x&&k+2==y)
                    return 2;
            }
        }
        else
        if(a[i][0]=='C')
        {
            if(j!=x&&k!=y)
            continue;
            if(j==x)
            {
            if(k<y)
            {
            t=0;
            for(r=0;r<n;r++)
            {
                p=s1[r];
                q=s2[r];
                if(p==x&&q==y)
                continue;
                if(p==x&&q>k&&q<y)
                t++;
            }
            if(t==1)
            return 3;
            }
            else
            if(k>y)
            {
                t=0;
                for(r=0;r<n;r++)
                {
                    p=s1[r];
                    q=s2[r];
                    if(p==x&&q==y)
                    continue;
                    if(p==x&&q>y&&q<k)
                    t++;
                }
                if(t==1)
                return 3;
            }
            }
            else
            if(k==y)
            {
                if(j<x)
                {
                    t=0;
                    for(r=0;r<n;r++)
                    {
                        p=s1[r];
                        q=s2[r];
                        if(p==x&&q==y)
                        continue;
                        if(q==y&&p>j&&p<x)
                        t++;
                    }
                    if(t==1)
                    return 3;
                }
                else
                if(j>x)
                {
                    t=0;
                    for(r=0;r<n;r++)
                    {
                        p=s1[r];
                        q=s2[r];
                        if(p==x&&q==y)
                        continue;
                        if(q==y&&p>x&&p<j)
                        t++;
                    }
                    if(t==1)
                    return 3;
                }
            }
        }
    }
    return -1;
}
int main()
{
    int i,j,k,x1,y1,x2,y2,t,x,y,ca=1;
    while(scanf("%d%d%d",&n,&x1,&y1)!=EOF)
    {
        if(n==0&&x1==0&&y1==0)
            break;
        for(i=0;i<n;i++)
        scanf("%s%d%d",a[i],&s1[i],&s2[i]);
        for(i=0;i<4;i++)
        {
            x2=x1+dir[i][0];
            y2=y1+dir[i][1];
            t=check(x2,y2);
            if(t==-1)
            break;
        }
        //printf("%d:",ca++);
        if(i==4)
        printf("YES\n");
        else
        printf("NO\n");
    }
    return 0;
}


附带数据
2 1 4
R 2 2
G 10 5
2 1 5
H 3 5
C 10 5
2 1 4
G 10 5
R 6 4
3 1 5
H 4 5
G 10 5
C 7 5
2 1 5
R 4 4
G 10 5
3 1 5
G 10 4
R 5 5
H 3 7
4 1 5
G 10 4
C 6 5
H 5 5
R 1 1
5 1 5
G 10 4
C 6 5
H 5 5
H 4 5
R 1 1
3 1 5
G 10 4
C 2 7
H 3 7
3 1 5
G 10 4
R 5 5
R 1 6
4 1 5
G 10 4
R 5 5
R 1 6
H 3 7
1
1 4
G 10 4
3 1 4
G 10 4
R 10 3
R 10 5

YES
YES
YES
NO
NO
YES
YES
NO
NO
NO
YES
NO
YES

原创粉丝点击