This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <stack>
#include <cassert>
#include <iterator>
#include <bitset>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(),v.end()
using namespace std;
int n,m,k,st,en;
vector<bool> used;
int u[1003][1003],d[1003][1003],l[1003][1003],r[1003][1003];
vector<pair<int,int>> g[1010006];
vector<int> dist;
char a[1003][1003];
int id(int y,int x)
{
return y*m+x;
}
void print(int ind)
{
int y,x;
x=ind%m;
if(!x)
x=m;
y=(ind-x)/m;
cout<<"! "<<y<<" "<<x<<" d="<<dist[ind]<<endl;
}
void addNode(int a,int b,int d)
{
g[a].PB(MP(b,d));
}
int main()
{
cin >> n >> m;
k=(m+1)*(n+1);
dist.resize(k+5);
used.resize(k+5);
for(int i=1;i<=n;i++)
{
int x=getchar();
for(int j=1;j<=m;j++)
{
x=getchar();
a[i][j]=(char)x;
if(a[i][j]=='S')
{
st=id(i,j);
a[i][j]='.';
}
else if(a[i][j]=='C')
{
en=id(i,j);
a[i][j]='.';
}
}
}
for(int i=1;i<=n;i++)
a[i][0]=a[i][m+1]='#';
for(int i=1;i<=m;i++)
a[0][i]=a[n+1][i]='#';
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
{
if(a[i][j]=='#')
{
u[i][j]=id(i+1,j);
l[i][j]=id(i,j+1);
}
else
{
u[i][j]=u[i-1][j];
l[i][j]=l[i][j-1];
}
}
for(int i=n+1;i>=1;i--)
for(int j=m+1;j>=1;j--)
{
if(a[i][j]=='#')
{
d[i][j]=id(i-1,j);
r[i][j]=id(i,j-1);
}
else
{
d[i][j]=d[i+1][j];
r[i][j]=r[i][j+1];
}
}
queue<int> Q;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]!='#' && a[i-1][j]=='#' || a[i+1][j]=='#' || a[i][j-1]=='#'|| a[i][j+1]=='#')
{
int start=id(i,j);
Q.push(start);
used[start]=1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(a[i][j]=='#')
continue;
if(a[i-1][j]!='#')
addNode(id(i,j),id(i-1,j),1);
if(a[i+1][j]!='#')
addNode(id(i,j),id(i+1,j),1);
if(a[i][j-1]!='#')
addNode(id(i,j),id(i,j-1),1);
if(a[i][j+1]!='#')
addNode(id(i,j),id(i,j+1),1);
}
while(!Q.empty())
{
int v=Q.front();
Q.pop();
//print(v);
for(int i=0;i<g[v].size();i++)
{
int to=g[v][i].first;
if(used[to])
continue;
used[to]=1;
dist[to]=dist[v]+1;
Q.push(to);
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(a[i][j]=='#')
continue;
if(1)
{
addNode(id(i,j),u[i][j],dist[id(i,j)]+1);
addNode(id(i,j),d[i][j],dist[id(i,j)]+1);
addNode(id(i,j),l[i][j],dist[id(i,j)]+1);
addNode(id(i,j),r[i][j],dist[id(i,j)]+1);
}
}
for(int i=1;i<=k;i++)
{
used[i]=0;
dist[i]=mod;
}
priority_queue<pair<int,int>> q;
q.push(MP(0,st));
dist[st]=0;
int ans=0;
while(!q.empty())
{
int v=-1;
int now=0;
do
{
v=q.top().second;
now=q.top().first;
q.pop();
}while(used[v] && !q.empty());
if(used[v]==1 && v==-1)
break;
used[v]=1;
if(v==en)
{
ans=-now;
break;
}
for(int i=0;i<g[v].size();i++)
{
int to=g[v][i].first;
int D=g[v][i].second;
if(!used[to])
{
q.push(MP(-(D-now),to));
}
}
}
cout<<ans<<endl;
return 0;
}
/*
15 10
.....C....
..........
..........
..........
..........
..........
..........
...S......
..........
..........
..........
..........
..........
..........
..........
*/
Compilation message (stderr)
portals.cpp: In function 'int main()':
portals.cpp:116:29: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if(a[i][j]!='#' && a[i-1][j]=='#' || a[i+1][j]=='#' || a[i][j-1]=='#'|| a[i][j+1]=='#')
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
portals.cpp:143:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++)
~^~~~~~~~~~~~
portals.cpp:198:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++)
~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |