QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#638099 | #6739. Teleport | UESTC_DECAYALI# | AC ✓ | 956ms | 306656kb | C++20 | 3.2kb | 2024-10-13 14:54:16 | 2024-10-13 14:54:16 |
Judging History
answer
#include <bits/stdc++.h>
int n, k;
int dis[3][5000][5000];
std::string ms[5000];
std::deque<std::tuple<short, short, short>> q;
inline short get_id(short x, short y) { return std::min(x, y) * 2 + (y > x); }
inline std::pair<short, short> get_pth_next(short x, short y, short p) {
short fx = x, fy = y, bid = get_id(x, y);
x += p >> 1, y += p >> 1;
if(p & 1) std::swap(x, y), y++;
if(x < n && y < n) return { x, y };
if(fx < fy) std::swap(fx, fy), fy++;
short dt = n - 1 - fx;
fx += dt, fy += dt;
if(get_id(fx, fy) / k == bid / k) return { -1, -1 };
return { fx, fy };
}
inline std::pair<short, short> get_pth_prev(short x, short y, short p) {
x -= p >> 1, y -= p >> 1;
if(p & 1) std::swap(x, y), x--;
return { x, y };
}
void update(short type, std::pair<short, short> ss, int d, int delta) {
auto [nx, ny] = ss;
if(nx < 0 || nx >= n || ny < 0 || ny >= n) return ;
if(type == 0 && ms[nx][ny] == '*') return ;
if(d + delta >= dis[type][nx][ny]) return ;
dis[type][nx][ny] = d + delta;
if(delta == 1) q.emplace_back(type, nx, ny);
else q.emplace_front(type, nx, ny);
return ;
}
int hkr[10000];
std::ostream& operator <<(std::ostream &out, const std::pair<short, short> &p) {
return out << p.first << " " << p.second;
}
int main() {
std::ios::sync_with_stdio(false);
memset(dis, 0x3f, sizeof(dis));
std::cin >> n >> k;
// for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) std::cerr << get_id(i, j) << char(j == n - 1 ? 10 : 32);
// std::cout << get_pth_next(0, 2, 3) << char(10);
hkr[0] = 0;
for(int i = 1; i < 10000; ++i) hkr[i] = (hkr[i - 1] + 1) % k;
for(int i = 0; i < n; ++i) std::cin >> ms[i];
for(int i = 0; i < n; ++i) for(int j = 0; j < i; ++j) std::swap(ms[i][j], ms[j][i]);
q.push_back({0, 0, 0});
if(ms[0][0] != '*') dis[0][0][0] = 0;
while(q.size()) {
auto [type, x, y] = q.front(); q.pop_front();
short id = get_id(x, y);
int d = dis[type][x][y];
switch(type) {
case 0:
for(auto [dx, dy]: (short[4][2]){0, 1, 0, -1, 1, 0, -1, 0})
update(0, std::make_pair(x + dx, y + dy), d, 1);
update(1, get_pth_next(x, y, 0), d, 1);
update(2, get_pth_next(x, y, k), d, 1);
break;
case 1:
if(hkr[id] + 1 != k) update(1, get_pth_next(x, y, 1), d, 0);
update(0, std::make_pair(x, y), d, 0);
break;
case 2:
if(hkr[id] != 0) update(2, get_pth_prev(x, y, 1), d, 0);
update(0, std::make_pair(x, y), d, 0);
break;
default:
abort();
}
}
// for(int type = 0; type < 3; ++type) {
// for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j)
// std::cerr << (dis[type][i][j] >= 114514 ? "*" : std::format("{}", dis[type][i][j])) << char(j == n - 1 ? 10 : 32);
// std::cerr << "==================================\n";
// }
if(dis[0][n - 1][n - 1] > n * n * 3) std::cout << "-1\n";
else std::cout << dis[0][n - 1][n - 1] << char(10);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 11ms
memory: 296708kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 11ms
memory: 296840kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 97ms
memory: 297736kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 150ms
memory: 297832kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 103ms
memory: 297728kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 97ms
memory: 298068kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 103ms
memory: 298076kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 734ms
memory: 306484kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 835ms
memory: 305936kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 956ms
memory: 306656kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"