답안 #731429

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
731429 2023-04-27T12:25:40 Z Trunkty 포탈들 (BOI14_portals) C++14
100 / 100
742 ms 83520 KB
#include <bits/extc++.h>
using namespace std;
typedef long long ll;
#define int ll

int r,c,sx,sy,fx,fy;
char grid[1005][1005];
int dist[1005][1005],lef[1005][1005],rit[1005][1005],up[1005][1005],down[1005][1005];
bool check[1005][1005];
int dp[1005][1005];
bool check2[1005][1005];

signed main(){
	//ios_base::sync_with_stdio(false);
	//cin.tie(NULL);
    cin >> r >> c;
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            cin >> grid[i][j];
            if(grid[i][j]=='S'){
                sx = i;
                sy = j;
                grid[i][j] = '.';
            }
            else if(grid[i][j]=='C'){
                fx = i;
                fy = j;
                grid[i][j] = '.';
            }
            dp[i][j] = 2e9;
        }
    }
    priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>> pq;
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(grid[i][j]=='.' and grid[i-1][j]=='.' and grid[i+1][j]=='.' and grid[i][j-1]=='.' and grid[i][j+1]=='.'){
                dist[i][j] = 2e9;
            }
            else if(grid[i][j]=='.'){
                pq.push({dist[i][j],i,j});
            }
        }
    }
    while(pq.size()>0){
        while(pq.size()>0 and check[pq.top()[1]][pq.top()[2]]){
            pq.pop();
        }
        if(pq.size()==0){
            break;
        }
        int x = pq.top()[1], y = pq.top()[2];
        pq.pop();
        check[x][y] = true;
        if(grid[x-1][y]=='.' and dist[x-1][y]>dist[x][y]+1){
            dist[x-1][y] = dist[x][y]+1;
            pq.push({dist[x-1][y],x-1,y});
        }
        if(grid[x+1][y]=='.' and dist[x+1][y]>dist[x][y]+1){
            dist[x+1][y] = dist[x][y]+1;
            pq.push({dist[x+1][y],x+1,y});
        }
        if(grid[x][y-1]=='.' and dist[x][y-1]>dist[x][y]+1){
            dist[x][y-1] = dist[x][y]+1;
            pq.push({dist[x][y-1],x,y-1});
        }
        if(grid[x][y+1]=='.' and dist[x][y+1]>dist[x][y]+1){
            dist[x][y+1] = dist[x][y]+1;
            pq.push({dist[x][y+1],x,y+1});
        }
    }
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(grid[i][j-1]!='.'){
                lef[i][j] = j;
            }
            else{
                lef[i][j] = lef[i][j-1];
            }
        }
        for(int j=c;j>=1;j--){
            if(grid[i][j+1]!='.'){
                rit[i][j] = j;
            }
            else{
                rit[i][j] = rit[i][j+1];
            }
        }
    }
    for(int j=1;j<=c;j++){
        for(int i=1;i<=r;i++){
            if(grid[i-1][j]!='.'){
                up[i][j] = i;
            }
            else{
                up[i][j] = up[i-1][j];
            }
        }
        for(int i=r;i>=1;i--){
            if(grid[i+1][j]!='.'){
                down[i][j] = i;
            }
            else{
                down[i][j] = down[i+1][j];
            }
        }
    }
    dp[sx][sy] = 0;
    pq.push({0,sx,sy});
    while(pq.size()>0){
        while(pq.size()>0 and check2[pq.top()[1]][pq.top()[2]]){
            pq.pop();
        }
        if(pq.size()==0){
            break;
        }
        int x = pq.top()[1], y = pq.top()[2];
        pq.pop();
        check2[x][y] = true;
        if(grid[x-1][y]=='.' and dp[x-1][y]>dp[x][y]+1){
            dp[x-1][y] = dp[x][y]+1;
            pq.push({dp[x-1][y],x-1,y});
        }
        if(grid[x+1][y]=='.' and dp[x+1][y]>dp[x][y]+1){
            dp[x+1][y] = dp[x][y]+1;
            pq.push({dp[x+1][y],x+1,y});
        }
        if(grid[x][y-1]=='.' and dp[x][y-1]>dp[x][y]+1){
            dp[x][y-1] = dp[x][y]+1;
            pq.push({dp[x][y-1],x,y-1});
        }
        if(grid[x][y+1]=='.' and dp[x][y+1]>dp[x][y]+1){
            dp[x][y+1] = dp[x][y]+1;
            pq.push({dp[x][y+1],x,y+1});
        }
        if(dp[x][lef[x][y]]>dp[x][y]+dist[x][y]+1){
            dp[x][lef[x][y]] = dp[x][y]+dist[x][y]+1;
            pq.push({dp[x][lef[x][y]],x,lef[x][y]});
        }
        if(dp[x][rit[x][y]]>dp[x][y]+dist[x][y]+1){
            dp[x][rit[x][y]] = dp[x][y]+dist[x][y]+1;
            pq.push({dp[x][rit[x][y]],x,rit[x][y]});
        }
        if(dp[up[x][y]][y]>dp[x][y]+dist[x][y]+1){
            dp[up[x][y]][y] = dp[x][y]+dist[x][y]+1;
            pq.push({dp[up[x][y]][y],up[x][y],y});
        }
        if(dp[down[x][y]][y]>dp[x][y]+dist[x][y]+1){
            dp[down[x][y]][y] = dp[x][y]+dist[x][y]+1;
            pq.push({dp[down[x][y]][y],down[x][y],y});
        }
    }
    cout << dp[fx][fy] << "\n";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 564 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 440 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 560 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 564 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 536 KB Output is correct
9 Correct 2 ms 1748 KB Output is correct
10 Correct 2 ms 1748 KB Output is correct
11 Correct 2 ms 1620 KB Output is correct
12 Correct 2 ms 1712 KB Output is correct
13 Correct 2 ms 1748 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 1748 KB Output is correct
16 Correct 1 ms 436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 15 ms 7796 KB Output is correct
6 Correct 16 ms 7788 KB Output is correct
7 Correct 18 ms 7920 KB Output is correct
8 Correct 14 ms 7948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 568 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 432 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 564 KB Output is correct
9 Correct 2 ms 1748 KB Output is correct
10 Correct 2 ms 1748 KB Output is correct
11 Correct 2 ms 1620 KB Output is correct
12 Correct 2 ms 1712 KB Output is correct
13 Correct 2 ms 1748 KB Output is correct
14 Correct 15 ms 7740 KB Output is correct
15 Correct 17 ms 7820 KB Output is correct
16 Correct 18 ms 7948 KB Output is correct
17 Correct 19 ms 8588 KB Output is correct
18 Correct 22 ms 8716 KB Output is correct
19 Correct 23 ms 7684 KB Output is correct
20 Correct 21 ms 7668 KB Output is correct
21 Correct 15 ms 8316 KB Output is correct
22 Correct 17 ms 8312 KB Output is correct
23 Correct 18 ms 8764 KB Output is correct
24 Correct 20 ms 7628 KB Output is correct
25 Correct 1 ms 440 KB Output is correct
26 Correct 2 ms 1748 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 13 ms 8008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 1716 KB Output is correct
10 Correct 3 ms 1748 KB Output is correct
11 Correct 2 ms 1712 KB Output is correct
12 Correct 2 ms 1716 KB Output is correct
13 Correct 2 ms 1748 KB Output is correct
14 Correct 15 ms 7784 KB Output is correct
15 Correct 17 ms 7820 KB Output is correct
16 Correct 17 ms 7948 KB Output is correct
17 Correct 18 ms 8652 KB Output is correct
18 Correct 22 ms 8668 KB Output is correct
19 Correct 22 ms 7764 KB Output is correct
20 Correct 20 ms 7712 KB Output is correct
21 Correct 15 ms 8204 KB Output is correct
22 Correct 18 ms 8332 KB Output is correct
23 Correct 20 ms 8780 KB Output is correct
24 Correct 541 ms 78236 KB Output is correct
25 Correct 742 ms 52100 KB Output is correct
26 Correct 559 ms 51788 KB Output is correct
27 Correct 558 ms 51732 KB Output is correct
28 Correct 374 ms 74868 KB Output is correct
29 Correct 420 ms 82624 KB Output is correct
30 Correct 469 ms 83520 KB Output is correct
31 Correct 19 ms 7636 KB Output is correct
32 Correct 517 ms 51768 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 2 ms 1748 KB Output is correct
35 Correct 455 ms 65320 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 13 ms 7988 KB Output is correct
38 Correct 329 ms 80068 KB Output is correct
39 Correct 341 ms 71004 KB Output is correct