QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#560609 | #8236. Snake Move | shabi666# | RE | 0ms | 13460kb | C++20 | 2.2kb | 2024-09-12 16:53:59 | 2024-09-12 16:54:00 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e3 + 5;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
char grid[N][N];
bool vis[N][N];
unsigned int dis[N][N];
map<pair<int, int>, int> idx;
void solve() {
memset(dis, 0x3f, sizeof dis);
int n, m, k;
cin >> n >> m >> k;
int sx, sy;
for (int i = 1; i <= k; i++) {
int x, y;
cin >> x >> y;
idx[{x, y}] = i;
if (i == 1) {
sx = x;
sy = y;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> grid[i][j];
}
}
auto invalid = [&](int x, int y) {
return x < 1 || x > n || y < 1 || y > m;
};
priority_queue<tuple<int, int, int, int>> q;
q.emplace(0, sx, sy, k);
dis[sx][sy] = 0;
while (!q.empty()) {
auto [d, x, y, len] = q.top(); q.pop();
if (vis[x][y])
continue;
vis[x][y] = true;
for (int i = 0; i < 4; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if (invalid(xx, yy) || grid[xx][yy] == '#')
continue;
if (idx.count({xx, yy}) && idx[{xx, yy}] <= len) {
int d = idx[{xx, yy}];
if (dis[xx][yy] > dis[x][y] + len - d + 1) {
dis[xx][yy] = dis[x][y] + len - d + 1;
q.emplace(-dis[xx][yy], xx, yy, max(0ll, len - d));
}
}
else if (dis[xx][yy] > dis[x][y] + 1) {
dis[xx][yy] = dis[x][y] + 1;
q.emplace(-dis[xx][yy], xx, yy, max(0ll, len - 1));
}
}
}
unsigned int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (dis[i][j] != 0x3f3f3f3f3f3f3f3f)
ans += dis[i][j] * dis[i][j];
}
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 13264kb
input:
4 5 5 3 5 3 4 3 3 3 2 4 2 ..... ..... ..... .....
output:
293
result:
ok single line: '293'
Test #2:
score: 0
Accepted
time: 0ms
memory: 11496kb
input:
2 2 4 1 1 1 2 2 2 2 1 .. ..
output:
14
result:
ok single line: '14'
Test #3:
score: 0
Accepted
time: 0ms
memory: 13460kb
input:
5 5 3 1 2 1 1 2 1 ..... .###. .#.#. .###. .....
output:
407
result:
ok single line: '407'
Test #4:
score: -100
Runtime Error
input:
3000 2900 1 1882 526 ........................................................................................................#................................................................................................................................................................#................