Submission #465655

# Submission time Handle Problem Language Result Execution time Memory
465655 2021-08-16T15:47:31 Z MohamedFaresNebili Portals (BOI14_portals) C++14
0 / 100
1 ms 244 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[m], h[n]; 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; grid[l][i]='.'; }
            if(c=='C') { ex=l; ey=i; grid[l][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);
            if(i==m-1&&grid[l][i]=='.') h[l].insert(i);
            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);
           	if(i==n-1&&grid[i][l]=='.') v[l].insert(i);
            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';
}
 
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[m], h[n]; 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';
      |                      ^~~~
portals.cpp:70:22: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 0 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Incorrect 0 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 244 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 0 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -