Submission #270441

# Submission time Handle Problem Language Result Execution time Memory
270441 2020-08-17T15:32:13 Z hieppgga Portals (BOI14_portals) C++14
100 / 100
371 ms 45356 KB
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
void readInput()
{

}
void solve()
{

}
const int N=1004;
int m,n,d[N][N],lef[N][N],rig[N][N],up[N][N],down[N][N],dp[N][N];
char c[1004][1004];
int x[4]={1,-1,0,0};
int y[4]={0,0,1,-1};
int INF=1e7;
typedef pair<int,int> ii;
typedef pair<int,pair<int,int>> iii;
priority_queue<iii,vector<iii>,greater<iii>> pq;
queue<ii> q;
ii start,en;
void bfs()
{
    while(!q.empty())
    {
        ii u=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
           if(u.fi+x[i]>=1&&u.fi+x[i]<=m&&u.se+y[i]>=1&&u.se+y[i]<=n&&d[u.fi+x[i]][u.se+y[i]]==m*n+1)
           {
               d[u.fi+x[i]][u.se+y[i]]=d[u.fi][u.se]+1;
               q.push({u.fi+x[i],u.se+y[i]});
           }
        }
    }
}
void dijkstra()
{
    for(int i=1;i<=m;i++)
       for(int j=1;j<=n;j++)
           dp[i][j]=INF;
    dp[start.fi][start.se]=0;
    pq.push({0,{start.fi,start.se}});
    while(!pq.empty())
    {
        iii u=pq.top();
        pq.pop();
        if(dp[u.se.fi][u.se.se]!=u.fi) continue;
        for(int i=0;i<4;i++)
        {
         if(u.se.fi+x[i]>=1&&u.se.fi+x[i]<=m&&u.se.se+y[i]>=1&&u.se.se+y[i]<=n&&dp[u.se.fi+x[i]][u.se.se+y[i]]>dp[u.se.fi][u.se.se]+1&&c[u.se.fi+x[i]][u.se.se+y[i]]!='#')
         {
             dp[u.se.fi+x[i]][u.se.se+y[i]]=dp[u.se.fi][u.se.se]+1;
             pq.push({ dp[u.se.fi+x[i]][u.se.se+y[i]],{u.se.fi+x[i],u.se.se+y[i]}});
         }
        }
        if(dp[up[u.se.fi][u.se.se]][u.se.se]>dp[u.se.fi][u.se.se]+d[u.se.fi][u.se.se]+1)
        {
            dp[up[u.se.fi][u.se.se]][u.se.se]=dp[u.se.fi][u.se.se]+d[u.se.fi][u.se.se]+1;
            pq.push({dp[up[u.se.fi][u.se.se]][u.se.se],{up[u.se.fi][u.se.se],u.se.se}});
        }
        if(dp[down[u.se.fi][u.se.se]][u.se.se]>dp[u.se.fi][u.se.se]+d[u.se.fi][u.se.se]+1)
        {
            dp[down[u.se.fi][u.se.se]][u.se.se]=dp[u.se.fi][u.se.se]+d[u.se.fi][u.se.se]+1;
            pq.push({dp[down[u.se.fi][u.se.se]][u.se.se],{down[u.se.fi][u.se.se],u.se.se}});
        }
        if(dp[u.se.fi][lef[u.se.fi][u.se.se]]>dp[u.se.fi][u.se.se]+d[u.se.fi][u.se.se]+1)
        {
            dp[u.se.fi][lef[u.se.fi][u.se.se]]=dp[u.se.fi][u.se.se]+d[u.se.fi][u.se.se]+1;
            pq.push({dp[u.se.fi][lef[u.se.fi][u.se.se]],{u.se.fi,lef[u.se.fi][u.se.se]}});
        }
         if(dp[u.se.fi][rig[u.se.fi][u.se.se]]>dp[u.se.fi][u.se.se]+d[u.se.fi][u.se.se]+1)
        {
            dp[u.se.fi][rig[u.se.fi][u.se.se]]=dp[u.se.fi][u.se.se]+d[u.se.fi][u.se.se]+1;
            pq.push({dp[u.se.fi][rig[u.se.fi][u.se.se]],{u.se.fi,rig[u.se.fi][u.se.se]}});
        }
    }
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  readInput();
  solve();
  cin>>m>>n;
  for(int i=1;i<=m;i++)
    for(int j=1;j<=n;j++)
        d[i][j]=m*n+1;
  for(int i=1;i<=m;i++)
  {
      for(int j=1;j<=n;j++)
      {
          cin>>c[i][j];
          if(c[i][j]=='#')
          {
              q.push({i,j});
              d[i][j]=0;
              for(int k=0;k<4;k++)
              {
                  if(i+x[k]>=1&&i+x[k]<=m&&j+y[k]>=1&&j+y[k]<=n)
                     d[i+x[k]][j+y[k]]=0,q.push({i+x[k],j+y[k]});
              }
          }
          else
          {
            if(c[i][j]=='S') start={i,j};
            if(c[i][j]=='C') en={i,j};
            if(i==1||i==m||j==1||j==n)
            {
                  q.push({i,j});
                  d[i][j]=0;
            }
          }
      }
  }
  bfs();
  for(int i=1;i<=m;i++)
  {
      int cnt=1;
      for(int j=1;j<=n;j++)
      {
          if(c[i][j]=='#') cnt=j+1;
          else
          lef[i][j]=cnt;
      }
      cnt=n;
      for(int j=n;j>=1;j--)
      {
          if(c[i][j]=='#') cnt=j-1;
          else
           rig[i][j]=cnt;
      }
  }
  for(int i=1;i<=n;i++)
  {
      int cnt=1;
      for(int j=1;j<=m;j++)
      {
          if(c[j][i]=='#') cnt=j+1;
          else up[j][i]=cnt;
      }
      cnt=m;
      for(int j=m;j>=1;j--)
      {
          if(c[j][i]=='#') cnt=j-1;
          else down[j][i]=cnt;
      }
  }
  dijkstra();
  cout<<dp[en.fi][en.se];

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 768 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 544 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 0 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 1664 KB Output is correct
10 Correct 1 ms 1664 KB Output is correct
11 Correct 2 ms 1792 KB Output is correct
12 Correct 2 ms 1664 KB Output is correct
13 Correct 2 ms 1664 KB Output is correct
14 Correct 0 ms 512 KB Output is correct
15 Correct 1 ms 1664 KB Output is correct
16 Correct 0 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 640 KB Output is correct
5 Correct 9 ms 6016 KB Output is correct
6 Correct 11 ms 5888 KB Output is correct
7 Correct 11 ms 5888 KB Output is correct
8 Correct 6 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 1664 KB Output is correct
10 Correct 2 ms 1664 KB Output is correct
11 Correct 1 ms 1664 KB Output is correct
12 Correct 1 ms 1664 KB Output is correct
13 Correct 2 ms 1664 KB Output is correct
14 Correct 9 ms 6016 KB Output is correct
15 Correct 11 ms 5888 KB Output is correct
16 Correct 11 ms 5888 KB Output is correct
17 Correct 10 ms 5884 KB Output is correct
18 Correct 12 ms 5628 KB Output is correct
19 Correct 12 ms 5376 KB Output is correct
20 Correct 12 ms 5376 KB Output is correct
21 Correct 9 ms 5928 KB Output is correct
22 Correct 10 ms 5804 KB Output is correct
23 Correct 10 ms 5848 KB Output is correct
24 Correct 10 ms 5248 KB Output is correct
25 Correct 0 ms 512 KB Output is correct
26 Correct 1 ms 1664 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 6 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 640 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 2 ms 1664 KB Output is correct
10 Correct 2 ms 1664 KB Output is correct
11 Correct 2 ms 1664 KB Output is correct
12 Correct 1 ms 1664 KB Output is correct
13 Correct 1 ms 1664 KB Output is correct
14 Correct 10 ms 6016 KB Output is correct
15 Correct 10 ms 5888 KB Output is correct
16 Correct 11 ms 5888 KB Output is correct
17 Correct 12 ms 5884 KB Output is correct
18 Correct 12 ms 5628 KB Output is correct
19 Correct 12 ms 5376 KB Output is correct
20 Correct 11 ms 5376 KB Output is correct
21 Correct 9 ms 5928 KB Output is correct
22 Correct 10 ms 5804 KB Output is correct
23 Correct 10 ms 5848 KB Output is correct
24 Correct 240 ms 25396 KB Output is correct
25 Correct 371 ms 25448 KB Output is correct
26 Correct 290 ms 25080 KB Output is correct
27 Correct 267 ms 25184 KB Output is correct
28 Correct 187 ms 42668 KB Output is correct
29 Correct 204 ms 27216 KB Output is correct
30 Correct 256 ms 25468 KB Output is correct
31 Correct 10 ms 5248 KB Output is correct
32 Correct 250 ms 24952 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 2 ms 1664 KB Output is correct
35 Correct 228 ms 25228 KB Output is correct
36 Correct 1 ms 512 KB Output is correct
37 Correct 6 ms 5888 KB Output is correct
38 Correct 112 ms 38664 KB Output is correct
39 Correct 183 ms 45356 KB Output is correct