跳到正文

题解

P5380 [THUPC2019] 鸭棋 题解

Qrebla约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
#define INV {cout<<"Invalid command\n";continue;}
#define ok (g[X][Y].second != 0 && (g[x][y].first == g[X][Y].first))
#define lx abs(x - X)
#define ly abs(y - Y)
#define dxd ((X - x) / abs(X - x))
#define dyd ((Y - y) / abs(Y - y))
using namespace std;

pair<bool, int> g[10][9]; //地图
string s[10]; //名字
bool END = false; //结束
pair<int, int> capt[2]; //王位置

void init() { //初始化
    capt[0] = {9, 4};
    capt[1] = {0, 4};
    for(int i=0; i<10; i++)
        for(int j=0; j<9; j++) {
            g[i][j] = {0, 0};
            if(i == 0 || i == 9) g[i][j] = {(i<=4), abs(j-4) + 1};
            else if((i == 2 || i == 7) && (j == 0 || j == 8)) g[i][j] = {(i<=4), 6};
            else if((i == 3 || i == 6) && j % 2 == 0) g[i][j] = {(i<=4), 7};
        }
    s[0] = "blue";
    s[1] = "red";
    s[2] = "captain";
    s[3] = "guard";
    s[4] = "elephant";
    s[5] = "horse";
    s[6] = "car";
    s[7] = "duck";
    s[8] = "soldier";
}

bool oom(int x, int y) {return (x < 0 || x > 9 || y < 0 || y > 8);} //出地图

bool move(int x ,int y, int X, int Y) { //判断能否到达
    if(END) return false;
    if(oom(x, y) || oom(X, Y)) return false;
    if(!g[x][y].second) return false; //没棋
    if(!lx && !ly) return false; //没动
    if(ok) return false; //同色
    switch (g[x][y].second) { //对每种棋子判断能否到达
        case 1: // captain
            return (lx + ly == 1);
            break;
        case 2 ://guard
            return (lx == 1 && ly == 1);
            break;
        case 3: //elephant
            return (ly == 2 && lx == 2 && g[x+dxd][y+dyd].second == 0);
            break;
        case 4: //horse
            if((lx == 2 && ly == 1)||(ly == 2 && lx == 1)) return (!g[X-dxd][Y-dyd].second);
            return false;
            break;
        case 5: // car
            if(lx > 0 && ly > 0) return false;
            if(!lx) for(int i=y+dyd; i!=Y; i+=dyd) if(g[x][i].second != 0) return false;
            else for(int i=x+dxd; i!=X; i+=dxd) if(g[i][y].second != 0) return false;
            return true;
            break;
        case 6: // duck
            if((lx == 3 && ly == 2) || (lx == 2 && ly == 3))
                return (!g[X-dxd][Y-dyd].second && !g[X-2*dxd][Y-2*dyd].second);
            return false;
            break;
        default: // soldier
            return (lx <= 1 && ly <= 1);
            break;
    }
    return false;
}

bool capture(bool si) { //判断将军
    for(int i=0; i<10; i++)
      for(int j=0; j<9; j++)
        if((g[i][j].second != 0) && (g[i][j].first == si) &&
            move(i, j, capt[!si].first, capt[!si].second)) return true;
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T; cin >> T;
    init();
    bool now = 1;
    while(T--) {
        int x, y, X, Y;
        cin >> x >> y >> X >> Y;
        if(now != g[x][y].first || END) INV //顺序不正确
        if(!move(x, y, X, Y)) INV
        now ^= 1;
        bool si = g[x][y].first;
        if(g[x][y].second == 1) capt[si] = {X, Y};
        if(g[X][Y].second == 1) END = true;
        cout << s[si] << " " << s[g[x][y].second+1] <<";";
        if(g[X][Y].second == 0) {
            swap(g[x][y], g[X][Y]);
            cout << "NA;";
        } else {
            cout << s[g[X][Y].first] << " " << s[g[X][Y].second+1] <<";";
            g[X][Y] = g[x][y];
            g[x][y] = {false, 0};
        }
        if(capture(0) || capture(1)) cout << "yes;";
        else cout << "no;";
        if(END) cout << "yes\n";
        else cout << "no\n";
    }
    return 0;
}
原文