답안 #129896

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
129896 2019-07-13T13:12:04 Z Vardanyan 포탈들 (BOI14_portals) C++14
100 / 100
369 ms 38172 KB
#include <bits/stdc++.h>
using namespace std;
 
const int N = 1005;
char a[N][N];
pair<int,int> lft[N][N];
pair<int,int> rg[N][N];
pair<int,int> up[N][N];
pair<int,int> dwn[N][N];
int d[N][N];
bool col[N][N];
int n,m;
bool check(int i,int j){
    if(a[i-1][j] == '#' || i-1 == 0) return true;
    if(a[i+1][j] == '#' || i+1 == n+1) return true;
    if(a[i][j+1] == '#' || j+1 == m+1) return true;
    if(a[i][j-1] == '#' || j-1 == 0) return true;
    return false;
}
int dist(int i1,int j1,int i2,int j2){
    return abs(i1-i2)+abs(j1-j2);
}
int main(){
    ios_base::sync_with_stdio(false);
 
    cin>>n>>m;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=m;j++) cin>>a[i][j];
    }
    int si,sj;
    int fi,fj;
    for(int i = 1;i<=n;i++){
        pair<int,int> now;
        now = {i,1};
        for(int j = 1;j<=m;j++){
            if(a[i][j] == '#'){
                now.second = j+1;
                continue;
            }
            if(a[i][j] == 'S'){
                si = i;
                sj = j;
            }
            if(a[i][j] == 'C'){
                fi = i;
                fj = j;
            }
            lft[i][j] = now;
        }
        now = {i,m};
        for(int j = m;j>=1;j--){
            if(a[i][j] == '#'){
                now.second = j-1;
                continue;
            }
            rg[i][j] = now;
        }
    }
    for(int j = 1;j<=m;j++){
        pair<int,int> now;
        now = {1,j};
        for(int i = 1;i<=n;i++){
            if(a[i][j] == '#'){
                now.first = i+1;
                continue;
            }
            up[i][j] = now;
        }
        now = {n,j};
        for(int i = n;i>=1;i--){
            if(a[i][j] == '#'){
                now.first = i-1;
                continue;
            }
            dwn[i][j] = now;
        }
    }
    /*for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n;j++){
            cout<<i<<" "<<j<<" "<<lft[i][j].first<<" "<<lft[i][j].second<<endl;
        }
    }*/
    priority_queue<pair<int,pair<int,int> > > pq;
    pq.push({0,{si,sj}});
    memset(d,63,sizeof(d));
    d[si][sj] = 0;
    while(!pq.empty()){
        pair<int,pair<int,int> > x = pq.top();
        pq.pop();
        int ds = x.first;
        int i = x.second.first;
        int j = x.second.second;
        if(i+1<=n && a[i+1][j]!='#' && d[i][j]+1<d[i+1][j]){
            d[i+1][j] = min(d[i+1][j],d[i][j]+1);
            pq.push({-d[i+1][j],{i+1,j}});
        }
        if(j+1<=m && a[i][j+1]!='#' && d[i][j]+1<d[i][j+1]){
            d[i][j+1] = min(d[i][j+1],d[i][j]+1);
            pq.push({-d[i][j+1],{i,j+1}});
        }
        if(i-1>=1 && a[i-1][j]!='#' && d[i][j]+1<d[i-1][j]){
            d[i-1][j] = min(d[i-1][j],d[i][j]+1);
            pq.push({-d[i-1][j],{i-1,j}});
        }
        if(j-1>=1 && a[i][j-1]!='#' && d[i][j]+1<d[i][j-1]){
            d[i][j-1] = min(d[i][j-1],d[i][j]+1);
            pq.push({-d[i][j-1],{i,j-1}});
        }
        int ii = lft[i][j].first;
        int jj = lft[i][j].second;
        int mot = dist(i,j,lft[i][j].first,lft[i][j].second);
        mot = min(mot,dist(i,j,rg[i][j].first,rg[i][j].second));
        mot = min(mot,dist(i,j,up[i][j].first,up[i][j].second));
        mot = min(mot,dist(i,j,dwn[i][j].first,dwn[i][j].second));
        mot++;
        if(ii && jj && d[i][j]+mot<d[ii][jj]){
            d[ii][jj] = d[i][j]+mot;
            pq.push({-d[ii][jj],{ii,jj}});
        }
        ii = rg[i][j].first;
        jj = rg[i][j].second;
        if(ii && jj && d[i][j]+mot<d[ii][jj]){
            d[ii][jj] = d[i][j]+mot;
            pq.push({-d[ii][jj],{ii,jj}});
        }
        ii = up[i][j].first;
        jj = up[i][j].second;
        if(ii && jj && d[i][j]+mot<d[ii][jj]){
            d[ii][jj] = d[i][j]+mot;
            pq.push({-d[ii][jj],{ii,jj}});
        }
        ii = dwn[i][j].first;
        jj = dwn[i][j].second;
        if(ii && jj && d[i][j]+mot<d[ii][jj]){
            d[ii][jj] = d[i][j]+mot;
            pq.push({-d[ii][jj],{ii,jj}});
        }
    }
    cout<<d[fi][fj]<<endl;
    return 0;
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:90:13: warning: unused variable 'ds' [-Wunused-variable]
         int ds = x.first;
             ^~
portals.cpp:86:15: warning: 'sj' may be used uninitialized in this function [-Wmaybe-uninitialized]
     d[si][sj] = 0;
     ~~~~~~~~~~^~~
portals.cpp:86:15: warning: 'si' may be used uninitialized in this function [-Wmaybe-uninitialized]
portals.cpp:139:19: warning: 'fj' may be used uninitialized in this function [-Wmaybe-uninitialized]
     cout<<d[fi][fj]<<endl;
                   ^
portals.cpp:139:19: warning: 'fi' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4344 KB Output is correct
2 Correct 5 ms 4472 KB Output is correct
3 Correct 5 ms 4472 KB Output is correct
4 Correct 5 ms 4344 KB Output is correct
5 Correct 5 ms 4472 KB Output is correct
6 Correct 5 ms 4476 KB Output is correct
7 Correct 5 ms 4472 KB Output is correct
8 Correct 5 ms 4472 KB Output is correct
9 Correct 6 ms 4344 KB Output is correct
10 Correct 5 ms 4344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4344 KB Output is correct
2 Correct 5 ms 4472 KB Output is correct
3 Correct 5 ms 4476 KB Output is correct
4 Correct 6 ms 4472 KB Output is correct
5 Correct 5 ms 4472 KB Output is correct
6 Correct 5 ms 4476 KB Output is correct
7 Correct 6 ms 4472 KB Output is correct
8 Correct 5 ms 4472 KB Output is correct
9 Correct 6 ms 5240 KB Output is correct
10 Correct 6 ms 5240 KB Output is correct
11 Correct 6 ms 5240 KB Output is correct
12 Correct 8 ms 5212 KB Output is correct
13 Correct 6 ms 5240 KB Output is correct
14 Correct 5 ms 4344 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 5 ms 4384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4344 KB Output is correct
2 Correct 8 ms 4472 KB Output is correct
3 Correct 5 ms 4472 KB Output is correct
4 Correct 5 ms 4472 KB Output is correct
5 Correct 14 ms 8952 KB Output is correct
6 Correct 15 ms 8952 KB Output is correct
7 Correct 16 ms 8952 KB Output is correct
8 Correct 12 ms 8952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4344 KB Output is correct
2 Correct 6 ms 4488 KB Output is correct
3 Correct 5 ms 4516 KB Output is correct
4 Correct 5 ms 4348 KB Output is correct
5 Correct 5 ms 4472 KB Output is correct
6 Correct 5 ms 4488 KB Output is correct
7 Correct 5 ms 4472 KB Output is correct
8 Correct 6 ms 4472 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5240 KB Output is correct
11 Correct 6 ms 5240 KB Output is correct
12 Correct 7 ms 5240 KB Output is correct
13 Correct 6 ms 5240 KB Output is correct
14 Correct 14 ms 9080 KB Output is correct
15 Correct 15 ms 9080 KB Output is correct
16 Correct 16 ms 8952 KB Output is correct
17 Correct 15 ms 8952 KB Output is correct
18 Correct 17 ms 9080 KB Output is correct
19 Correct 18 ms 8952 KB Output is correct
20 Correct 18 ms 9208 KB Output is correct
21 Correct 14 ms 9080 KB Output is correct
22 Correct 15 ms 8952 KB Output is correct
23 Correct 16 ms 9084 KB Output is correct
24 Correct 16 ms 8952 KB Output is correct
25 Correct 5 ms 4344 KB Output is correct
26 Correct 7 ms 5240 KB Output is correct
27 Correct 5 ms 4472 KB Output is correct
28 Correct 12 ms 8952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4264 KB Output is correct
2 Correct 6 ms 4472 KB Output is correct
3 Correct 5 ms 4516 KB Output is correct
4 Correct 5 ms 4472 KB Output is correct
5 Correct 6 ms 4476 KB Output is correct
6 Correct 5 ms 4472 KB Output is correct
7 Correct 5 ms 4472 KB Output is correct
8 Correct 6 ms 4472 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5244 KB Output is correct
11 Correct 6 ms 5240 KB Output is correct
12 Correct 6 ms 5240 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 14 ms 8952 KB Output is correct
15 Correct 15 ms 8952 KB Output is correct
16 Correct 16 ms 9080 KB Output is correct
17 Correct 15 ms 8952 KB Output is correct
18 Correct 17 ms 8952 KB Output is correct
19 Correct 18 ms 9080 KB Output is correct
20 Correct 18 ms 9080 KB Output is correct
21 Correct 14 ms 8952 KB Output is correct
22 Correct 15 ms 8952 KB Output is correct
23 Correct 15 ms 8952 KB Output is correct
24 Correct 242 ms 37880 KB Output is correct
25 Correct 369 ms 38172 KB Output is correct
26 Correct 325 ms 38136 KB Output is correct
27 Correct 305 ms 37964 KB Output is correct
28 Correct 169 ms 37880 KB Output is correct
29 Correct 187 ms 37804 KB Output is correct
30 Correct 227 ms 37752 KB Output is correct
31 Correct 16 ms 8952 KB Output is correct
32 Correct 284 ms 37860 KB Output is correct
33 Correct 5 ms 4344 KB Output is correct
34 Correct 6 ms 5244 KB Output is correct
35 Correct 230 ms 37876 KB Output is correct
36 Correct 5 ms 4344 KB Output is correct
37 Correct 12 ms 8956 KB Output is correct
38 Correct 122 ms 37880 KB Output is correct
39 Correct 156 ms 30200 KB Output is correct