Submission #1127626

#TimeUsernameProblemLanguageResultExecution timeMemory
1127626ALTAKEXERobots (APIO13_robots)C++17
0 / 100
70 ms109384 KiB
#include <bits/stdc++.h> #define ll long long int using namespace std; const int INF = 1e9; const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; vector<string> s(500); vector<pair<int, int>> ind(10); vector<vector<vector<int>>> dp(10, vector<vector<int>>(10, vector<int>(500 * 500, INF))); bool ok(int x, int y, int w, int h) { return x >= 0 && x < h && y >= 0 && y < w && s[x][y] != 'x'; } vector<vector<int>> BFS(int w, int h) { vector<vector<int>> d(w * h, vector<int>(w * h, INF)); for (int i = 0; i < w * h; i++) { int sx = i / w, sy = i % w; if (s[sx][sy] == 'x') continue; queue<pair<int, int>> q; q.push({sx, sy}); d[i][i] = 0; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); int t = x * w + y; for (int k = 0; k < 4; k++) { int xx = x, yy = y, cnt = 0; while (ok(xx + dx[k], yy + dy[k], w, h)) { xx += dx[k]; yy += dy[k]; cnt++; if (d[i][xx * w + yy] > d[i][t] + 1) { d[i][xx * w + yy] = d[i][t] + 1; q.push({xx, yy}); } } } } } return d; } int main() { int n, w, h; cin >> n >> w >> h; for (int i = 0; i < h; i++) { cin >> s[i]; for (int j = 0; j < w; j++) { if (isdigit(s[i][j])) ind[s[i][j] - '0'] = {i, j}; } } vector<vector<int>> d = BFS(w, h); for (int i = 1; i <= n; ++i) dp[i][i][ind[i].first * w + ind[i].second] = 0; for (int len = 2; len <= n; ++len) { for (int l = 1; l + len - 1 <= n; ++l) { int r = l + len - 1; for (int i = 0; i < w * h; i++) { if (i / w >= h || i % w >= w || s[i / w][i % w] == 'x') continue; for (int k = l; k < r; k++) { for (int q = 0; q < w * h; q++) { for (int e = 0; e < w * h; e++) { if (d[q][i] == INF || d[e][i] == INF) continue; dp[l][r][i] = min(dp[l][r][i], dp[l][k][q] + dp[k + 1][r][e] + d[q][i] + d[e][i]); } } } } } } ll ans = INF; for (int i = 0; i < w * h; i++) ans = min(ans, (ll)dp[1][n][i]); cout << (ans == INF ? -1 : ans) << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...