HDU 4121 Xiangqi(模拟)

文章地址:http://henuly.top/?p=666

题目:

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:
imgGeneral: 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.
imgChariot: the chariots can move and capture vertically and horizontally by any distance, but may not jump over intervening pieces
imgCannon: 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.
imgHorse: 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”.

img

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.

题目链接

用一个二维数组记录棋盘上的所有点,把红方棋子能够攻击到的点位全部标记出来,判断黑方“将”棋能否移动到红方棋子攻击不到的点位。

红方“帅”棋子可以攻击黑方“将”棋子,同样黑方“将”棋子也可以直接攻击红方“帅”棋子;

黑方“将”棋子可以打掉红方一枚棋子;

黑方“将”棋子打掉红方棋子之后判断黑方“将”棋子点位是否可以被红方棋子攻击;

斜线只是“士”的可行走路径。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
    Finish_read = 0;
    x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') {
            f = -1;
        }
        if (ch == EOF) {
            return;
        }
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    x *= f;
    Finish_read = 1;
};

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n, BlackGY, BlackGX;
    while (cin >> n >> BlackGY >> BlackGX) {
        if (!n && !BlackGY && !BlackGX) {
            break;
        }
        bool move[11][10];
        mem(move, 1);
        char pieces[11][10];
        mem(pieces, ' ');
        bool flag = 0;
        for (int i = 0; i < n; ++i) {
            char piece;
            int y, x;
            cin >> piece >> y >> x;
            pieces[y][x] = piece;
        }
        for (int i = 1; i <= 10; ++i) {
            for (int j = 1; j <= 9; ++j) {
                if (pieces[i][j] != ' ') {
                    if (pieces[i][j] == 'G' || pieces[i][j] == 'R') {
                        for (int k = i - 1; k > 0; --k) {
                            if (pieces[k][j] == ' ') {
                                move[k][j] = 0;
                            }
                            else {
                                move[k][j] = 0;
                                break;
                            }
                        }
                        for (int k = i + 1; k <= 10; ++k) {
                            if (pieces[k][j] == ' ') {
                                move[k][j] = 0;
                            }
                            else {
                                move[k][j] = 0;
                                break;
                            }
                        }
                        for (int k = j - 1; k > 0; --k) {
                            if (pieces[i][k] == ' ') {
                                move[i][k] = 0;
                            }
                            else {
                                move[i][k] = 0;
                                break;
                            }
                        }
                        for (int k = j + 1; k <= 9; ++k) {
                            if (pieces[i][k] == ' ') {
                                move[i][k] = 0;
                            }
                            else {
                                move[i][k] = 0;
                                break;
                            }
                        }
                    }
                    else if (pieces[i][j] == 'C') {
                        int cnt = 0;
                        for (int k = i - 1; k > 0; --k) {
                            if (pieces[k][j] == ' ') {
                                if (cnt) {
                                    move[k][j] = 0;
                                }
                            }
                            else {
                                if (!cnt) {
                                    cnt++;
                                }
                                else {
                                    move[k][j] = 0;
                                    break;
                                }
                            }
                        }
                        cnt = 0;
                        for (int k = i + 1; k <= 10; ++k) {
                            if (pieces[k][j] == ' ') {
                                if (cnt) {
                                    move[k][j] = 0;
                                }
                            }
                            else {
                                if (!cnt) {
                                    cnt++;
                                }
                                else {
                                    move[k][j] = 0;
                                    break;
                                }
                            }
                        }
                        cnt = 0;
                        for (int k = j - 1; k > 0; --k) {
                            if (pieces[i][k] == ' ') {
                                if (cnt) {
                                    move[i][k] = 0;
                                }
                            }
                            else {
                                if (!cnt) {
                                    cnt++;
                                }
                                else {
                                    move[i][k] = 0;
                                    break;
                                }
                            }
                        }
                        cnt = 0;
                        for (int k = j + 1; k <= 9; ++k) {
                            if (pieces[i][k] == ' ') {
                                if (cnt) {
                                    move[i][k] = 0;
                                }
                            }
                            else {
                                if (!cnt) {
                                    cnt++;
                                }
                                else {
                                    move[i][k] = 0;
                                    break;
                                }
                            }
                        }
                    }
                    else if (pieces[i][j] == 'H') {
                        if (i >= 3) {
                            if (pieces[i - 1][j] == ' ') {
                                if (j >= 2) {
                                    move[i - 2][j - 1] = 0;
                                }
                                if (j <= 8) {
                                    move[i - 2][j + 1] = 0;
                                }
                            }
                        }
                        if (j >= 3) {
                            if (pieces[i][j - 1] == ' ') {
                                if (i >= 2) {
                                    move[i - 1][j - 2] = 0;
                                }
                                if (i <= 9) {
                                    move[i + 1][j - 2] = 0;
                                }
                            }
                        }
                        if (i <= 8) {
                            if (pieces[i + 1][j] == ' ') {
                                if (j >= 2) {
                                    move[i + 2][j - 1] = 0;
                                }
                                if (j <= 8) {
                                    move[i + 2][j + 1] = 0;
                                }
                            }
                        }
                        if (j <= 7) {
                            if(pieces[i][j + 1] == ' ') {
                                if (i >= 2) {
                                    move[i - 1][j + 2] = 0;
                                }
                                if (i <= 9) {
                                    move[i + 1][j + 2] = 0;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (BlackGY == 1) {
            if (BlackGX == 4) {
                if (move[1][5] || move[2][4]) {
                    flag = 1;
                }
            }
            else if (BlackGX == 5) {
                if (move[1][4] || move[1][6] || move[2][5]) {
                    flag = 1;
                }
            }
            else if (BlackGX == 6) {
                if (move[1][5] || move[2][6]) {
                    flag = 1;
                }
            }
        }
        else if (BlackGY == 2) {
            if (BlackGX == 4) {
                if (move[1][4] || move[2][5] || move[3][4]) {
                    flag = 1;
                }
            }
            else if (BlackGX == 5) {
                if (move[1][5] || move[2][4] || move[2][6] || move[3][5]) {
                    flag = 1;
                }
            }
            else if (BlackGX == 6) {
                if (move[1][6] || move[2][5] || move[3][6]) {
                    flag = 1;
                }
            }
        }
        else if (BlackGY == 3) {
            if (BlackGX == 4) {
                if (move[2][4] || move[3][5]) {
                    flag = 1;
                }
            }
            else if (BlackGX == 5) {
                if (move[3][4] || move[2][5] || move[3][6]) {
                    flag = 1;
                }
            }
            else if (BlackGX == 6) {
                if (move[3][5] || move[2][6]) {
                    flag = 1;
                }
            }
        }
        bool ok = 1;
        for (int i = BlackGY + 1; i <= 10; ++i) {
            if (pieces[i][BlackGX] != ' ') {
                if (pieces[i][BlackGX] == 'G' && ok) {
                    flag = 1;
                }
                break;
            }
        }
        if (flag) {
            cout << "NO" << endl;
        }
        else {
            cout << "YES" << endl;
        }
    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Tony5t4rk/article/details/81435199
今日推荐