题解 | 小红的战争棋盘

小红的战争棋盘

https://www.nowcoder.com/practice/65fab5af902744c7bf13911d6adbdf36

巨型模拟题。

使用路径压缩合并并查集即可维护领土兼并功能。注意投放军队的时候也会产生冲突,但不输出任何信息。

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

using pii = pair<int, int>;

int n;
int m;
int k;
vector<string> names;
unordered_map<string, int> id;
vector<pii> pos;
vector<int> cntLand;

int q;

vector<int> p;

vector<vector<int>> grid;

int P(int a) {
    if (p[a] == a) {
        return a;
    }
    return p[a] = P(p[a]);
}

bool Cmp(int a, int b) {
    return cntLand[a] < cntLand[b] || cntLand[a] == cntLand[b] &&
           names[a] < names[b];
}

void Eat(int a, int b, bool show = true) {
    p[b] = a;
    id.erase(names[b]);
    if (show) {

        cntLand[a] += cntLand[b];
        cout << names[a] << " wins!\n";
        // cout << "eat " << names[b] << endl;
    }
}

void Battle(int a, int b, bool show = true) {
    if (Cmp(a, b)) {
        Eat(b, a, show);
        return;
    }
    Eat(a, b, show);
}

void Solve() {
    cin >> n >> m >> k;
    pos.resize(k);
    p.resize(k);
    names.resize(k);
    grid.assign(n, vector<int>(m, -1));
    cntLand.assign(k, 1);
    for (int i = 0; i < k; i++) {
        auto& str = names[i];
        auto& [x, y] = pos[i];
        cin >> str >> x >> y;
        x--;
        y--;
        id[str] = i;
        p[i] = i;
        if (grid[x][y] != -1) {
            Battle(P(grid[x][y]), i, false);
            continue;
        }
        grid[x][y] = i;
    }
    cin >> q;
    string str;
    char c;
    while (q--) {
        cin >> str >> c;
        auto it = id.find(str);
        if (it == id.end()) {
            cout << "unexisted empire.\n";
            continue;
        }
        int idx = it->second;
        auto [x, y] = pos[idx];
        switch (c) {
            case 'W':
                x--;
                break;
            case 'S':
                x++;
                break;
            case 'A':
                y--;
                break;
            default:
                y++;
                break;
        }
        if (x < 0 || x >= n || y < 0 || y >= m) {
            cout << "out of bounds!\n";
            continue;
        }
        pos[idx] = {x, y};
        int& t = grid[x][y];
        // cout << x << ',' << y << ',' << t << endl;
        if (t == -1) {
            t = idx;
            cntLand[idx]++;
            cout << "vanquish!\n";
            continue;
        }
        t = P(t);
        // cout << t << ',' << idx << endl;
        if (t == idx) {
            cout << "peaceful.\n";
            continue;
        }
        Battle(idx, t);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    Solve();
    return 0;
}

全部评论

相关推荐

关于“实习生工资多少才算正常”,其实并没有一个放之四海而皆准的标准,但如果结合一线城市的生活成本、工作强度以及实习本身创造的价值来看,我个人认为6000&nbsp;元左右应当是一个基本及格线,也就是每天&nbsp;200&nbsp;多元。如果能达到&nbsp;300、400&nbsp;元一天,甚至更高,那无疑是更理想的状态。首先,从现实成本看,房租、通勤、餐饮几乎都是刚性支出。低于这个水平的实习,往往意味着实习生需要用家庭或存款“倒贴”工作,这在长期来看并不合理。实习本质上是学习,但并不等于“廉价劳动力”,更不应该是经济压力的来源。其次,愿意给实习生更高薪资的公司,通常不会是差公司。这至少说明两点:一是公司资金相对充足,不是靠压缩人力成本勉强维持;二是公司认可实习生的价值,希望你真正参与业务、创造产出,而不是只做边角料工作。很多高薪实习往往伴随着更规范的培养体系、更高的信息密度和更真实的项目经验。当然,高工资并不等于一切,但它往往是一个重要信号。能给到&nbsp;300、400&nbsp;元一天甚至更多的公司,往往对效率、能力和长期发展更有追求,也更可能处在一个有前景的赛道中。总结来说,实习工资不仅是钱的问题,更是公司态度、实力和发展前景的体现。在条件允许的情况下,争取一份“付得起你时间”的实习,本身就是一种理性选择。
北国牛马:你是不是忘了你一周只能上五天班,月薪6000那你日薪就得300了,日薪200一个月也就4000,也就刚好覆盖生活成本了
实习生工资多少才算正常?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务