首页 > ACM题库 > HDU-杭电 > HDU 4121-Xiangqi-模拟-[解题报告]HOJ
2015
04-16

HDU 4121-Xiangqi-模拟-[解题报告]HOJ

Xiangqi

问题描述 :

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

Ji-Tu Problem

We only use 4 kinds of pieces introducing as follows:
Ji-Tu ProblemGeneral: 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.
Ji-Tu ProblemChariot: the chariots can move and capture vertically and horizontally by any distance, but may not jump over intervening pieces
Ji-Tu ProblemCannon: 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.
Ji-Tu ProblemHorse: 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”.

Ji-Tu Problem

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

输入:

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.

输出:

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.

样例输入:

2 1 4
G 10 5
R 6 4

3 1 5
H 4 5
G 10 5
C 7 5

0 0 0

样例输出:

YES
NO

Hint
Ji-Tu Problem
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.

这题有点坑而已。。。但不算很坑。。。

先吐槽些省赛的事,只能用蒟蒻来形容,代码能力渣,尤其是模拟题上手是没有自信。。。平时题目刷的也不够,方向还特别的窄,╮(╯▽╰)╭

回来后被严重B视了。。好在有些人还是比较理解咱这个马上要考TG的狗。。。

好,回到正式正题,这道题题意简单,就是判断当前的黑方是不是彻底OVER了。。。象棋规则省略。这种在坐标上的模拟题,首先要看看他的X,Y代表什么,而棋盘上的X,Y和坐标系的X,Y貌似是反着的。。。(如果所输入的每个棋子的坐标中第一个数为X)。。这个就要小心,因为读题问题WA是很不值当(调试)的。。(PS: 话说省赛时读题很糟糕。。)

然后分别按照车、马、炮的规则判断就是了。

坑点,由于将可以移动,所以可能会吃掉棋子。。附代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>

#define pb push_back
#define cl(a) memset(a,0,sizeof(a))

using namespace std;

struct zb
{
    int x;
    int y;
};

const int fy[4]={-1,0,1,0};
const int fx[4]={0,-1,0,1};
const int hy[8]={-2,-2,-1,1,2,2,1,-1};
const int hx[8]={1,-1,-2,-2,-1,1,2,2};

vector<zb>c,h,r;
zb g;
int map[13][13];


void init()
{
    cl(map);
    c.clear();
    h.clear();
    r.clear();
}

int lie_pd(int y1,int y2,int x0,int t)
{
    int i,z;
    if (y1>y2)
    {
        int tmp=y1;
        y1=y2;
        y2=tmp;
    }
    z=0;
    for (i=y1+1;i<y2;i++)
        if (map[x0][i]) z++;
    if (z==t) return 1;
    return 0;
}

int hang_pd(int x1,int x2,int y0,int t)
{
    int i,z;
    if (x1>x2)
    {
        int tmp=x1;
        x1=x2;
        x2=tmp;
    }
    z=0;
    for (i=x1+1;i<x2;i++)
        if (map[i][y0]) z++;
    if (z==t) return 1;
    return 0;
}

int pd_general(int x0,int y0)
{
    int i;
    if (g.y==y0)
    {
        for (i=x0+1;i<g.x;i++) 
            if (map[i][y0]) return 1;
        return 0;
    }
    return 1;
}

int pd_chariot(int x0,int y0)
{
    int i,j;
    for (j=0;j<r.size();j++)
    {
        if (r[j].x==x0&&r[j].y==y0) continue;
        else if (r[j].x==x0) 
        {
            if (lie_pd(r[j].y,y0,x0,0)) return 0;
        }
        else if (r[j].y==y0)
        {
            if (hang_pd(r[j].x,x0,y0,0)) return 0;
        }
    }
    return 1;
}

int pd_connon(int x0,int y0)
{
    int i,j;
    for (j=0;j<c.size();j++)
    {
        if (c[j].x==x0) 
        {
            if (lie_pd(c[j].y,y0,x0,1)) return 0;
        }
        if (c[j].y==y0)
        {
            if (hang_pd(c[j].x,x0,y0,1)) return 0;
        }
    }
    return 1;
}

int pd_horse(int x0,int y0)
{
    int i,j,x11,y11,x1,y1;
    for (j=0;j<h.size();j++)
    {
        for (i=0;i<8;i++)
        {
            x1=h[j].x+hx[i];
            y1=h[j].y+hy[i];
            x11=h[j].x+fx[i/2];
            y11=h[j].y+fy[i/2];
            if (x1==x0&&y1==y0&&!map[x11][y11]) return 0;
        }
    }
    return 1;
}

int pd(int x0,int y0)
{
    return pd_horse(x0,y0)*pd_chariot(x0,y0)*pd_connon(x0,y0)*pd_general(x0,y0);
}

int main()
{
    int n,i,x0,y0;
    while (cin>>n)
    {   
        init();
        cin>>x0>>y0;
        if (!n&&!x0&&!y0) break;
        for (i=1;i<=n;i++)
        {
            char ch;
            zb t;
            cin>>ch;
            cin>>t.x>>t.y;
            if (ch=='G') g=t;
            if (ch=='H') h.pb(t);
            if (ch=='C') c.pb(t);
            if (ch=='R') r.pb(t);
            map[t.x][t.y]=1; 
        }
        for (i=0;i<4;i++)
        {
            int x1,y1;
            x1=x0+fx[i];
            y1=y0+fy[i];
            if ((x1>=1&&x1<=3)&&(y1>=4&&y1<=6)) 
            {
                if (pd(x1,y1))
                {
                    cout<<"NO"<<endl;
                    break;
                }
            }
        }
        if (i==4) cout<<"YES"<<endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/liverpippta/article/details/8980225


  1. 思路二可以用一个长度为k的队列来实现,入队后判断下队尾元素的next指针是否为空,若为空,则出队指针即为所求。

  2. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。