제출 #973502

#제출 시각아이디문제언어결과실행 시간메모리
973502njoop포탈들 (BOI14_portals)C++17
70 / 100
1062 ms88184 KiB
#include <bits/stdc++.h> #define ti tuple<int, int, int> using namespace std; int r, c, dp[1010][1010], stx, sty, enx, eny, cx, cy, cdi, mn; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; char arr[1010][1010]; pair<int, int> dir[4][1010][1010], cur; priority_queue<ti, vector<ti>, greater<ti>> pq; int main() { cin.tie(0)->sync_with_stdio(0); cin >> r >> c; for(int i=0; i<1010; i++) for(int j=0; j<1010; j++) arr[i][j] = '#'; for(int i=1; i<=r; i++) { for(int j=1; j<=c; j++) { cin >> arr[i][j]; if(arr[i][j] == 'S') { stx = i; sty = j; } if(arr[i][j] == 'C') { enx = i; eny = j; } dp[i][j] = 1e9; } } for(int j=0; j<=c+1; j++) { for(int i=0; i<=r+1; i++) { if(arr[i][j] == '#') cur = {i+1, j}; dir[0][i][j] = cur; } } for(int i=0; i<=r+1; i++) { for(int j=0; j<=c+1; j++) { if(arr[i][j] == '#') cur = {i, j+1}; dir[1][i][j] = cur; } } for(int j=0; j<=c+1; j++) { for(int i=r+1; i>=0; i--) { if(arr[i][j] == '#') cur = {i-1, j}; dir[2][i][j] = cur; } } for(int i=0; i<=r+1; i++) { for(int j=c+1; j>=0; j--) { if(arr[i][j] == '#') cur = {i, j-1}; dir[3][i][j] = cur; } } pq.push({0, stx, sty}); while(pq.size()) { cdi = get<0>(pq.top()); cx = get<1>(pq.top()); cy = get<2>(pq.top()); pq.pop(); if(cx < 1 || cx > r || cy < 1 || cy > c || dp[cx][cy] <= cdi || arr[cx][cy] == '#') continue; dp[cx][cy] = cdi; mn = 1e9; for(int i=0; i<4; i++) { mn = min(mn, abs(dir[i][cx][cy].first-cx) + abs(dir[i][cx][cy].second-cy)); pq.push({cdi+1, cx+dx[i], cy+dy[i]}); } for(int i=0; i<4; i++) { pq.push({cdi+mn+1, dir[i][cx][cy].first, dir[i][cx][cy].second}); } } cout << dp[enx][eny]; 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...
#Verdict Execution timeMemoryGrader output
Fetching results...