Submission #103334

#TimeUsernameProblemLanguageResultExecution timeMemory
103334luciocfPortals (BOI14_portals)C++14
70 / 100
205 ms2856 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 210; typedef pair<int, int> pii; typedef pair<int, pii> piii; int dist[maxn][maxn]; int prox[4][maxn][maxn]; int ox, oy, dx, dy, n, m; bool mark[maxn][maxn]; char tab[maxn][maxn]; int linha[] = {-1, 1, 0, 0}; int coluna[] = {0, 0, -1, 1}; void bfs(void) { for (int i = 0; i <= n+1; i++) for (int j = 0; j <= m+1; j++) dist[i][j] = 1e9+10; dist[ox][oy] = 0; priority_queue<piii, vector<piii>, greater<piii>> fila; fila.push({0, {ox, oy}}); while (!fila.empty()) { int x = fila.top().second.first, y = fila.top().second.second; fila.pop(); if (mark[x][y]) continue; mark[x][y] = true; for (int i = 0; i < 4; i++) { int a = x+linha[i], b = y+coluna[i]; if (a < 0 || a > n+1 || b < 0 || b > m+1 || tab[a][b] == '#') continue; if (dist[x][y]+1 < dist[a][b]) { dist[a][b] = dist[x][y]+1; fila.push({dist[a][b], {a, b}}); } } int d = 1e9+10; for (int i = prox[0][x][y]; i <= prox[1][x][y]; i++) d = min({d, abs(i-x)+abs(prox[2][x][y]-y), abs(i-x)+abs(prox[3][x][y]-y)}); for (int i = prox[2][x][y]; i <= prox[3][x][y]; i++) d = min({d, abs(i-y)+abs(prox[0][x][y]-x), abs(i-y)+abs(prox[1][x][y]-x)}); for (int a = x; a >= 1; a--) { if (tab[a-1][y] != '#') continue; if (dist[x][y]+d < dist[a][y]) { dist[a][y] = dist[x][y]+d; fila.push({dist[a][y], {a, y}}); } break; } for (int a = x; a <= n; a++) { if (tab[a+1][y] != '#') continue; if (dist[x][y]+d < dist[a][y]) { dist[a][y] = dist[x][y]+d; fila.push({dist[a][y], {a, y}}); } break; } for (int b = y; b >= 1; b--) { if (tab[x][b-1] != '#') continue; if (dist[x][y]+d < dist[x][b]) { dist[x][b] = dist[x][y]+d; fila.push({dist[x][b], {x, b}}); } break; } for (int b = y; b <= m; b++) { if (tab[x][b+1] != '#') continue; if (dist[x][y]+d < dist[x][b]) { dist[x][b] = dist[x][y]+d; fila.push({dist[x][b], {x, b}}); } break; } } } int main(void) { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf(" %c", &tab[i][j]); if (tab[i][j] == 'S') ox = i, oy = j; if (tab[i][j] == 'C') dx = i, dy = j; } } for (int i = 0; i <= n; i++) tab[i][0] = tab[i][m+1] = '#'; for (int j = 0; j <= m; j++) tab[0][j] = tab[n+1][j] = '#'; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { for (int a = i-1; a >= 0; a--) { if (tab[a][j] == '#') { prox[0][i][j] = a; break; } } for (int a = i+1; a <= n+1; a++) { if (tab[a][j] == '#') { prox[1][i][j] = a; break; } } for (int b = j-1; b >= 0; b--) { if (tab[i][b] == '#') { prox[2][i][j] = b; break; } } for (int b = j+1; b <= m+1; b++) { if (tab[i][b] == '#') { prox[3][i][j] = b; break; } } } } bfs(); printf("%d\n", dist[dx][dy]); }

Compilation message (stderr)

portals.cpp: In function 'int main()':
portals.cpp:117:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
portals.cpp:123:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf(" %c", &tab[i][j]);
    ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...