/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int y[4]={0,0,1,-1},x[4]={1,-1,0,0};
int const N=1010;
char a[N][N]={};
int n,m;
int ans=1e9+10;
bool vis[N][N][8]={};
int dist[N][N]={};
pair<int,int>por[N][N][4]={};
int dis(pair<int,int>a,pair<int,int>b)
{
return abs(a.first-b.first)+abs(b.second-a.second);
}
bool check(int i,int j,int dir)
{
if (i<=0||i>n||j<=0||j>m||a[i][j]=='#')
return 0;
return 1;
}
void bfs(int i,int j)
{
set<vector<int>>ind;
ind.insert({0,i,j,-1});
while (ind.size())
{
vector<int>z=*begin(ind);
ind.erase(z);
if (a[z[1]][z[2]]=='C')
continue;
for (int i=0;i<4;i++)
{
int f=1e9+10;
for (int j=0;j<4;j++)
f=min(f,dis({z[1],z[2]},por[z[1]][z[2]][j])+(i!=j));
if (check(por[z[1]][z[2]][i].first,por[z[1]][z[2]][i].second,i+4)&&dist[por[z[1]][z[2]][i].first][por[z[1]][z[2]][i].second]>z[0]+f)
{
dist[por[z[1]][z[2]][i].first][por[z[1]][z[2]][i].second]=z[0]+f;
ind.insert({z[0]+f,por[z[1]][z[2]][i].first,por[z[1]][z[2]][i].second,i+4});
}
}
for (int i=0;i<4;i++)
{
if (check(z[1]+x[i],z[2]+y[i],i)&&dist[z[1]+x[i]][z[2]+y[i]]>z[0]+1)
{
dist[z[1]+x[i]][z[2]+y[i]]=z[0]+1;
ind.insert({z[0]+1,z[1]+x[i],z[2]+y[i],i});
}
}
}
}
inline void solve()
{
cin>>n>>m;
int sti=0,stj=0,eni=0,enj=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
cin>>a[i][j];
if (a[i][j]=='S')
sti=i,stj=j;
if (a[i][j]=='C')
eni=i,enj=j;
dist[i][j]=1e9+10;
}
for (int i=0;i<=n;i++)
{
a[i][0]='#';
a[i][m+1]='#';
}
for (int i=0;i<=m;i++)
{
a[0][i]='#';
a[n+1][i]='#';
}
for (int j=1;j<=m;j++)
for (int i=1;i<=n;i++)
{
if (a[i][j]!='#')
{
int k=i;
while (a[k][j]!='#')
k++;
k--;
for (int l=i;l<=k;+l++)
{
por[l][j][0]={i,j};
por[l][j][1]={k,j};
}
i=k;
}
}
for (int j=1;j<=n;j++)
{
for (int i=1;i<=m;i++)
{
if (a[j][i]!='#')
{
int k=i;
while (a[j][k]!='#')
k++;
k--;
for (int l=i;l<=k;l++)
{
por[j][l][2]={j,i};
por[j][l][3]={j,k};
}
i=k;
}
}
}
pair<int,int>f={1,5};
dist[sti][stj]=0;
bfs(sti,stj);
cout<<dist[eni][enj]<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
Compilation message
portals.cpp: In function 'void solve()':
portals.cpp:94:35: warning: value computed is not used [-Wunused-value]
94 | for (int l=i;l<=k;+l++)
| ^~~~
portals.cpp:121:18: warning: variable 'f' set but not used [-Wunused-but-set-variable]
121 | pair<int,int>f={1,5};
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4564 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
8 ms |
10844 KB |
Output is correct |
6 |
Correct |
9 ms |
11036 KB |
Output is correct |
7 |
Correct |
10 ms |
10844 KB |
Output is correct |
8 |
Correct |
6 ms |
10972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4440 KB |
Output is correct |
14 |
Correct |
8 ms |
10844 KB |
Output is correct |
15 |
Correct |
9 ms |
10844 KB |
Output is correct |
16 |
Correct |
11 ms |
11036 KB |
Output is correct |
17 |
Correct |
11 ms |
11052 KB |
Output is correct |
18 |
Correct |
14 ms |
11352 KB |
Output is correct |
19 |
Correct |
15 ms |
11112 KB |
Output is correct |
20 |
Correct |
13 ms |
11100 KB |
Output is correct |
21 |
Correct |
8 ms |
10844 KB |
Output is correct |
22 |
Correct |
9 ms |
10840 KB |
Output is correct |
23 |
Correct |
11 ms |
10844 KB |
Output is correct |
24 |
Correct |
12 ms |
11044 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
5 ms |
10832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4556 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4440 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
8 ms |
10832 KB |
Output is correct |
15 |
Correct |
10 ms |
10844 KB |
Output is correct |
16 |
Correct |
11 ms |
10844 KB |
Output is correct |
17 |
Correct |
12 ms |
10844 KB |
Output is correct |
18 |
Correct |
18 ms |
11312 KB |
Output is correct |
19 |
Correct |
15 ms |
11108 KB |
Output is correct |
20 |
Correct |
13 ms |
11100 KB |
Output is correct |
21 |
Correct |
8 ms |
10840 KB |
Output is correct |
22 |
Correct |
9 ms |
10844 KB |
Output is correct |
23 |
Correct |
13 ms |
11020 KB |
Output is correct |
24 |
Correct |
301 ms |
39892 KB |
Output is correct |
25 |
Correct |
415 ms |
40528 KB |
Output is correct |
26 |
Correct |
361 ms |
39900 KB |
Output is correct |
27 |
Correct |
355 ms |
40140 KB |
Output is correct |
28 |
Correct |
210 ms |
39492 KB |
Output is correct |
29 |
Correct |
219 ms |
39508 KB |
Output is correct |
30 |
Correct |
249 ms |
39544 KB |
Output is correct |
31 |
Correct |
13 ms |
10844 KB |
Output is correct |
32 |
Correct |
344 ms |
39760 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
261 ms |
39684 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
5 ms |
10844 KB |
Output is correct |
38 |
Correct |
120 ms |
39348 KB |
Output is correct |
39 |
Correct |
166 ms |
39408 KB |
Output is correct |