• ACM/ICPC 2011 福州赛区现场赛第一题 (A .Xiangqi)


    A.Xiangqi

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

    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.
     
    Source
     

    题目大意:
    判断黑方是否为死棋。

    思路:
    果断模拟之。。。
    我将一个子的杀点(该能杀到的点)和所占点(该子所在的位置)分开保存。
    马注意鳖马腿.处理方法:向一个方向跳的时候,记录蹩马腿的位置,加上判断此位置是否是某子的所占点。

    注意:
    车的所占点,并不是他的杀点!!
    帅、车的杀点,包括挡在他前面的子的所在点。
    炮的杀点,包括挡在炮前面的前面的子的所在点。

    这次真的感受到了出数据的力量。上面注意的三点,自己全都没考虑到。而且细节方面考虑太不周到了,调试出来后都是写不该有的失误。

    模拟题还是要多练啊!!!。。。静下心,把每一步都确认好。。。


    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <vector>
    #include <algorithm>

    const int inf=0x7fffffff;

    using namespace std;

    struct point//red  pieces
    {
        int x;
        int y;
        char va;
    }p[8];

    int n;
    int fx,fy;//black general坐标
    int vis[10][9];//记录棋盘坐标上是否有red pieces
    int kill[10][9];//记录轮到red走时的杀点
    int hor[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};//马走日
    int hobo[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; //判断马的蹩脚点,black pieces能走的四个方向


    bool noover(int x,int y)//坐标是否在棋盘内
    {
        if ((x>=0)&&(x<10)&&(y>=0)&&(y<9))
            return true;
        else return false;
    }


    bool gnoover(int x,int y)//坐标是否在general能落的田字格内
    {
        if ((x>=0)&&(x<3)&&(y>=3)&&(y<6))
            return true;
        else return false;
    }


    void ban(point p)//找出red pieces各个可以走的杀点。
    {
        if ((p.va=='G')||(p.va=='R'))
        {
            for (int i=p.x+1;i<10;i++)//十字四个方向
            {
                kill[i][p.y]=1;
                if (vis[i][p.y]==1)
                    break;
            }
            for (int i=p.x-1;i>=0;i--)
            {
                kill[i][p.y]=1;
                if (vis[i][p.y]==1)
                    break;
            }
            for (int i=p.y+1;i<9;i++)
            {
                kill[p.x][i]=1;
                if (vis[p.x][i]==1)
                    break;
            }
            for (int i=p.y-1;i>=0;i--)
            {
                kill[p.x][i]=1;
                if (vis[p.x][i]==1)
                    break;
            }
        }
        else if(p.va=='H')
        {
            for (int i=0;i<8;i++)
                if (noover(p.x+hobo[i/2][0],p.y+hobo[i/2][1]))
                    if (vis[p.x+hobo[i/2][0]][p.y+hobo[i/2][1]]==0)
                    {
                        if (noover(p.x+hor[i][0],p.y+hor[i][1]))
                            kill[p.x+hor[i][0]][p.y+hor[i][1]]=1;
                    }
        }
        else if(p.va=='C')
        {
            for (int i=p.x+1;i<10;i++) //十字四个方向
                if (vis[i][p.y]==1)
                {
                    for (int j=i+1;j<10;j++)
                    {
                        kill[j][p.y]=1;
                        if (vis[j][p.y]==1)
                            break;
                    }
                    break;
                }


            for (int i=p.x-1;i>=0;i--)
                if (vis[i][p.y]==1)
                {
                    for (int j=i-1;j>=0;j--)
                    {
                        kill[j][p.y]=1;
                        if (vis[j][p.y]==1)
                            break;
                    }
                    break;
                }
            for (int i=p.y+1;i<9;i++)
                if (vis[p.x][i]==1)
                {
                    for (int j=i+1;j<9;j++)
                    {
                        kill[p.x][j]=1;
                        if (vis[p.x][j]==1)
                            break;
                    }
                    break;
                }


            for (int i=p.y-1;i>=0;i--)
                if (vis[p.x][i]==1)
                {
                    for (int j=i-1;j>=0;j--)
                    {
                        kill[p.x][j]=1;
                        if (vis[p.x][j]==1)
                            break;
                    }
                    break;
                }
        }
    }






    int main()
    {
        while(cin>>n>>fx>>fy)
        {
            if ((n&&fx&&fy)==0) return 0;
            fx--;
            fy--;
            memset(vis,0,sizeof(vis));
            memset(kill,0,sizeof(kill));
            for (int i=0;i<n;i++)
                {
                    cin>>p[i].va>>p[i].x>>p[i].y;
                    p[i].x-=1;
                    p[i].y-=1;
                    vis[p[i].x][p[i].y]=1;
                }


            for (int i=0;i<n;i++)
                ban(p[i]);
            //for (int i=0;i<10;i++)
            //   for (int j=0;j<9;j++)
            //    cout<<i<<' '<<j<<' '<<kill[i][j]<<endl;
            int ok=0;


            for (int i=0;i<4;i++)
                if (gnoover(fx+hobo[i][0],fy+hobo[i][1]))
                    if (!kill[fx+hobo[i][0]][fy+hobo[i][1]])
                        ok=1;
            if (ok)
                cout<<"NO"<<endl;
            else cout<<"YES"<<endl;
        }


        return 0;
    }

  • 相关阅读:
    神武
    position 属性
    C# 工作区和窗口的属性名
    魔兽世界命令行
    Dota2一直 正在登录服务器的解决办法
    dota2交换物品
    Servlet的5种方式实现表单提交(注册小功能)
    JQuery的ajaxFileUpload的使用
    JS日期转换
    Tomcat7后台通过get接收数据处理乱码
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/2598270.html
Copyright © 2020-2023  润新知