QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#657385 | #6621. Luggage Lock | daoqi# | TL | 1ms | 3848kb | C++20 | 1.6kb | 2024-10-19 14:40:33 | 2024-10-19 14:40:34 |
Judging History
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++) {
if (b[j] == s[j]) continue;
for (int k = j; k <= 4; k++) {
if (k == 4 || ((b[j] - s[j]) * (b[k] - s[k])) <= 0) {
auto ts = s, ts2 = s;
for (int i = j; i < k; i++) {
if (ts[i] == '0') ts[i] = '9';
else ts[i]--;
if (ts2[i] == '9') ts[i] = '0';
else ts2[i]++;
}
auto t2 = std::stoi(ts), t3 = std::stoi(ts2);
if (vis[t2] == -1 || vis[t2] > vis[t1] + 1) {
q.push(ts);
vis[t2] = vis[t1] + 1;
}
if (vis[t3] == -1 || vis[t3] > vis[t1] + 1) {
q.push(ts2);
vis[t3] = vis[t1] + 1;
}
}
}
}
}
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3848kb
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...