QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#657271#6621. Luggage Lockdaoqi#TL 2ms3960kbC++201.5kb2024-10-19 14:29:112024-10-19 14:29:15

Judging History

你现在查看的是最新测评结果

  • [2024-10-19 14:29:15]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3960kb
  • [2024-10-19 14:29:11]
  • 提交

answer

#include<bits/stdc++.h>

using i64 = long long;

void DAOQI() {
    std::string a, b;
    std::cin >> a >> b;
    std::vector<int> vis(10000, -1);
    vis[std::stoi(a)] = 0;
    std::queue<std::string> q;
    q.push(a);
    while (!q.empty()) {
        auto s = q.front();
        q.pop();
        auto t1 = std::stoi(s);
        if (s == b) {
            std::cout << vis[t1] << "\n";
            return;
        }
        for (int j = 0; j < 4; j++) {
            for (int k = j; k < 4; k++) {
                auto t = s;
                for (int o = j; o <= k; o++) {
                    if (t[o] < '9') t[o]++;
                    else t[o] = '0';
                }
                auto tt = std::stoi(t);
                if (vis[tt] == -1 || vis[tt] > vis[t1] + 1) {
                    vis[tt] = vis[t1] + 1;
                    q.push(t);
                }
                t = s;
                for (int o = j; o <= k; o++) {
                    if (t[o] > '0') t[o]--;
                    else t[o] = '9';
                }
                tt = std::stoi(t);
                if (vis[tt] == -1 || vis[tt] > vis[t1] + 1) {
                    vis[tt] = vis[t1] + 1;
                    q.push(t);
                }
            }
        }
    }
    std::cout << vis[std::stoi(b)] << "\n";
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int T = 1;
    std::cin >> T;
    while (T--) DAOQI();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3960kb

input:

6
1234 2345
1234 0123
1234 2267
1234 3401
1234 1344
1234 2468

output:

1
1
4
5
1
4

result:

ok 6 numbers

Test #2:

score: -100
Time Limit Exceeded

input:

100000
9138 9251
0887 4577
2745 6294
6883 1197
8654 2502
7867 7941
7505 2717
1497 7726
3337 8042
2767 8782
5311 4368
9711 6229
7105 8101
3553 6673
6544 7940
8848 8920
1458 6315
4605 2931
5505 7310
5963 4765
7625 0418
4049 0136
2078 6899
3339 1080
2276 6814
4249 4182
1861 0824
8975 8926
6447 6965
226...

output:


result: