#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)
{
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;
}
}
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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Incorrect |
1 ms |
768 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
416 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |