acwing -- 3370. 牛年

 大模拟,本题我们可以唯一确定每头牛的相对年龄。

若无法确定牛的相对年龄,可以用图论进行遍历。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<unordered_map>

using namespace std;

unordered_map<string, int> age, ord = {{"Ox", 0}, {"Tiger",  1}, 
    {"Rabbit", 2}, {"Dragon", 3}, {"Snake", 4}, {"Horse", 5}, 
        {"Goat", 6}, {"Monkey", 7}, {"Rooster", 8}, {"Dog", 9}, 
            {"Pig", 10}, {"Rat", 11}};

unordered_map<string, string> na;

int main() {
    int n;
    cin >> n;
    na["Bessie"] = "Ox";
    age["Bessie"] = 0;
    
    string a, b, op, tp, yr;
    while(n -- ) {
        cin >> a >> tp >> tp >> op >> yr >> tp >> tp >> b;
        na[a] = yr;
        if(na[a] == na[b]) {
            age[a] = (op == "previous" ? age[b] - 12: age[a] + 12);
        } else {
            age[a] = (op == "previous" ? age[b] - (ord[na[b]] - ord[yr] + 12) % 12: age[b] + (ord[yr] - ord[na[b]] + 12) % 12);
        }
    }
    
     cout << abs(age["Elsie"]) << endl;
    
    return 0;
}

 

posted @ 2023-07-11 11:20  深渊之巅  阅读(5)  评论(0编辑  收藏  举报