JXUFE紫书第四章习题 A 4-1

考虑一个象棋残局,其中红方有n2≤n≤7)个旗子,黑方只有一个将。红方除了有一个帅(G)之外还有3种可能的棋子:车(R),马(H),炮(C),并且需要考虑蹩马腿与将和帅不能照面(将、帅如果同在一条直线上,中间又不隔着任何棋子的情况下,先走的一方获胜)的规则。

输入所有棋子的位置,保证局面合法并且红方已经将军。你的任务是判断红方是否已经把黑方将死。关于中国象棋的相关规则请参见原题。

原题描述

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

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.


样例输入和样例输出:


#include <iostream>
#include <cctype>
#include <memory.h>
using namespace std;


char chess[11][10];//棋盘


const int movH[8][2] = {{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1}};//馬的走位


//棋子
struct Chess
{
    char type;
    int x;
    int y;
};


//由馬的走位推出“馬脚”的位置
int isMove (int i)
{
    if(i > 0)
        return -1;
    else if (i < 0)
        return 1;
    else
        return 0;
}


//帥的攻击路线
bool G (int x, int y, int target_x, int target_y)
{
    int i;
    if (y != target_y)//如果帥与目标不在同一纵轴上,则必定无法攻击成功
        return false;
    //将的攻击路线上是否有别的棋子
    for (i = x-1; i > target_x; i--)
        if (isalpha(chess[i][y]))
            return false;
    return true;
}


//車的攻击路线
bool R (int x, int y, int target_x, int target_y)
{
    int i;
    if (x != target_x && y != target_y)
        return false;
    else
    {
        //按照車在敌方将的上下左右四个位置有不同的攻击路线(炮也一样)
        if (x == target_x)
        {
            if (y < target_y)//車在将的左边
                for (i = y+1; i <= target_y; i++)
                {
                    if (isalpha(chess[x][i]))
                        return false;
                    else
                        continue;
                }
            else if (y > target_y)//車在将的右边
                for (i = y-1; i >= target_y; i--)
                {
                    if (isalpha(chess[x][i]))
                        return false;
                    else
                        continue;
                }
        }
        else if (y == target_y)
        {
            if (x < target_x)//車在将的上方
                for (i = x+1; i <= target_x; i++)
                {
                    if (isalpha(chess[i][y]))
                        return false;
                    else
                        continue;
                }
            else if (x > target_x)//車在将的下方
                for (i = x-1; i >= target_x; i--)
                {
                    if (isalpha(chess[i][y]))
                        return false;
                    else
                        continue;
                }
        }
    }
    return true;
}


//馬的攻击路线
bool H (int x, int y, int target_x, int target_y)
{
    int i;
    for (i = 0; i < 8; i++)
        if (x+movH[i][0] == target_x && y+movH[i][1] == target_y)
            if (!isalpha(chess[i+isMove(movH[i][0])][i+isMove(movH[i][1])]))//“馬脚”处是否有棋子
                return true;
    return false;
}


//炮的攻击路线
bool C (int x, int y, int target_x, int target_y)
{
    int i,cnt = 0;
    if (x != target_x && y != target_y)
        return false;
    else
    {
        if (x == target_x)
        {
            if (y < target_y)
                for (i = y+1; i <= target_y; i++)
                {
                    if (isalpha(chess[x][i]))
                        cnt++;
                    else
                        continue;
                }
            else if (y > target_y)
                for (i = y-1; i >= target_y; i--)
                {
                    if (isalpha(chess[x][i]))
                        cnt++;
                    else
                        continue;
                }
        }
        else if (y == target_y)
        {
            if (x < target_x)
                for (i = x+1; i <= target_x; i++)
                {
                    if (isalpha(chess[i][y]))
                        cnt++;
                    else
                        continue;
                }
            else if (x > target_x)
                for (i = x-1; i >= target_x; i--)
                {
                    if (isalpha(chess[i][y]))
                        cnt++;
                    else
                        continue;
                }
        }
    }
    if (cnt == 1)//炮与攻击目标之间有且只有一个棋子才能攻击成功
        return true;
    else
        return false;
}


//红方所有棋子分别对目标位置进行攻击
bool isKill (Chess* p, int n, int target_x, int target_y)
{
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        if (p[i].type == 'G')
        {
            if (G(p[i].x, p[i].y, target_x, target_y))
                cnt++;
        }
        else if (p[i].type == 'R')
        {
            if (R(p[i].x, p[i].y, target_x, target_y))
                cnt++;
        }
        else if (p[i].type == 'C')
        {
            if (C(p[i].x, p[i].y, target_x, target_y))
                cnt++;
        }
        else if (p[i].type == 'H')
        {
            if (H(p[i].x, p[i].y, target_x, target_y))
                cnt++;
        }
        else
            continue;
    }
    if (cnt && !isalpha(chess[target_x][target_y]))
        return true;
    else if (isalpha(chess[target_x][target_y]) && cnt > 1)//若黑方将的下一步会吃掉红方一棋子(不包括将与帥之间的攻击),那么红方必须有其他棋子能成功进行下一步攻击
        return true;
    else
        return false;
}


int main (void)
{
    char c;
    int n,t_x,t_y;
    while ((cin >> n >> t_x >> t_y) && n && t_x && t_y)
    {
        memset(chess, '*', sizeof(chess));
        Chess* p = new Chess[7];
        chess[t_x][t_y] = 'g';
        int i,x,y;
        for (i = 0; i < n; i++)
        {
            cin >> c >> x >> y;
            chess[x][y] = c;
            p[i].type = c;
            p[i].x = x;
            p[i].y = y;
        }


        bool way1 = true, way2 = true, way3 = true, way4 = true;
        //先对帥的位置进行判断,如果将与帥处于可以互相攻击的位置,那么红方必输
        for (i = 0; i < n; i++)
            if (p[i].type == 'G')
                if (G(p[i].x, p[i].y, t_x, t_y))
                {
                    cout << "NO\n";
                    return 0;
                }
        //对黑方"将"所有可行的下一步棋进行模拟攻击
        if (t_x-1 >= 1)
        {
            way1 = false;
            if (isKill(p, n, t_x, t_y-1))
                way1 = true;
        }
        if (t_x+1 <= 3)
        {
            way2 = false;
            if (isKill(p, n, t_x, t_y+1))
                way2 = true;
        }
        if (t_y-1 >= 4)
        {
            way3 = false;
            if (isKill(p, n, t_x-1, t_y))
                way3 = true;
        }
        if (t_y+1 <= 6)
        {
            way4 = false;
            if (isKill(p, n, t_x+1, t_y))
                way4 = true;
        }
        if (way1 && way2 && way3 && way4)
            cout << "YES\n";
        else
            cout << "NO\n";
        delete []p;
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/jxufe_acmer/article/details/80326642