QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#440177 | #6739. Teleport | suibian_xiaozhao | AC ✓ | 739ms | 83804kb | C++20 | 2.8kb | 2024-06-13 11:12:55 | 2024-06-13 11:12:56 |
Judging History
answer
#include<bits/stdc++.h>
using i64 = long long;
using namespace std;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
int f[5001 * 5001];
int find(int x) {
if (f[x] != x)
f[x] = find(f[x]);
return f[x];
// int t = x;
// while (f[x] != x) {
// x = f[x];
// }
// while (f[t] != t) {
// t = f[t];
// f[t] = x;
// }
// return x;
}
// void solve() {
// int n, k; cin >> n >> k;
// vector<string> s(n);
// for (int i = 0; i < n; ++i) cin >> s[i];
// vector dis(n, vector<int>(n, -1));
// queue<pair<int, int>> q;
// q.emplace(0, 0);
// dis[0][0] = 0;
// iota(f, f + (n + 1) * (n + 1), 0);
// while (!q.empty()) {
// auto [x, y] = q.front();
// q.pop();
// for (int k = 0; k < 4; ++k) {
// int nx = x + dx[k];
// int ny = y + dy[k];
// if (0 <= nx && nx < n && 0 <= ny && ny < n && dis[nx][ny] == -1 && s[nx][ny] == '.') {
// q.emplace(nx, ny);
// dis[nx][ny] = dis[x][y] + 1;
// }
// }
// while (1) {
// int p = find(x * (n + 1) + y);
// int nx = p / (n + 1), ny = p % (n + 1);
// if (nx == n || ny == n || nx + ny > x + y + k)
// break;
// if (dis[nx][ny] == -1 && s[nx][ny] == '.') {
// q.emplace(nx, ny);
// dis[nx][ny] = dis[x][y] + 1;
// }
// f[nx * (n + 1) + ny] = (ny + 1) * (n + 1) + nx;
// }
// }
// cout << dis[n - 1][n - 1] << '\n';
// }
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, k; cin >> n >> k;
vector<string> s(n);
for (int i = 0; i < n; ++i) cin >> s[i];
vector dis(n, vector<int>(n, -1));
queue<pair<int, int>> q;
q.emplace(0, 0);
dis[0][0] = 0;
iota(f, f + (n + 1) * (n + 1), 0);
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k];
int ny = y + dy[k];
if (0 <= nx && nx < n && 0 <= ny && ny < n && dis[nx][ny] == -1 && s[nx][ny] == '.') {
q.emplace(nx, ny);
dis[nx][ny] = dis[x][y] + 1;
}
}
while (1) {
int p = find(x * (n + 1) + y);
int nx = p / (n + 1), ny = p % (n + 1);
if (nx == n || ny == n || nx + ny > x + y + k)
break;
if (dis[nx][ny] == -1 && s[nx][ny] == '.') {
q.emplace(nx, ny);
dis[nx][ny] = dis[x][y] + 1;
}
f[nx * (n + 1) + ny] = (ny + 1) * (n + 1) + nx;
}
}
cout << dis[n - 1][n - 1] << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3612kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 50ms
memory: 13144kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 51ms
memory: 13304kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 60ms
memory: 12968kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 57ms
memory: 12616kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 64ms
memory: 12876kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 446ms
memory: 83804kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 658ms
memory: 78324kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 739ms
memory: 82680kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"