QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#129746 | #6739. Teleport | UndertrainedOverpressure# | TL | 83ms | 30244kb | C++23 | 3.4kb | 2023-07-22 22:55:32 | 2023-07-22 22:55:35 |
Judging History
answer
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
struct DSU {
vector<int> p;
vector<int> alive;
int n;
DSU(int n) : n(n) {
p.resize(n + 1);
iota(p.begin(), p.end(), 0);
alive.resize(n + 1, 1);
}
int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }
void kill(int x) {
if (alive[x]) {
alive[x] = 0;
p[x] = x + 1;
}
}
int get_next_alive(int x) {
x = get(x);
return x == n ? -1 : x;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<vector<char>> f(n, vector<char>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> f[i][j];
}
}
auto diagonal = [&](int x, int y) { return x - y + n - 1; };
int dcount = 2 * n - 1;
vector<vector<pair<int, int>>> diagonals(dcount);
vector<vector<int>> id(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int cd = diagonal(i, j);
diagonals[cd].emplace_back(i, j);
id[i][j] = diagonals[cd].size() - 1;
}
}
vector<DSU> dsu;
for (int i = 0; i < dcount; ++i) {
dsu.emplace_back(diagonals[i].size());
}
auto ok = [&](int x, int y) { return 0 <= x && x < n && 0 <= y && y < n; };
auto kill = [&](int x, int y) {
int cd = diagonal(x, y);
int cid = id[x][y];
dsu[cd].kill(cid);
};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (f[i][j] == '*') {
kill(i, j);
}
}
}
const int Inf = 1e9;
vector<vector<int>> d(n, vector<int>(n, Inf));
queue<pair<int, int>> q;
d[0][0] = 0;
q.push({0, 0});
kill(0, 0);
while (!q.empty()) {
auto [x, y] = q.front();
int cur_dist = d[x][y];
// cout << x << ' ' << y << ' ' << cur_dist << endl;
q.pop();
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
if (dx * dx + dy * dy == 1) {
int nx = x + dx;
int ny = y + dy;
if (ok(nx, ny) && d[nx][ny] == Inf && f[nx][ny] == '.') {
d[nx][ny] = cur_dist + 1;
q.push({nx, ny});
kill(nx, ny);
}
}
}
}
for (int tt = 0; tt <= 1; ++tt) {
int cx, cy, max_dist;
if (tt == 0) {
cx = x, cy = y;
max_dist = k / 2;
} else {
cx = y + 1, cy = x;
if (!ok(cx, cy)) {
continue;
}
max_dist = (k - 1) / 2;
}
int cd = diagonal(cx, cy);
int idx = id[cx][cy];
while (true) {
int nxt = dsu[cd].get_next_alive(idx);
if (nxt == -1) {
break;
}
int nd = abs(nxt - idx);
if (nd > max_dist) {
break;
}
int tx = diagonals[cd][nxt].first, ty = diagonals[cd][nxt].second;
assert(d[tx][ty] == Inf);
d[tx][ty] = cur_dist + 1;
q.push({tx, ty});
kill(tx, ty);
}
}
}
cout << (d[n - 1][n - 1] == Inf ? -1 : d[n - 1][n - 1]) << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3456kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3500kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 83ms
memory: 29120kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 73ms
memory: 29792kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 82ms
memory: 29380kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 77ms
memory: 29732kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 78ms
memory: 30244kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: -100
Time Limit Exceeded
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...