답안 #941136

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
941136 2024-03-08T07:50:36 Z Faisal_Saqib 포탈들 (BOI14_portals) C++17
100 / 100
208 ms 23444 KB
#include <bits/stdc++.h>
using namespace std;
const int N=1e3+10;
char grid[N][N];
bool vis[N][N];
int n,m;
vector<pair<int,int>> dir={{0,1},{0,-1},{1,0},{-1,0}};
bool valid(int i,int j)
{
    if(min(i,j)>=0 and i<n and j<m and grid[i][j]!='#')
        return 1;
    return 0;
}
int dist[N][N],before[N][N],after[N][N],up[N][N],down[N][N];
void dp(int sx,int sy)
{
  dist[sx][sy]=0;
  priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> pq;
  pq.push({0,{sx,sy}});
  while(pq.size())
  {
    auto tp=pq.top();
    pq.pop();
    int dip=tp.first;
    pair<int,int> pt=tp.second;
    if(dist[pt.first][pt.second]==dip)
    {
        // cout<<"Hola "<<pt.first<<' '<<pt.second<<' '<<dip<<endl;
        vis[pt.first][pt.second]=1;
        for(int j=0;j<4;j++)
        {
            if(valid(pt.first+dir[j].first,pt.second+dir[j].second) and dist[pt.first+dir[j].first][pt.second+dir[j].second]>dip+1)
            {
                dist[pt.first+dir[j].first][pt.second+dir[j].second]=dip+1;
                pq.push({dip+1,{pt.first+dir[j].first,pt.second+dir[j].second}});
            }
        }
        // Go to Up first
        {
            // Up
            int j=up[pt.first][pt.second];
            int jp=down[pt.first][pt.second];
            int curd=dip+(pt.first-j);
            if(curd<dist[jp-1][pt.second])
            {
                dist[jp-1][pt.second]=curd;
                pq.push({curd,{jp-1,pt.second}});
            }
 
            jp=before[pt.first][pt.second];
            if(curd<dist[pt.first][jp+1])
            {
                dist[pt.first][jp+1]=curd;
                pq.push({curd,{pt.first,jp+1}});
            }
 
            jp=after[pt.first][pt.second];
            if(curd<dist[pt.first][jp-1])
            {
                dist[pt.first][jp-1]=curd;
                pq.push({curd,{pt.first,jp-1}});
            }
        }
 
        {
            // Down
            int j=down[pt.first][pt.second];
            int jp=up[pt.first][pt.second];
            int curd=dip+(j-pt.first);
            if(curd<dist[jp+1][pt.second])
            {
                dist[jp+1][pt.second]=curd;
                pq.push({curd,{jp+1,pt.second}});
            }
 
            jp=before[pt.first][pt.second];
            if(curd<dist[pt.first][jp+1])
            {
                dist[pt.first][jp+1]=curd;
                pq.push({curd,{pt.first,jp+1}});
            }
 
            jp=after[pt.first][pt.second];
            if(curd<dist[pt.first][jp-1])
            {
                dist[pt.first][jp-1]=curd;
                pq.push({curd,{pt.first,jp-1}});
            }
        }
 
        {
            // Down
            int j=before[pt.first][pt.second];
            int curd=dip+(pt.second-j);
            int jp=up[pt.first][pt.second];
            if(curd<dist[jp+1][pt.second])
            {
                dist[jp+1][pt.second]=curd;
                pq.push({curd,{jp+1,pt.second}});
            }
 
            jp=down[pt.first][pt.second];
            if(curd<dist[jp-1][pt.second])
            {
                dist[jp-1][pt.second]=curd;
                pq.push({curd,{jp-1,pt.second}});
            }
 
            jp=after[pt.first][pt.second];
            if(curd<dist[pt.first][jp-1])
            {
                dist[pt.first][jp-1]=curd;
                pq.push({curd,{pt.first,jp-1}});
            }
        }
 
        {
            // Down
            int j=after[pt.first][pt.second];
            int curd=dip+(j-pt.second);
            int jp=up[pt.first][pt.second];
            if(curd<dist[jp+1][pt.second])
            {
                dist[jp+1][pt.second]=curd;
                pq.push({curd,{jp+1,pt.second}});
            }
 
            jp=down[pt.first][pt.second];
            if(curd<dist[jp-1][pt.second])
            {
                dist[jp-1][pt.second]=curd;
                pq.push({curd,{jp-1,pt.second}});
            }
 
            jp=before[pt.first][pt.second];
            if(curd<dist[pt.first][jp+1])
            {
                dist[pt.first][jp+1]=curd;
                pq.push({curd,{pt.first,jp+1}});
            }
        }
    }
  }
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    cin>>n>>m;
    n+=2;
    m+=2;
    int sx=0,sy=0,cx,cy;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(i==0 or j==0 or i==n-1 or j==m-1)
            {
                grid[i][j]='#';
            }
            else{
                cin>>grid[i][j];
                if(grid[i][j]=='S')
                {
                    sx=i;
                    sy=j;
                }
                if(grid[i][j]=='C')
                {
                    cx=i;
                    cy=j;
                }
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            dist[i][j]=1e9;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(grid[i][j]=='#')
            {
                before[i][j]=j;
            }
            else{
                before[i][j]=before[i][j-1];
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=m-1;j>=0;j--)
        {
            if(grid[i][j]=='#')
            {
                after[i][j]=j;
            }
            else{
                after[i][j]=after[i][j+1];
            }
        }
    }
    for(int j=0;j<m;j++)
    {
        for(int i=0;i<n;i++)
        {
            if(grid[i][j]=='#')
            {
                up[i][j]=i;
            }
            else{
                up[i][j]=up[i-1][j];
            }
        }
    }
    for(int j=0;j<m;j++)
    {
        for(int i=n-1;i>=0;i--)
        {
            if(grid[i][j]=='#')
            {
                down[i][j]=i;
            }
            else{
                down[i][j]=down[i+1][j];
            }
        }
    }
    // for(int i=0;i<n;i++)
    // {
    //     for(int j=0;j<m;j++)
    //     {
    //         cout<<grid[i][j];
    //     }
    //     cout<<endl;;
    // }  
    dp(sx,sy);
    cout<<dist[cx][cy];
    return 0;
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:245:22: warning: 'cy' may be used uninitialized in this function [-Wmaybe-uninitialized]
  245 |     cout<<dist[cx][cy];
      |                      ^
portals.cpp:245:22: warning: 'cx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10708 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 1 ms 10712 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 2 ms 12892 KB Output is correct
10 Correct 2 ms 12892 KB Output is correct
11 Correct 2 ms 12892 KB Output is correct
12 Correct 2 ms 12888 KB Output is correct
13 Correct 2 ms 12892 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 2 ms 12892 KB Output is correct
16 Correct 2 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 7 ms 21212 KB Output is correct
6 Correct 7 ms 21592 KB Output is correct
7 Correct 8 ms 21340 KB Output is correct
8 Correct 5 ms 21340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 1 ms 10712 KB Output is correct
3 Correct 1 ms 10708 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10728 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 1 ms 10588 KB Output is correct
9 Correct 2 ms 12892 KB Output is correct
10 Correct 2 ms 12892 KB Output is correct
11 Correct 2 ms 12892 KB Output is correct
12 Correct 2 ms 13012 KB Output is correct
13 Correct 2 ms 12892 KB Output is correct
14 Correct 6 ms 21340 KB Output is correct
15 Correct 8 ms 21340 KB Output is correct
16 Correct 8 ms 21340 KB Output is correct
17 Correct 9 ms 21340 KB Output is correct
18 Correct 9 ms 21340 KB Output is correct
19 Correct 9 ms 21340 KB Output is correct
20 Correct 9 ms 21340 KB Output is correct
21 Correct 6 ms 21340 KB Output is correct
22 Correct 8 ms 21408 KB Output is correct
23 Correct 8 ms 21340 KB Output is correct
24 Correct 9 ms 21592 KB Output is correct
25 Correct 1 ms 10584 KB Output is correct
26 Correct 2 ms 12892 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 4 ms 21340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10584 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10740 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 12892 KB Output is correct
10 Correct 2 ms 12888 KB Output is correct
11 Correct 2 ms 12892 KB Output is correct
12 Correct 2 ms 12892 KB Output is correct
13 Correct 3 ms 13012 KB Output is correct
14 Correct 8 ms 21340 KB Output is correct
15 Correct 8 ms 21336 KB Output is correct
16 Correct 8 ms 21340 KB Output is correct
17 Correct 8 ms 21340 KB Output is correct
18 Correct 9 ms 21328 KB Output is correct
19 Correct 10 ms 21328 KB Output is correct
20 Correct 9 ms 21420 KB Output is correct
21 Correct 6 ms 21336 KB Output is correct
22 Correct 7 ms 21340 KB Output is correct
23 Correct 8 ms 21220 KB Output is correct
24 Correct 173 ms 23300 KB Output is correct
25 Correct 208 ms 23444 KB Output is correct
26 Correct 185 ms 23348 KB Output is correct
27 Correct 175 ms 23388 KB Output is correct
28 Correct 101 ms 23124 KB Output is correct
29 Correct 121 ms 23164 KB Output is correct
30 Correct 150 ms 23196 KB Output is correct
31 Correct 8 ms 21336 KB Output is correct
32 Correct 163 ms 23240 KB Output is correct
33 Correct 2 ms 10588 KB Output is correct
34 Correct 3 ms 12892 KB Output is correct
35 Correct 148 ms 23420 KB Output is correct
36 Correct 1 ms 10588 KB Output is correct
37 Correct 4 ms 21340 KB Output is correct
38 Correct 45 ms 23152 KB Output is correct
39 Correct 87 ms 23160 KB Output is correct