首页 > 其他分享 >题解 LGP5380【[THUPC2019]鸭棋】

题解 LGP5380【[THUPC2019]鸭棋】

时间:2022-11-21 18:12:59浏览次数:78  
标签:cnt return int 题解 void 鸭棋 bool chess LGP5380

posted on 2021-06-01 13:27:59 | under 题解 | source

给一种船新的做法,存棋子的位置而不是棋盘,我们只需要写一个生成棋子能移动到哪些位置的函数就可以了。

#include <string>
#include <vector>
#include <iostream>
using namespace std;
#define inv cout<<"Invalid command"<<endl
template<class T> struct vec {
    vector<T> a;
    int cnt;
    vec() {
        cnt = 0;
    }
    void clear() {
        cnt = 0, a.clear();
    }
    int size() {
        return cnt;
    }
    bool empty() {
        return cnt == 0;
    }
    void pub(T x) {
        a.push_back(x), cnt++;
    }
    T &operator[](int x) {
        return a[x - 1];
    }
    int find(T x) {
        for (int i = 0; i < cnt; i++)
            if (a[i] == x)
                return i + 1;

        return -1;
    }
};
int n = 10, m = 9, gameover = 0, turn = 1;
struct dot {
    int x, y;
    dot(int x, int y): x(x), y(y) {}
    bool operator==(dot b) {
        return x == b.x && y == b.y;
    }
};
struct chess {
    string name, id;
    int x, y;
    vec<dot> pos;
    chess(string a, string b, int c, int d) {
        name = a, id = b, x = c, y = d;
    }
    chess(int c, int d) {
        x = c, y = d;
    }
    bool operator==(chess b) {
        return x == b.x && y == b.y;
    }
    bool check(int, int);
    bool check2(int, int);
    bool check3(int, int);
    void getmovepos();
    void opt() {
        cout << id << " " << name << ";";
    }
};
vec<chess> a;
bool chess::check(int x, int y) { //越界
    return 0 <= x && x < n && 0 <= y && y < m;
}
bool chess::check2(int x, int y) { //任意一方都不行
    if (!check(x, y))
        return 0;

    int tmp = a.find(chess(x, y));
    return !~tmp;
}
bool chess::check3(int x, int y) { //可以踩对面
    if (!check(x, y))
        return 0;

    int tmp = a.find(chess(x, y));
    return !~tmp || a[tmp].id != id;
}
const int Dx[] = {0, -1, 0, 0, 1, -1, -1, 1, 1},
                 Dy[] = {0, 0, -1, 1, 0, -1, 1, -1, 1};
void chess::getmovepos() {
    pos.clear();

    if (name == "captain") {
        for (int i = 1; i <= 4; i++) {
            if (check3(x + Dx[i], y + Dy[i]))
                pos.pub(dot(x + Dx[i], y + Dy[i]));
        }
    } else if (name == "guard") {
        for (int i = 5; i <= 8; i++) {
            if (check3(x + Dx[i], y + Dy[i]))
                pos.pub(dot(x + Dx[i], y + Dy[i]));
        }
    } else if (name == "elephant") {
        for (int i = -1; i <= 1; i++)
            for (int j = -1; j <= 1; j++)
                if (i && j) {
                    if (check2(x + i, y + j) && check3(x + i * 2, y + j * 2))
                        pos.pub(dot(x + i * 2, y + j * 2));
                }
    } else if (name == "horse") {
        for (int i = -1; i <= 1; i++)
            for (int j = -1; j <= 1; j++)
                if (i && j) {
                    if (check2(x + i, y) && check3(x + i * 2, y + j))
                        pos.pub(dot(x + i * 2, y + j));

                    if (check2(x, y + j) && check3(x + i, y + j * 2))
                        pos.pub(dot(x + i, y + j * 2));
                }
    } else if (name == "car") {
        for (int i = 1; i <= 4; i++) {
            int tmpx = x + Dx[i], tmpy = y + Dy[i];

            while (check2(tmpx, tmpy)) {
                pos.pub(dot(tmpx, tmpy));
                tmpx += Dx[i], tmpy += Dy[i];
            }

            if (check3(tmpx, tmpy))
                pos.pub(dot(tmpx, tmpy));
        }
    } else if (name == "duck") {
        for (int i = -1; i <= 1; i++)
            for (int j = -1; j <= 1; j++)
                if (i && j) {
                    if (check2(x + i, y) && check2(x + i * 2, y + j) && check3(x + i * 3, y + j * 2))
                        pos.pub(dot(x + i * 3, y + j * 2));

                    if (check2(x, y + j) && check2(x + i, y + j * 2) && check3(x + i * 2, y + j * 3))
                        pos.pub(dot(x + i * 2, y + j * 3));
                }
    } else if (name == "soldier") {
        for (int i = 1; i <= 8; i++) {
            if (check3(x + Dx[i], y + Dy[i]))
                pos.pub(dot(x + Dx[i], y + Dy[i]));
        }
    }
}
void init() {
    a.pub(chess("captain", "red", 0, 4));
    a.pub(chess("captain", "blue", 9, 4));

    a.pub(chess("car", "red", 0, 0));
    a.pub(chess("horse", "red", 0, 1));
    a.pub(chess("elephant", "red", 0, 2));
    a.pub(chess("guard", "red", 0, 3));
    //
    a.pub(chess("guard", "red", 0, 5));
    a.pub(chess("elephant", "red", 0, 6));
    a.pub(chess("horse", "red", 0, 7));
    a.pub(chess("car", "red", 0, 8));
    a.pub(chess("duck", "red", 2, 0));
    a.pub(chess("duck", "red", 2, 8));
    a.pub(chess("soldier", "red", 3, 0));
    a.pub(chess("soldier", "red", 3, 2));
    a.pub(chess("soldier", "red", 3, 4));
    a.pub(chess("soldier", "red", 3, 6));
    a.pub(chess("soldier", "red", 3, 8));

    a.pub(chess("car", "blue", 9, 0));
    a.pub(chess("horse", "blue", 9, 1));
    a.pub(chess("elephant", "blue", 9, 2));
    a.pub(chess("guard", "blue", 9, 3));
    //
    a.pub(chess("guard", "blue", 9, 5));
    a.pub(chess("elephant", "blue", 9, 6));
    a.pub(chess("horse", "blue", 9, 7));
    a.pub(chess("car", "blue", 9, 8));
    a.pub(chess("duck", "blue", 7, 0));
    a.pub(chess("duck", "blue", 7, 8));
    a.pub(chess("soldier", "blue", 6, 0));
    a.pub(chess("soldier", "blue", 6, 2));
    a.pub(chess("soldier", "blue", 6, 4));
    a.pub(chess("soldier", "blue", 6, 6));
    a.pub(chess("soldier", "blue", 6, 8));
}
const string Turns[] = {"?", "red", "blue"};
int mian(int x1, int y1, int x2, int y2) {
    if (gameover)
        return inv, 0;

    int tmp = a.find(chess(x1, y1));

    if (!~tmp || a[tmp].id != Turns[turn])
        return inv, 0;

    a[tmp].getmovepos();

    if (!~a[tmp].pos.find(dot(x2, y2)))
        return inv, 0;

    a[tmp].opt();

    int to = a.find(chess(x2, y2));

    if (!~to)
        cout << "NA" << ";";
    else {
        if (to == 1 || to == 2)
            gameover = 1;

        a[to].opt();
        a[to].x = 114514, a[to].y = 1919810;
    }

    a[tmp].x = x2, a[tmp].y = y2;

    for (int i = 1; i <= a.cnt; i++)
        a[i].getmovepos();

    bool flag = 0;

    for (int i = 1; i <= a.cnt; i++)
        flag |= !!~a[i].pos.find(dot(a[1].x, a[1].y));

    for (int i = 1; i <= a.cnt; i++)
        flag |= !!~a[i].pos.find(dot(a[2].x, a[2].y));

    if (flag)
        cout << "yes" << ";";
    else
        cout << "no" << ";";

    if (gameover)
        cout << "yes" << endl;
    else
        cout << "no" << endl;

    turn ^= 3;
    return 0;
}
int main() {
    init();
    int q, x1, y1, x2, y2;
    cin >> q;

    while (q--) {
        cin >> x1 >> y1 >> x2 >> y2;
        mian(x1, y1, x2, y2);
    }

    return 0;
}

标签:cnt,return,int,题解,void,鸭棋,bool,chess,LGP5380
From: https://www.cnblogs.com/caijianhong/p/solution-P5380.html

相关文章

  • ARC104F Visibility Sequence 题解
    [ARC104F]VisibilitySequence给一个长为\(n\)的数列\(h_{1\dotsn}\),第\(i\)项在\([1,h_i]\)中选一个数,得到数列\(x_{1\dotsn}\)。再构造一个数列\(p_{1\do......
  • AT_arc126_d [ARC126D] Pure Straight 题解
    又来给do_while_true大佬交作业了,所以本篇题解仅仅是对该篇题解进行补充说明的。本篇题解适用于像我这样的小萌新,那篇题解适合大佬食用。Part1:为什么我们要用状压d......
  • K8S kube-scheduler-master CreateContainerError 问题解决及思路
    错误信息1:kubectlgetpods  发现pod状态一直在runing-error-CrashLoopBackOff-循环解决方法:1,查看日志。kubectllogspodsweb-674477549d-zx8gmkubectldescri......
  • Python字符串的encode与decode研究心得乱码问题解决方法(转)
    ​​Python字符串的encode与decode研究心得乱码问题解决方法(转)​​为什么会报错“UnicodeEncodeError:'ascii'codeccan'tencodecharactersinposition0-1:o......
  • ABC278 整合题解
    AA题,送分题。link。思路数据范围很小,其实直接模拟也是可以通过的。不过我们很容易想到\(O(n)\)的算法。对于前\(k\)个数,不输出,其他数正常输出。然后再在末尾......
  • ABC278F 题解
    前言题目传送门!或许更好的阅读体验?博弈论,状压,记忆化搜索。思路看到很小的\(n\),容易联想到状压、搜索。本题就是状压加搜索。设状态\(x\)的每一位表示:如果第\(i\)......
  • [ARC152D] Halftree题解
    很好的一道题,即使是我这种菜鸡也感到心潮澎湃。直觉有余,证明不足。思路有余,推导不足。无论是什么比赛,对拍都是最有效的查错方式。本篇题解里的所有图片采用gra......
  • ABC278D 题解
    前言题目传送门!更好的阅读体验?难度加强版:P1253。思路很容易想到线段树。维护\(cov_i\)表示覆盖的懒标记。单点加与单点查都非常简单。全局覆盖只需要给每一层都打......
  • CF1383E Strange Operation 题解
    linkSolutionshaber题,但是又没有做出来。我们发现这个变化相当于可以任意删掉\(0\),\(1\)的话只有与\(1\)相邻的时候可以删掉。那么相当于我们可以把一段包含\(1\)......
  • [题解] Uoj Easy Round 11
    A使用动态规划,\(f_{i,j}\)代表竖着的前\(i\)个,第\(i\)个被第\(j\)个横着的挡住的方案数,当然前提是有\(l_j\gei\),否则\(f_{i,j}=0\)。转移就是做一遍前缀和。考......