#include<bits/stdc++.h>
using namespace std;
const int nmax=1e3+42;
int n,m;
char inp[nmax][nmax];
char get_in()
{
char c=getchar();
while(c!='.'&&c!='#'&&c!='S'&&c!='C')c=getchar();
return c;
}
bool in(int i,int j)
{
return 1<=i&&i<=n&&1<=j&&j<=m;
}
int nxt_left[nmax][nmax],nxt_right[nmax][nmax],nxt_up[nmax][nmax],nxt_down[nmax][nmax];
int dist[nmax][nmax];
priority_queue< pair<int, pair<int,int> > > pq;
int x_move[4]={1,-1,0,0};
int y_move[4]={0,0,1,-1};
int calc_dist(pair<int,int> a,pair<int,int> b)
{
return abs(a.first-b.first)+abs(a.second-b.second);
}
int main()
{
scanf("%i%i",&n,&m);
for(int i=0;i<=n+1;i++)
for(int j=0;j<=m+1;j++)
{
if(in(i,j)==0)inp[i][j]='#';
else inp[i][j]=get_in();
}
for(int i=0;i<=n+1;i++)
for(int j=0;j<=m+1;j++)
{
if(inp[i][j]=='#')nxt_left[i][j]=j;
else nxt_left[i][j]=nxt_left[i][j-1];
if(inp[i][j]=='#')nxt_up[i][j]=i;
else nxt_up[i][j]=nxt_up[i-1][j];
}
for(int i=n+1;i>=0;i--)
for(int j=m+1;j>=0;j--)
{
if(inp[i][j]=='#')nxt_right[i][j]=j;
else nxt_right[i][j]=nxt_right[i][j+1];
if(inp[i][j]=='#')nxt_down[i][j]=i;
else nxt_down[i][j]=nxt_down[i+1][j];
}
/*
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cout<<i<<" "<<j<<" -> "<<nxt_left[i][j]<<" "<<nxt_right[i][j]<<" "<<nxt_down[i][j]<<" "<<nxt_up[i][j]<<endl;
}
*/
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
dist[i][j]=1e9;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(inp[i][j]=='S')
{
pq.push({0,{i,j}});
dist[i][j]=0;
}
while(pq.size())
{
pair<int, pair<int,int> > tp=pq.top();
pq.pop();
int d_now=-tp.first;
int x=tp.second.first;
int y=tp.second.second;
if(dist[x][y]!=d_now)continue;
if(in(x,y)==0)continue;
//cout<<x<<" "<<y<<" -> "<<d_now<<endl;
dist[x][y]=d_now;
if(inp[x][y]=='C')
{
printf("%i\n",d_now);
return 0;
}
if(inp[x][y]=='#')continue;
//move adjacent
for(int t=0;t<4;t++)
{
int x_=x+x_move[t];
int y_=y+y_move[t];
if(in(x_,y_))
{
if(dist[x_][y_]<=d_now+1)continue;
pq.push({-(d_now+1),{x_,y_}});
dist[x_][y_]=d_now+1;
}
}
//use portals
pair<int,int> go[4];
go[0]={x,nxt_left[x][y]+1};
go[1]={x,nxt_right[x][y]-1};
go[2]={nxt_up[x][y]+1,y};
go[3]={nxt_down[x][y]-1,y};
for(int move_2=0;move_2<=3;move_2++)
{
//move (i,j) -> move_1 -> ( teleport ) move_2
int lowest=1e9;
for(int move_1=0;move_1<=3;move_1++)
lowest=min(lowest,calc_dist({x,y},go[move_1])+1);
if(dist[go[move_2].first][go[move_2].second]<=d_now+lowest)continue;
pq.push({-(d_now+lowest),go[move_2]});
dist[go[move_2].first][go[move_2].second]=d_now+lowest;
}
}
assert(0==1);
return 0;
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i%i",&n,&m);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
4 ms |
512 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
6 ms |
1536 KB |
Output is correct |
10 |
Correct |
5 ms |
1408 KB |
Output is correct |
11 |
Correct |
5 ms |
1536 KB |
Output is correct |
12 |
Correct |
6 ms |
1536 KB |
Output is correct |
13 |
Correct |
5 ms |
1536 KB |
Output is correct |
14 |
Correct |
4 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
1536 KB |
Output is correct |
16 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
4 ms |
640 KB |
Output is correct |
5 |
Correct |
13 ms |
4736 KB |
Output is correct |
6 |
Correct |
16 ms |
4608 KB |
Output is correct |
7 |
Correct |
12 ms |
4736 KB |
Output is correct |
8 |
Correct |
11 ms |
4608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
6 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
6 ms |
1408 KB |
Output is correct |
10 |
Correct |
5 ms |
1408 KB |
Output is correct |
11 |
Correct |
5 ms |
1408 KB |
Output is correct |
12 |
Correct |
7 ms |
1408 KB |
Output is correct |
13 |
Correct |
5 ms |
1408 KB |
Output is correct |
14 |
Correct |
13 ms |
4608 KB |
Output is correct |
15 |
Correct |
15 ms |
4736 KB |
Output is correct |
16 |
Correct |
12 ms |
4736 KB |
Output is correct |
17 |
Correct |
14 ms |
4608 KB |
Output is correct |
18 |
Correct |
14 ms |
4736 KB |
Output is correct |
19 |
Correct |
7 ms |
4608 KB |
Output is correct |
20 |
Correct |
11 ms |
4736 KB |
Output is correct |
21 |
Correct |
12 ms |
4608 KB |
Output is correct |
22 |
Correct |
13 ms |
4608 KB |
Output is correct |
23 |
Correct |
14 ms |
4608 KB |
Output is correct |
24 |
Correct |
14 ms |
4608 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
1408 KB |
Output is correct |
27 |
Correct |
5 ms |
512 KB |
Output is correct |
28 |
Correct |
9 ms |
4608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
6 ms |
1408 KB |
Output is correct |
10 |
Correct |
6 ms |
1536 KB |
Output is correct |
11 |
Correct |
5 ms |
1408 KB |
Output is correct |
12 |
Correct |
5 ms |
1408 KB |
Output is correct |
13 |
Correct |
5 ms |
1408 KB |
Output is correct |
14 |
Correct |
12 ms |
4608 KB |
Output is correct |
15 |
Correct |
16 ms |
4608 KB |
Output is correct |
16 |
Correct |
13 ms |
4712 KB |
Output is correct |
17 |
Correct |
13 ms |
4736 KB |
Output is correct |
18 |
Correct |
17 ms |
4736 KB |
Output is correct |
19 |
Correct |
8 ms |
4608 KB |
Output is correct |
20 |
Correct |
12 ms |
4736 KB |
Output is correct |
21 |
Correct |
12 ms |
4608 KB |
Output is correct |
22 |
Correct |
13 ms |
4736 KB |
Output is correct |
23 |
Correct |
15 ms |
4736 KB |
Output is correct |
24 |
Correct |
243 ms |
22008 KB |
Output is correct |
25 |
Correct |
326 ms |
23224 KB |
Output is correct |
26 |
Correct |
52 ms |
22908 KB |
Output is correct |
27 |
Correct |
148 ms |
23224 KB |
Output is correct |
28 |
Correct |
170 ms |
22776 KB |
Output is correct |
29 |
Correct |
203 ms |
22832 KB |
Output is correct |
30 |
Correct |
194 ms |
23032 KB |
Output is correct |
31 |
Correct |
14 ms |
4736 KB |
Output is correct |
32 |
Correct |
230 ms |
22776 KB |
Output is correct |
33 |
Correct |
5 ms |
512 KB |
Output is correct |
34 |
Correct |
5 ms |
1408 KB |
Output is correct |
35 |
Correct |
182 ms |
22848 KB |
Output is correct |
36 |
Correct |
5 ms |
512 KB |
Output is correct |
37 |
Correct |
9 ms |
4736 KB |
Output is correct |
38 |
Correct |
93 ms |
22776 KB |
Output is correct |
39 |
Correct |
159 ms |
22776 KB |
Output is correct |