Submission #69120

#TimeUsernameProblemLanguageResultExecution timeMemory
69120MatheusLealVPortals (BOI14_portals)C++17
70 / 100
1101 ms139464 KiB
#include <bits/stdc++.h> #define N 1050 #define f first #define s second using namespace std; typedef pair<int, int> pii; int n, m, xo, yo, xi, yi, dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1}; int id[N][N], cnt, dist[N*N], pai[N*N]; pii O[N]; char mat[N][N]; vector<pii> grafo[N*N]; int solve(int ini, int fim) { priority_queue<pii, vector<pii>, greater<pii> > fila; memset(dist, 0x3f3f3f3f, sizeof dist); dist[ini] = 0; fila.push({0, ini}); while(!fila.empty()) { int x = fila.top().s, d = fila.top().s; fila.pop(); for(auto v: grafo[x]) { if(dist[v.f] > dist[x] + v.s) { dist[v.f] = dist[x] + v.s; pai[v.f] = x; fila.push({dist[v.f], v.f}); } } } int u = fim; // while(true) // { // int x = O[u].f, y = O[u].s; // cout<<"at position "<<x<<" "<<y<<"\n"; // if(u == ini) break; // u = pai[u]; // } return dist[fim]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>m; for(int i = 0; i <= n + 1; i++) { for(int j = 0; j <= m + 1; j++) { id[i][j] = ++ cnt; O[id[i][j]] = {i, j}; } } for(int i = 0; i <= n + 1; i++) for(int j = 0; j <= m + 1; j++) { if(!i or i == n + 1 or !j or j == m + 1) { mat[i][j] = '#'; continue; } cin>>mat[i][j]; if(mat[i][j] == 'S') xo = i, yo = j; if(mat[i][j] == 'C') xi = i, yi = j; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { int x = id[i][j]; if(mat[i][j] != '#') { for(int p = 0; p < 4; p++) { int a = i + dx[p], b = j + dy[p]; if(a < 0 or b < 0 or a > n + 1 or b > m + 1) continue; if(mat[a][b] != '#') grafo[x].push_back({id[a][b], 1}); } } } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(mat[i][j] == '#') continue; vector< pii > pos; for(int z = j; z > 0; z--) { if(mat[i][z] == '#') break; if(mat[i][z - 1] == '#' and mat[i][z] != '#') { pos.push_back({i, z}); break; } } for(int z = j; z <= m; z ++) { if(mat[i][z] == '#') break; if(mat[i][z + 1] == '#' and mat[i][z] != '#') { pos.push_back({i, z}); break; } } for(int z = i; z <= n; z ++) { if(mat[z][j] == '#') break; if(mat[z + 1][j] == '#' and mat[z][j] != '#') { pos.push_back({z, j}); break; } } for(int z = i; z > 0; z --) { if(mat[z][j] == '#') break; if(mat[z - 1][j] == '#' and mat[z][j] != '#') { pos.push_back({z, j}); break; } } int x = id[i][j]; for(int a = 0; a < pos.size(); a++) { for(int b = 0; b < pos.size(); b++) { if(a == b) continue; int A = abs(i - pos[a].f) + abs(j - pos[a].s), B = abs(i - pos[b].f) + abs(j - pos[b].s); int custo = min(A, B) + 1; grafo[x].push_back({id[pos[a].f][pos[a].s], custo}); // grafo[id[pos[b].f][pos[b].s]].push_back({id[pos[a].f][pos[a].s], custo}); //cout<<"GRAFO "<<i<<" "<<j<<" "<<pos[a].f<<" "<<pos[a].s<<" "<<pos[b].f<<" "<<pos[b].s<<" "<<custo<<"\n"; } } } } cout<<solve(id[xo][yo], id[xi][yi])<<"\n"; }

Compilation message (stderr)

portals.cpp: In function 'int solve(int, int)':
portals.cpp:30:25: warning: unused variable 'd' [-Wunused-variable]
   int x = fila.top().s, d = fila.top().s;
                         ^
portals.cpp:47:6: warning: unused variable 'u' [-Wunused-variable]
  int u = fim;
      ^
portals.cpp: In function 'int main()':
portals.cpp:174:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int a = 0; a < pos.size(); a++)
                   ~~^~~~~~~~~~~~
portals.cpp:176:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int b = 0; b < pos.size(); b++)
                    ~~^~~~~~~~~~~~
#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...