#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(n) n.begin(),n.end()
#define endl "\n"
#define pll pair<ll,ll>
#define ff first
#define ss second
using namespace std;
const ll maxn=2e3+500;
const ll mod=1e9+7 ;
const ll base=500;
/// con 9 ngay
ll d[maxn][maxn];
ll u[maxn][maxn];
ll r[maxn][maxn];
ll l[maxn][maxn];
ll d1[maxn][maxn];
ll dp[maxn][maxn];
char a[maxn][maxn];
ll ax[maxn]={1,0,-1,0};
ll ay[maxn]= {0,1,0,-1};
pll st;
pll ed;
ll n, m;
bool chk(ll i,ll j )
{
if (i<1||i>n) return false;
if (j<1||j>m) return false;
if (a[i][j]=='#') return false;
return true;
}
queue<pll> q;
void bfs()
{
while (!q.empty())
{
auto p= q.front();
q.pop();
ll x=p.ff;
ll y= p.ss;
for (int i=0;i<4;i++)
{
ll x1= x+ax[i];
ll y1= y+ay[i];
if (chk(x1,y1)&&d1[x1][y1]==1e15)
{
d1[x1][y1]=d1[x][y]+1;
q.push(make_pair(x1,y1));
}
}
}
}
void dosth()
{
priority_queue<pair<ll,pll>, vector<pair<ll,pll>>, greater<pair<ll,pll>>> q;
dp[st.ff][st.ss]=0;
// cout <<st.ff<<" "<<st.ss<<endl;
q.push(make_pair(dp[st.ff][st.ss],st));
while (!q.empty())
{
auto p= q.top();
q.pop();
if (dp[p.ss.ff][p.ss.ss]!=p.ff) continue;
ll x=p.ss.ff;
ll y=p.ss.ss;
// cout <<x<<" "<<y<<" "<<dp[x][y]<<endl;
for (int i=0;i<4;i++)
{
ll x1=x+ax[i];
ll y1=y+ay[i];
if (chk(x1,y1))
{
if (dp[x1][y1]>p.ff+1)
{
dp[x1][y1]=p.ff+1;
q.push(make_pair(dp[x1][y1],make_pair(x1,y1)));
}
}
}
if (dp[l[x][y]][y]>dp[x][y]+d1[x][y]+1)
{
dp[l[x][y]][y]=dp[x][y]+d1[x][y]+1;
q.push(make_pair(dp[l[x][y]][y],make_pair(l[x][y],y)));
}
if (dp[r[x][y]][y]>dp[x][y]+d1[x][y]+1)
{
dp[r[x][y]][y]=dp[x][y]+d1[x][y]+1;
q.push(make_pair(dp[r[x][y]][y],make_pair(r[x][y],y)));
}
if (dp[x][u[x][y]]>dp[x][y]+d1[x][y]+1)
{
dp[x][u[x][y]]=dp[x][y]+d1[x][y]+1;
q.push(make_pair(dp[x][u[x][y]],make_pair(x,u[x][y])));
}
if (dp[x][d[x][y]]>dp[x][y]+d1[x][y]+1)
{
dp[x][d[x][y]]=dp[x][y]+d1[x][y]+1;
q.push(make_pair(dp[x][d[x][y]],make_pair(x,d[x][y])));
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("test.txt","r",stdin);
if (fopen("Farm.INP","r"))
{
freopen("Farm.INP","r",stdin);
freopen("Farm.OUT","w",stdout);
}
cin>>n>>m;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
cin>>a[i][j];
d1[i][j]=1e15;
dp[i][j]=1e15;
if (a[i][j]=='S')
{
st=make_pair(i,j);
}
if (a[i][j]=='C')
{
ed=make_pair(i,j);
}
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (a[i][j]=='#')
{
for (int k=0;k<4;k++)
{
ll x1=ax[k]+i;
ll y1=ay[k]+j;
if (chk(x1,y1))
{
d1[x1][y1]=0;
q.push(make_pair(x1,y1));
}
}
}
else
{
if (i==1||i==n||j==1||j==m)
{
d1[i][j]=0;
q.push(make_pair(i,j));
}
}
}
}
bfs();
for (int i=1;i<=n;i++)
{
ll pre=1;
for (int j=1;j<=m;j++)
{
if (a[i][j]=='#') pre=j+1;
else
{
u[i][j]=pre;
}
}
pre=m;
for (int j=m;j>=1;j--)
{
if (a[i][j]=='#') pre=j-1;
else
{
d[i][j]=pre;
}
}
}
for (int j=1;j<=m;j++)
{
ll pre=1;
for (int i=1;i<=n;i++)
{
if (a[i][j]=='#') pre=i+1;
else
{
l[i][j]=pre;
}
}
pre=n;
for (int i=n;i>=1;i--)
{
if (a[i][j]=='#') pre=i-1;
else
{
r[i][j]=pre;
}
}
}
/* for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
cout <<l[i][j]<<" "<<r[i][j]<<" "<<u[i][j]<<" "<<d[i][j]<<" "<<d1[i][j]<<endl;
}
}*/
dosth();
// cout <<ed.ff<<" "<<ed.ss<<endl;
cout <<dp[ed.ff][ed.ss]<<endl;
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
118 | freopen("Farm.INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
portals.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
119 | freopen("Farm.OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
2 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
2 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1920 KB |
Output is correct |
13 |
Correct |
2 ms |
1920 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
1792 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
13 ms |
8320 KB |
Output is correct |
6 |
Correct |
13 ms |
8320 KB |
Output is correct |
7 |
Correct |
14 ms |
8320 KB |
Output is correct |
8 |
Correct |
9 ms |
8448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
2 ms |
1920 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
2 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1920 KB |
Output is correct |
13 |
Correct |
2 ms |
1920 KB |
Output is correct |
14 |
Correct |
12 ms |
8404 KB |
Output is correct |
15 |
Correct |
13 ms |
8352 KB |
Output is correct |
16 |
Correct |
15 ms |
8320 KB |
Output is correct |
17 |
Correct |
12 ms |
8064 KB |
Output is correct |
18 |
Correct |
16 ms |
8064 KB |
Output is correct |
19 |
Correct |
14 ms |
7680 KB |
Output is correct |
20 |
Correct |
14 ms |
7680 KB |
Output is correct |
21 |
Correct |
12 ms |
8140 KB |
Output is correct |
22 |
Correct |
12 ms |
8140 KB |
Output is correct |
23 |
Correct |
13 ms |
8176 KB |
Output is correct |
24 |
Correct |
13 ms |
7712 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
1792 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
10 ms |
8448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
2 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
2 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1920 KB |
Output is correct |
13 |
Correct |
2 ms |
1920 KB |
Output is correct |
14 |
Correct |
12 ms |
8320 KB |
Output is correct |
15 |
Correct |
15 ms |
8372 KB |
Output is correct |
16 |
Correct |
14 ms |
8320 KB |
Output is correct |
17 |
Correct |
12 ms |
8064 KB |
Output is correct |
18 |
Correct |
14 ms |
8064 KB |
Output is correct |
19 |
Correct |
29 ms |
7680 KB |
Output is correct |
20 |
Correct |
14 ms |
7680 KB |
Output is correct |
21 |
Correct |
12 ms |
8140 KB |
Output is correct |
22 |
Correct |
13 ms |
8140 KB |
Output is correct |
23 |
Correct |
13 ms |
8176 KB |
Output is correct |
24 |
Correct |
298 ms |
75304 KB |
Output is correct |
25 |
Correct |
529 ms |
75640 KB |
Output is correct |
26 |
Correct |
397 ms |
75256 KB |
Output is correct |
27 |
Correct |
417 ms |
75416 KB |
Output is correct |
28 |
Correct |
246 ms |
93612 KB |
Output is correct |
29 |
Correct |
276 ms |
77300 KB |
Output is correct |
30 |
Correct |
337 ms |
75536 KB |
Output is correct |
31 |
Correct |
14 ms |
7552 KB |
Output is correct |
32 |
Correct |
448 ms |
75000 KB |
Output is correct |
33 |
Correct |
1 ms |
512 KB |
Output is correct |
34 |
Correct |
2 ms |
1792 KB |
Output is correct |
35 |
Correct |
295 ms |
67692 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
9 ms |
8448 KB |
Output is correct |
38 |
Correct |
221 ms |
96684 KB |
Output is correct |
39 |
Correct |
231 ms |
75872 KB |
Output is correct |