UVA1589 象棋 Xiangqi

紫书第一道绿题,但是就是那个特判我不是很理解

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

struct student { //储存信息
    char ch;
    int x, y;
} stu[15];

int n; //函数里面要用到设成全局变量
int dx[4] = {-1, 0, 1, 0}; //将的方向数组,同时也是蹩马脚的方向数组,顺时针,上右下左
int dy[4] = {0, 1, 0, -1};
int ddx[4][2] = {-2, -2, -1, 1, 2, 2, 1, -1}; //马可以管的地方,顺时针,上右下左
int ddy[4][2] = {-1, 1, 2, 2, 1, -1, -2, -2};

int count(int x1, int y1, int x2, int y2) { //返回两个棋子之间有几个棋子
    int cnt = 0;
    if (x1 == x2) { //两个棋子在一行
        if (y1 > y2) swap(y1, y2); //因为是从小往大遍历,所以这里判断一下
        for (int i = y1 + 1; i <= y2 - 1; i++) { //遍历两个棋子中间的位置
            for (int j = 0; j < n; j++) { //遍历所有的红方棋子
                if (stu[j].x == x1 && stu[j].y == i) cnt++; //如果存在棋子的位置和两子中间的位置相等,累加
            }
        }
    } else { //两个棋子在一列
        if (x1 > x2) swap(x1, x2); //同上
        for (int i = x1 + 1; i <= x2 - 1; i++) { //遍历两个棋子中间的位置
            for (int j = 0; j < n; j++) { //遍历所有棋子
                if (stu[j].x == i && stu[j].y == y1) cnt++;
            }
        }
    }
    return cnt;
}

int main() {
    int x, y;
    while (cin >> n >> x >> y && n) {
        map<pa, int> mp;
        int xxx = 0, gx;
        for (int i = 0; i < n; i++) {
            cin >> stu[i].ch >> stu[i].x >> stu[i].y;
            mp[mk(stu[i].x, stu[i].y)] = 1; //表示这个位置已经有棋子了
            if (stu[i].ch == 'G' && stu[i].y == y) { xxx = 1; gx = stu[i].x; } //将帅在一列,但是要出循环操作,因为可能后面输入会放在两者中间
        }
        if (xxx && !count(x, y, gx, y)) { cout << "NO" << endl; continue; } //如果将帅在一列,同时中间没有别的棋子,则照面
        int ok = 1;
        for (int i = 0; i < 4; i++) { //遍历将可能走的四个位置
            int xx = x + dx[i]; //将可能走的位置
            int yy = y + dy[i];
            if (!xx || xx == 4 || yy == 3 || yy == 7) continue; //将不能出这几个位置,
            int ook = 0; //这个位置没有一个将军,则为零,至少有一个将军,则为1
            for (int j = 0; j < n; j++) {
                if (xx == stu[j].x && yy == stu[j].y) continue; //将的位置和红方相同,说明将吃掉了红方,跳过
                if (stu[j].ch == 'G') { //帅
                    if (stu[j].y == yy && !count(xx, yy, stu[j].x, stu[j].y)) ook = 1; //将帅同一列中间没有棋子,照面,将军
                } else if (stu[j].ch == 'R') { //车
                    if ((stu[j].y == yy || stu[j].x == xx) && !count(xx, yy, stu[j].x, stu[j].y)) ook = 1; //将和车同一列或者同一行,中间没有棋子,将军
                } else if (stu[j].ch == 'C') { //炮
                    if ((stu[j].y == yy || stu[j].x == xx) && count(xx, yy, stu[j].x, stu[j].y) == 1) ook = 1; //将和炮同一列或者同一行,中间有一个棋子,将军
                } else {
                    for (int k = 0; k < 4; k++) { //遍历蹩马脚的位置,当前棋子位置加上方向数组就是蹩马脚的位置
                        if (mp.find(mk(stu[j].x + dx[k], stu[j].y + dy[k])) != mp.end()) continue; //如果蹩马脚的位置有棋子,那么跳过,马不起作用
                        if (xx == stu[j].x + ddx[k][0] && yy == stu[j].y + ddy[k][0]) ook = 1; //马起作用,将在马能管的位置
                        if (xx == stu[j].x + ddx[k][1] && yy == stu[j].y + ddy[k][1]) ook = 1; //每个马脚对应两个马能管的位置,所以再写一个
                    }
                }
            }
            if (!ook) ok = 0; //标志为0,说明这个位置没有一个棋子将军,那么总的就是没有将死
        }
        cout << (ok ? "YES" : "NO") << endl; //输出结果
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值