Submission #155763

# Submission time Handle Problem Language Result Execution time Memory
155763 2019-09-30T09:54:58 Z brcode Portals (BOI14_portals) C++14
100 / 100
506 ms 64188 KB
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
int n,m;
char grid[MAXN][MAXN];
pair<int,int> start;
bool portal[MAXN][MAXN];
vector<pair<int,int>> portals;
queue<pair<int,int>> q1;
priority_queue<pair<int,pair<int,int>>> pq;
pair<int,int> finish;
int dx[5] = {1,0,-1,0};
int dy[5] = {0,1,0,-1};
int portall[MAXN][MAXN];
int portald[MAXN][MAXN];
int portalr[MAXN][MAXN];
int portalu[MAXN][MAXN];
int dp[MAXN][MAXN];
int dpl[MAXN][MAXN];
int dpr[MAXN][MAXN];
int dpu[MAXN][MAXN];
int dpd[MAXN][MAXN];
int dist[MAXN][MAXN];
bool check(int x,int y){
    if(x<=n && x>=1 && y<=m && y>=1){
        return true;
    }
    return false;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>grid[i][j];
            if(grid[i][j] == 'S'){
                start = make_pair(i,j);
               // cout<<i<<" "<<j<<endl;
            }
            if(grid[i][j] == 'C'){
                finish = make_pair(i,j);
            }
        }
    }
    for(int i=1;i<=n;i++){
        grid[i][0] = '#';
        grid[i][m+1] = '#';
    }
    for(int i=1;i<=m;i++){
        grid[0][i] = '#';
        grid[n+1][i] = '#';
    }
 
    grid[0][0] = '#';
    grid[0][m+1] = '#';
    grid[n+1][0] = '#';
    grid[n+1][m+1] = '#';
    for(int i=0;i<=n+1;i++){
        for(int j=0;j<=m+1;j++){
            if(grid[i][j] == '#'){
                for(int k=0;k<4;k++){
                    int x2 = i+dx[k];
                    int y2 = j+dy[k];
 
                    if(check(x2,y2) && grid[x2][y2]!='#'){
                        //if within grid and surrounding cell is a movable cell, place a portal here
                        //int dx[5] = {1,0,-1,0};
                        //int dy[5] = {0,1,0,-1};
                        if(k==0){
                            portalu[x2][y2] = true;
                        }
                        if(k==1){
                            portall[x2][y2] = true;
                        }
                        if(k==2){
                            portald[x2][y2] = true;
                        }
                        if(k==3){
                            portalr[x2][y2] = true;
                        }
                        portal[x2][y2] = true;
                        portals.push_back(make_pair(x2,y2));
                    }
                }
            }
 
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            dist[i][j] = 1e9;
            dp[i][j] = 1e9;
        }
    }
    for(auto x:portals){
        q1.push(x);
 
       // cout<<x.first<<" "<<x.second<<endl;
        dist[x.first][x.second] = 0;
    }
    while(!q1.empty()){
        auto hold =q1.front();
        q1.pop();
        int x = hold.first;
        int y = hold.second;
        for(int i=0;i<4;i++){
            int x2 = x+dx[i];
            int y2 = y+dy[i];
            if(check(x2,y2) && dist[x2][y2]==1e9 && grid[x2][y2]!='#'){
 
                dist[x2][y2] = dist[x][y]+1;
                q1.push(make_pair(x2,y2));
            }
        }
    }
 
    for(int i=1;i<=n;i++){
        int hold = 1;
        for(int j=1;j<=m;j++){
            if(grid[i][j] == '#'){
                continue;
            }
            if(portall[i][j]){
                hold = j;
            }
            dpl[i][j] = hold;
            
        }
    }
 
    for(int i=1;i<=n;i++){
        int hold = m;
        for(int j=m;j>=1;j--){
            if(grid[i][j] == '#'){
                continue;
            }
            if(portalr[i][j]){
                hold = j;
            }
            dpr[i][j] = hold;
            
        }
    }
 
    for(int i=1;i<=m;i++){
        int hold = 1;
        for(int j=1;j<=n;j++){
            if(grid[j][i] == '#'){
                continue;
            }
            if(portalu[j][i]){
                hold = j;
            }
            
            dpu[j][i] = hold;

            
        }
    }
 
    for(int i=1;i<=m;i++){
        int hold = n;
        for(int j=n;j>=1;j--){
            if(grid[j][i] == '#'){
                continue;
            }
            if(portald[j][i]){
                hold =j;
            }
            dpd[j][i] = hold;
            
        }
    }

    pq.push(make_pair(0,start));
    dp[start.first][start.second] = 0;
    while(!pq.empty()){
        auto hold = pq.top();
        pq.pop();
        int w = hold.first;
 
        int x = hold.second.first;
        int y = hold.second.second;
 
        if(x == finish.first && y == finish.second){
            cout<<-1*w<<endl;
            return 0;
        }
        for(int i=0;i<4;i++){
            int x2 = x+dx[i];
            int y2 = y+dy[i];
            if(check(x2,y2) && grid[x2][y2]!='#' && dp[x2][y2]>dp[x][y]+1){
                dp[x2][y2] = dp[x][y]+1;
                pq.push(make_pair(-1*dp[x2][y2],make_pair(x2,y2)));
 
            }
            int p = dpl[x][y];
            if(check(x,p) && grid[x][p] != '#' && dp[x][p]>dp[x][y]+dist[x][y]+1){
                dp[x][p]=dp[x][y]+dist[x][y]+1;
                pq.push(make_pair(-1*dp[x][p],make_pair(x,p)));
 
            }
            p = dpr[x][y];
 
            if(check(x,p) && grid[x][p] != '#' && dp[x][p]>dp[x][y]+dist[x][y]+1){
                dp[x][p]=dp[x][y]+dist[x][y]+1;
                pq.push(make_pair(-1*dp[x][p],make_pair(x,p)));
 
            }
            p = dpu[x][y];
            if(x == 4 && y == 3){
                //cout<<123<<" "<<p<<endl;
                //cout<<dp[p][y]<<" "<<dp[x][y]+dist[x][y]+1<<endl;
            }
            if(check(p,y) && grid[p][y] != '#' && dp[p][y]>dp[x][y]+dist[x][y]+1){
                dp[p][y]=dp[x][y]+dist[x][y]+1;
                pq.push(make_pair(-1*dp[p][y],make_pair(p,y)));
 
            }
            p = dpd[x][y];
            if(check(p,y) && grid[p][y] != '#' && dp[p][y]>dp[x][y]+dist[x][y]+1){
                dp[p][y]=dp[x][y]+dist[x][y]+1;
                pq.push(make_pair(-1*dp[p][y],make_pair(p,y)));
 
            }
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 760 KB Output is correct
3 Correct 2 ms 760 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 796 KB Output is correct
6 Correct 3 ms 760 KB Output is correct
7 Correct 3 ms 760 KB Output is correct
8 Correct 3 ms 760 KB Output is correct
9 Correct 4 ms 572 KB Output is correct
10 Correct 2 ms 576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 760 KB Output is correct
3 Correct 2 ms 760 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 760 KB Output is correct
6 Correct 2 ms 760 KB Output is correct
7 Correct 2 ms 760 KB Output is correct
8 Correct 2 ms 760 KB Output is correct
9 Correct 4 ms 2168 KB Output is correct
10 Correct 4 ms 2168 KB Output is correct
11 Correct 4 ms 2552 KB Output is correct
12 Correct 4 ms 2424 KB Output is correct
13 Correct 5 ms 2552 KB Output is correct
14 Correct 2 ms 632 KB Output is correct
15 Correct 4 ms 2268 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 760 KB Output is correct
3 Correct 2 ms 760 KB Output is correct
4 Correct 2 ms 760 KB Output is correct
5 Correct 21 ms 9456 KB Output is correct
6 Correct 23 ms 9456 KB Output is correct
7 Correct 20 ms 9456 KB Output is correct
8 Correct 19 ms 9584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 760 KB Output is correct
3 Correct 2 ms 764 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 760 KB Output is correct
6 Correct 2 ms 760 KB Output is correct
7 Correct 2 ms 760 KB Output is correct
8 Correct 2 ms 760 KB Output is correct
9 Correct 4 ms 2296 KB Output is correct
10 Correct 4 ms 2168 KB Output is correct
11 Correct 4 ms 2552 KB Output is correct
12 Correct 4 ms 2552 KB Output is correct
13 Correct 4 ms 2552 KB Output is correct
14 Correct 21 ms 9452 KB Output is correct
15 Correct 22 ms 9456 KB Output is correct
16 Correct 21 ms 9456 KB Output is correct
17 Correct 26 ms 8692 KB Output is correct
18 Correct 21 ms 9204 KB Output is correct
19 Correct 14 ms 7288 KB Output is correct
20 Correct 17 ms 7288 KB Output is correct
21 Correct 21 ms 9456 KB Output is correct
22 Correct 22 ms 9456 KB Output is correct
23 Correct 23 ms 9456 KB Output is correct
24 Correct 20 ms 7928 KB Output is correct
25 Correct 2 ms 632 KB Output is correct
26 Correct 4 ms 2296 KB Output is correct
27 Correct 2 ms 504 KB Output is correct
28 Correct 20 ms 9584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Correct 2 ms 508 KB Output is correct
5 Correct 3 ms 760 KB Output is correct
6 Correct 3 ms 760 KB Output is correct
7 Correct 2 ms 764 KB Output is correct
8 Correct 2 ms 760 KB Output is correct
9 Correct 4 ms 2296 KB Output is correct
10 Correct 4 ms 2040 KB Output is correct
11 Correct 4 ms 2552 KB Output is correct
12 Correct 4 ms 2424 KB Output is correct
13 Correct 5 ms 2552 KB Output is correct
14 Correct 21 ms 9504 KB Output is correct
15 Correct 22 ms 9460 KB Output is correct
16 Correct 20 ms 9456 KB Output is correct
17 Correct 20 ms 8696 KB Output is correct
18 Correct 28 ms 9176 KB Output is correct
19 Correct 13 ms 7288 KB Output is correct
20 Correct 18 ms 7300 KB Output is correct
21 Correct 22 ms 9456 KB Output is correct
22 Correct 22 ms 9456 KB Output is correct
23 Correct 23 ms 9460 KB Output is correct
24 Correct 398 ms 52560 KB Output is correct
25 Correct 506 ms 36268 KB Output is correct
26 Correct 210 ms 35252 KB Output is correct
27 Correct 324 ms 35448 KB Output is correct
28 Correct 370 ms 61392 KB Output is correct
29 Correct 405 ms 61756 KB Output is correct
30 Correct 424 ms 61320 KB Output is correct
31 Correct 23 ms 8056 KB Output is correct
32 Correct 456 ms 39288 KB Output is correct
33 Correct 2 ms 632 KB Output is correct
34 Correct 4 ms 2296 KB Output is correct
35 Correct 335 ms 49244 KB Output is correct
36 Correct 2 ms 504 KB Output is correct
37 Correct 22 ms 9584 KB Output is correct
38 Correct 371 ms 64188 KB Output is correct
39 Correct 281 ms 56972 KB Output is correct