제출 #97092

#제출 시각아이디문제언어결과실행 시간메모리
97092dalgerok포탈들 (BOI14_portals)C++14
100 / 100
556 ms42580 KiB
#include<bits/stdc++.h> using namespace std; const int N = 1005, INF = 1e9, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; int n, m; char a[N][N]; int dist_to_wall[N][N], d[N][N]; int sx, sy, tx, ty; pair < int, int > nxt[4][N][N]; inline bool is_wall(int x, int y){ return x < 1 || x > n || y < 1 || y > m || a[x][y] == '#'; } void walls(){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ dist_to_wall[i][j] = INF; } } queue < pair < int, int > > q; for(int i = 0; i <= n + 1; i++){ for(int j = 0; j <= m + 1; j++){ if(is_wall(i, j)){ dist_to_wall[i][j] = 0; q.push(make_pair(i, j)); } } } while(!q.empty()){ int x = q.front().first, y = q.front().second; q.pop(); for(int i = 0; i < 4; i++){ int xx = x + dx[i], yy = y + dy[i]; if(is_wall(xx, yy)){ continue; } if(dist_to_wall[xx][yy] > dist_to_wall[x][y] + 1){ dist_to_wall[xx][yy] = dist_to_wall[x][y] + 1; q.push(make_pair(xx, yy)); } } } } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cin >> a[i][j]; } } walls(); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(j == 1 || a[i][j - 1] == '#'){ nxt[0][i][j] = make_pair(i, j); } else{ nxt[0][i][j] = nxt[0][i][j - 1]; } if(i == 1 || a[i - 1][j] == '#'){ nxt[1][i][j] = make_pair(i, j); } else{ nxt[1][i][j] = nxt[1][i - 1][j]; } } } for(int i = n; i >= 1; i--){ for(int j = m; j >= 1; j--){ if(j == m || a[i][j + 1] == '#'){ nxt[2][i][j] = make_pair(i, j); } else{ nxt[2][i][j] = nxt[2][i][j + 1]; } if(i == n || a[i + 1][j] == '#'){ nxt[3][i][j] = make_pair(i, j); } else{ nxt[3][i][j] = nxt[3][i + 1][j]; } } } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ d[i][j] = INF; if(a[i][j] == 'S'){ sx = i; sy = j; } else if(a[i][j] == 'C'){ tx = i; ty = j; } } } /**for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cout << dist_to_wall[i][j] << " "; } cout << "\n"; }*/ set < pair < int, pair < int, int > > > q; d[sx][sy] = 0; q.insert(make_pair(0, make_pair(sx, sy))); while(!q.empty()){ int x = q.begin()->second.first, y = q.begin()->second.second; q.erase(q.begin()); for(int i = 0; i < 4; i++){ int xx = x + dx[i], yy = y + dy[i]; if(!is_wall(xx, yy) && d[xx][yy] > d[x][y] + 1){ q.erase(make_pair(d[xx][yy], make_pair(xx, yy))); d[xx][yy] = d[x][y] + 1; q.insert(make_pair(d[xx][yy], make_pair(xx, yy))); } } for(int i = 0; i < 4; i++){ int xx = nxt[i][x][y].first, yy = nxt[i][x][y].second; if(d[xx][yy] > d[x][y] + dist_to_wall[x][y]){ q.erase(make_pair(d[xx][yy], make_pair(xx, yy))); d[xx][yy] = d[x][y] + dist_to_wall[x][y]; q.insert(make_pair(d[xx][yy], make_pair(xx, yy))); } } } cout << d[tx][ty]; }
#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...