码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4121 Xiangqi 模拟

时间:2017-07-26 23:28:49      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:cti   direct   and   point   地图   ack   capture   cap   asc   

原题: http://acm.hdu.edu.cn/showproblem.php?

pid=4121

题目:

Xiangqi

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

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

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.

思路:

对于给定的棋盘。黑棋仅仅有一个,红棋有n个,如今黑将能够走一步。假设怎么走都活不了。就是被将死了。输出YES。

这道题我们仅仅须要模拟每一个红棋的攻击范围,最后看黑将的能走的地方是否在攻击范围就能够了。

代码:

#include <iostream>
#include"cstdio"
#include"stdlib.h"
#include"string.h"
using namespace std;

int main()
{
    //freopen("in.txt","r",stdin);
    int mp[12][11];
    int t,sx,sy;
    while(scanf("%d %d %d",&t,&sx,&sy)!=EOF)
    {
        if(t==0&&sx==0&&sy==0)    break;
        memset(mp,0,sizeof(mp));
        while(t--)
        {
            int x,y;
            char s[10];
            scanf("%s %d %d",s,&x,&y);
            //由于帅和车的攻击范围一样,所以我们这里把帅和车看作同一种棋
            if(s[0]==‘G‘)//帅
                mp[x][y]=2;
            else if(s[0]==‘R‘)//车
                mp[x][y]=2;
            else if(s[0]==‘H‘)//马
                mp[x][y]=3;
            else if(s[0]==‘C‘)//炮
                mp[x][y]=4;
        }
        //用来存储攻击范围
        bool flag[11][10];
        memset(flag,false,sizeof(flag));
        //遍历每一个点
        for(int i=1; i<=10; i++)
        {
            for(int j=1; j<=9; j++)
            {
            //枚举每一种棋
                if(mp[i][j]==2)
                {
                    //向右
                    for(int k=j+1; k<=9; k++)
                    {
                        //该方向的全部点都该为被攻击区域
                        flag[i][k]=1;
                        //除非遇到有棋子遮挡。但那个棋子的位置是能够被攻击的,
                        //后面的位置才不会被攻击。所以这里先标记再推断。
                        //防止黑将的下一步能够吃掉你那里的棋子,但
                        //是那个棋子之前占的地方并不在你的攻击范围内
                        if(mp[i][k])
                            break;
                    }
                    //向左
                    for(int k=j-1; k>=1; k--)
                    {
                        flag[i][k]=1;
                        if(mp[i][k])
                            break;
                    }
                    //向下
                    for(int k=i+1; k<=10; k++)
                    {
                        flag[k][j]=1;
                        if(mp[k][j])
                            break;
                    }
                    //向上
                    for(int k=i-1; k>=1; k--)
                    {
                        flag[k][j]=1;
                        if(mp[k][j])
                            break;
                    }
                }
                //考虑到出界问题,所以这里分成8种情况
                //实际上把数组数组往右下角再平移一格就不用操心了
                //可是那样写代码有点痛苦
                else if(mp[i][j]==3)
                {
                    //向左上
                    if(mp[i][j-1]==0&&j>=3&&i>=2)
                    {
                        flag[i-1][j-2]=1;
                    }
                    //向左下
                    if(mp[i][j-1]==0&&j>=3&&i<=9)
                    {
                        flag[i+1][j-2]=1;
                    }
                    //向右上
                    if(mp[i][j+1]==0&&j<=8&&i>=2)
                    {
                        flag[i-1][j+2]=1;
                    }
                    //向右下
                    if(mp[i][j+1]==0&&j<=8&&i<=9)
                    {
                        flag[i+1][j+2]=1;
                    }
                    //向上左
                    if(mp[i-1][j]==0&&i>=3&&j>=2)
                    {
                        flag[i-2][j-1]=1;
                    }
                    //向上右
                    if(mp[i-1][j]==0&&i>=3&&j<=8)
                    {
                        flag[i-2][j+1]=1;
                    }
                    //向下左
                    if(mp[i+1][j]==0&&i<=7&&j>=2)
                    {
                        flag[i+2][j-1]=1;
                    }
                    //向下右
                    if(mp[i+1][j]==0&&i<=7&&j<=8)
                    {
                        flag[i+2][j+1]=1;
                    }
                }
                else if(mp[i][j]==4)
                {
                    int tflag=0;
                    //向右
                    for(int k=j+1; k<=9; k++)
                    {
                        //当前没有炮架子的时候并扫描到了有棋能够当架子
                        if(tflag==0&&mp[i][k]!=0)
                            tflag=1;
                        //有架子后面的都是被攻击范围
                        else if(tflag==1)
                        {
                            flag[i][k]=1;
                            //一旦遇到有棋的地方。该棋是在攻击范围内的。
                            //可是后面的不在了,所以要跳出循环
                            if(mp[i][k])    break;
                        }

                    }
                    tflag=0;
                    //向左
                    for(int k=j-1; k>=1; k--)
                    {
                        if(tflag==0&&mp[i][k]!=0)
                            tflag=1;
                        else if(tflag==1)
                        {
                            flag[i][k]=1;
                            if(mp[i][k])    break;
                        }
                    }
                    tflag=0;
                    //向下
                    for(int k=i+1; k<=10; k++)
                    {
                        if(tflag==0&&mp[k][j]!=0)
                            tflag=1;
                        else if(tflag==1)
                        {
                            flag[k][j]=1;
                            if(mp[k][j])    break;
                        }
                    }
                    tflag=0;
                    //向上
                    for(int k=i-1; k>=1; k--)
                    {
                        if(tflag==0&&mp[k][j]!=0)
                            tflag=1;
                        else if(tflag==1)
                        {
                            flag[k][j]=1;
                            if(mp[k][j])    break;
                        }
                    }
                }
            }
        }
        //将地图上黑方城外都标记为攻击区域,就是说走出界也是在攻击范围内
        for(int i=4;i<=6;i++)
        {
            flag[0][i]=1;
            flag[4][i]=1;
        }
        for(int i=1;i<=3;i++)
        {
            flag[i][3]=1;
            flag[i][7]=1;
        }
//        for(int i=0; i<=11; i++)
//        {
//            for(int j=0; j<=10; j++)
//            {
//                printf("%d ",flag[i][j]);
//            }
//            printf("\n");
//        }
        //当4边都是攻击范围就GG了
        if(flag[sx+1][sy]&&flag[sx-1][sy]&&flag[sx][sy+1]&&flag[sx][sy-1])
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

HDU 4121 Xiangqi 模拟

标签:cti   direct   and   point   地图   ack   capture   cap   asc   

原文地址:http://www.cnblogs.com/lytwajue/p/7242109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!