QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#165039 | #6739. Teleport | xaphoenix# | AC ✓ | 750ms | 285092kb | C++14 | 3.2kb | 2023-09-05 15:37:03 | 2023-09-05 15:37:04 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define LC k<<1
#define RC k<<1|1
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repn(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = (n) - 1; i >= a; i--)
#define pern(i, a, n) for (int i = n; i >= a; i--)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef pair<ull, ull> PUU;
typedef pair<LL, LL> PLL;
const int N = 5100;
const int M = N * N;
const int mod = 1e9+7;
const int inf = (int)1e9;
const LL INF = 1e18;
const double eps = 1e-9;
mt19937_64 Rand((unsigned long long)new char);
#define rand Rand
int n, k, k1, k2, a[N][N], f1[M], f2[M], dis[N][N];
int find(int x, int f[]) {
return f[x] == x ? x: f[x] = find(f[x], f);
}
int cal(int x, int y) {
return (x - 1) * n + y;
}
PII cal2(int id) {
if (id % n == 0) return mp(id / n, n);
return mp(id / n + 1, id % n);
}
PII que[M];
int head, tail;
const int wayx[4] = {-1, 1, 0, 0};
const int wayy[4] = {0, 0, -1, 1};
int check(int x, int y) {
if (x < 1 || x > n || y < 1 || y > n) return 0;
if (dis[x][y] != -1 || a[x][y] == 0) return 0;
return 1;
}
int main() {
IO;
cin >> n >> k;
k1 = k / 2, k2 = (k + 1) / 2;
repn(i, 1, n) {
string s;
cin >> s;
repn(j, 1, n) {
if (s[j - 1] == '.') a[i][j] = 1;
else a[i][j] = 0;
int idx = cal(i, j);
f1[idx] = f2[idx] = idx;
}
}
int lim = cal(n + 1, n + 1);
f1[lim] = f2[lim] = lim;
memset(dis, -1, sizeof(dis));
que[head = tail = 1] = mp(1, 1);
dis[1][1] = 0;
while (head <= tail) {
auto now = que[head++];
int x = now.fi, y = now.se, w = dis[x][y];;
rep(i, 0, 4) {
int nx = x + wayx[i], ny = y + wayy[i];
if (check(nx, ny)) {
dis[nx][ny] = w + 1;
que[++tail] = mp(nx, ny);
}
}
// f1
int delta = min(k1, min(n - x, n - y));
int sx = x + 1, sy = y + 1, ex = x + delta, ey = y + delta;
int id = cal(sx, sy), idx = cal(ex, ey);
while (id <= idx) {
id = find(id, f1);
if (id > idx) break;
auto p = cal2(id);
int nx = p.fi, ny = p.se;
if (check(nx, ny)) {
dis[nx][ny] = w + 1;
que[++tail] = mp(nx, ny);
}
if (nx + 1 > n || ny + 1 > n) f1[id] = cal(n + 1, n + 1), id = f1[id];
else f1[id] = cal(nx + 1, ny + 1), id = f1[id];
}
// f2
delta = min(k2, min(n - y, n - x + 1));
sx = y + 1, sy = x, ex = y + delta, ey = x + delta - 1;
id = cal(sx, sy), idx = cal(ex, ey);
while (id <= idx) {
id = find(id, f2);
if (id > idx) break;
auto p = cal2(id);
int nx = p.fi, ny = p.se;
if (check(nx, ny)) {
dis[nx][ny] = w + 1;
que[++tail] = mp(nx, ny);
}
if (nx + 1 > n || ny + 1 > n) f2[id] = cal(n + 1, n + 1), id = f2[id];
else f2[id] = cal(nx + 1, ny + 1), id = f2[id];
}
}
cout << dis[n][n] << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 109988kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 3ms
memory: 112028kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 53ms
memory: 142132kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 64ms
memory: 143464kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 83ms
memory: 143840kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 65ms
memory: 142816kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 72ms
memory: 142276kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 335ms
memory: 282004kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 553ms
memory: 275260kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 750ms
memory: 285092kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"