답안 #465642

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
465642 2021-08-16T15:42:15 Z MohamedFaresNebili 포탈들 (BOI14_portals) C++14
0 / 100
1 ms 408 KB
#include <bits/stdc++.h>

using namespace std;

void solve()
{
    /// JUST KEEP GOING
    const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0};
    int n, m; cin>>n>>m; char grid[n][m]; int d[n][m];
    set<int>v[n], h[m]; int sx, sy, ex, ey; bool vis[n][m];
    for(int l=0;l<n;l++) {
        for(int i=0;i<m;i++) {
            cin>>grid[l][i]; d[l][i]=1e9;
            char c=grid[l][i]; vis[l][i]=false;
            if(c=='S') { sx=l; sy=i; d[l][i]=0; }
            if(c=='C') { ex=l; ey=i; }
        }
    }
    for(int l=0;l<n;l++) {
        for(int i=0;i<m;i++) {
            if(i==0&&grid[l][i]=='.') h[l].insert(i);
            else if(i==m-1&&grid[l][i]=='.') h[l].insert(i);
            else if(i>0&&grid[l][i]=='.'&&grid[l][i-1]=='#') h[l].insert(i);
        }
    }
    for(int l=0;l<m;l++) {
        for(int i=0;i<n;i++) {
            if(i==0&&grid[i][l]=='.') v[l].insert(i);
            else if(i==n-1&&grid[i][l]=='.') v[l].insert(i);
            else if(i>0&&grid[i][l]=='.'&&grid[i-1][l]=='#') v[l].insert(i);
        }
    }
    queue<pair<int, int>>q; q.push({sx, sy});
    while(!q.empty()) {
        int a=q.front().first, b=q.front().second; q.pop();
        for(int l=0;l<4;l++) {
            int x=a+nx[l], y=b+ny[l];
            if(x>=0&&x<n&&y>=0&&y<m) {
                if(grid[x][y]=='#') continue;
                if(d[x][y]>d[a][b]+1) {
                    d[x][y]=d[a][b]+1; q.push({x, y});
                }
            }
        }
        auto it=h[a].lower_bound(b);
        if(it!=h[a].end()) {
            if(d[a][*it]>d[a][b]+1) {
                d[a][*it]=d[a][b]+1; q.push({a, *it});
            }
        }
        if(it!=h[a].begin()) {
            it--;
            if(d[a][*it]>d[a][b]+1) {
                d[a][*it]=d[a][b]+1; q.push({a, *it});
            }
        }
        it=v[b].lower_bound(a);
        if(it!=v[b].end()) {
            if(d[*it][b]>d[a][b]+1) {
                d[*it][b]=d[a][b]+1; q.push({*it, b});
            }
        }
        if(it!=v[b].begin()) {
            it--;
            if(d[*it][b]>d[a][b]+1) {
                d[*it][b]=d[a][b]+1; q.push({*it, b});
            }
        }
    }
    cout<<d[ex][ey]<<'\n'; return;
}

int32_t main()
{

    int t; t=1;
    while(t--) {
        solve();
    }
    return 0;
}

Compilation message

portals.cpp: In function 'void solve()':
portals.cpp:10:50: warning: variable 'vis' set but not used [-Wunused-but-set-variable]
   10 |     set<int>v[n], h[m]; int sx, sy, ex, ey; bool vis[n][m];
      |                                                  ^~~
portals.cpp:70:22: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
   70 |     cout<<d[ex][ey]<<'\n'; return;
      |                      ^~~~
portals.cpp:70:22: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 408 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 292 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -