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;
}

转载于:https://www.cnblogs.com/AbandonZHANG/archive/2012/06/24/2598270.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值