#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second
int n, m;
inline int enc(int r, int c){ return (r-1)*m + c; }
char z[1010][1010];
int aL[1010][1010], aR[1010][1010], aD[1010][1010], aU[1010][1010];
void bm(){
for(int i=1; i<=n; ++i){
aL[i][0] = 1;
for(int j=1; j<=m; ++j){
aL[i][j] = aL[i][j-1];
if(z[i][j] == '#') aL[i][j]=j+1;
}
aR[i][m+1] = m;
for(int j=m; 1<=j; --j){
aR[i][j] = aR[i][j+1];
if(z[i][j] == '#') aR[i][j]=j-1;
}
}
for(int j=1; j<=m; ++j){
aU[0][j] = 1;
for(int i=1; i<=n; ++i){
aU[i][j] = aU[i-1][j];
if(z[i][j] == '#') aU[i][j]=i+1;
}
aD[n+1][j] = n;
for(int i=n; 1<=i; --i){
aD[i][j] = aD[i+1][j];
if(z[i][j] == '#') aD[i][j]=i-1;
}
}
}
vector<pp> e[1001*1001];
void be(){
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
if(z[i][j]=='#') continue;
int nd = 1e9;
nd = min(nd, j - aL[i][j]);
nd = min(nd, i - aU[i][j]);
nd = min(nd, aR[i][j] - j);
nd = min(nd, aD[i][j] - i);
nd += 1;
int me = enc(i, j);
auto f = [&](int a, int b){
//printf("%d,%d -> %d,%d\n", i, j, a, b);
e[me].eb(enc(a, b), nd);
};
f(i, aL[i][j]);
f(i, aR[i][j]);
f(aU[i][j], j);
f(aD[i][j], j);
//putchar(10);
}
}
}
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
pp sp, ep;
priority_queue<pp> pq;
int dist[1001*1001];
int main()
{
read(n, m);
for(int i=1; i<=n; ++i){
scanf("%s", z[i]+1);
for(int j=1; j<=m; ++j){
if(z[i][j] == 'S') sp={i, j};
if(z[i][j] == 'C') ep={i, j};
}
}
bm();
be();
int V = n*m;
for(int i=1; i<=V; ++i) dist[i] = 1e9;
int s = enc(sp.x, sp.y);
dist[s]=0; pq.emplace(0, s);
while(pq.size()){
int d, p; tie(d, p)=pq.top(); pq.pop();
if(-d != dist[p]) continue;
int x=1+(p-1)/m, y=p-(x-1)*m;
for(int d=0; d<4; ++d){
int nx=x+dx[d], ny=y+dy[d];
if(1<=nx && 1<=ny && nx<=n && ny<=m && z[nx][ny] != '#'){
int np=enc(nx, ny);
if(dist[np] > dist[p]+1){
dist[np] = dist[p] + 1;
pq.emplace(-dist[np], np);
}
}
}
for(auto t:e[p]){
int q, d; tie(q, d)=t;
if(dist[q] > dist[p]+d){
dist[q] = dist[p]+d;
pq.emplace(-dist[q], q);
}
}
}
printf("%d\n", dist[enc(ep.x, ep.y)]);
return 0;
}
Compilation message
portals.cpp: In function 'void read(int&)':
portals.cpp:6:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
6 | void read(int& x){ scanf("%d",&x); }
| ~~~~~^~~~~~~~~
portals.cpp: In function 'void read(ll&)':
portals.cpp:7:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
7 | void read(ll& x){ scanf("%lld",&x); }
| ~~~~~^~~~~~~~~~~
portals.cpp: In function 'int main()':
portals.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
87 | scanf("%s", z[i]+1);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23928 KB |
Output is correct |
2 |
Correct |
16 ms |
24064 KB |
Output is correct |
3 |
Correct |
16 ms |
24064 KB |
Output is correct |
4 |
Correct |
19 ms |
23936 KB |
Output is correct |
5 |
Correct |
16 ms |
24064 KB |
Output is correct |
6 |
Correct |
18 ms |
24064 KB |
Output is correct |
7 |
Correct |
22 ms |
24064 KB |
Output is correct |
8 |
Correct |
18 ms |
24064 KB |
Output is correct |
9 |
Correct |
17 ms |
23968 KB |
Output is correct |
10 |
Correct |
16 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23936 KB |
Output is correct |
2 |
Correct |
14 ms |
24064 KB |
Output is correct |
3 |
Correct |
14 ms |
24064 KB |
Output is correct |
4 |
Correct |
35 ms |
23928 KB |
Output is correct |
5 |
Correct |
18 ms |
24064 KB |
Output is correct |
6 |
Correct |
18 ms |
24064 KB |
Output is correct |
7 |
Correct |
16 ms |
24064 KB |
Output is correct |
8 |
Correct |
16 ms |
24108 KB |
Output is correct |
9 |
Correct |
17 ms |
24832 KB |
Output is correct |
10 |
Correct |
17 ms |
24832 KB |
Output is correct |
11 |
Correct |
20 ms |
24832 KB |
Output is correct |
12 |
Correct |
17 ms |
24836 KB |
Output is correct |
13 |
Correct |
21 ms |
24832 KB |
Output is correct |
14 |
Correct |
20 ms |
23936 KB |
Output is correct |
15 |
Correct |
17 ms |
24832 KB |
Output is correct |
16 |
Correct |
16 ms |
23988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23936 KB |
Output is correct |
2 |
Correct |
16 ms |
24044 KB |
Output is correct |
3 |
Correct |
15 ms |
23988 KB |
Output is correct |
4 |
Correct |
17 ms |
24056 KB |
Output is correct |
5 |
Correct |
27 ms |
28536 KB |
Output is correct |
6 |
Correct |
29 ms |
28620 KB |
Output is correct |
7 |
Correct |
30 ms |
28672 KB |
Output is correct |
8 |
Correct |
27 ms |
28672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23936 KB |
Output is correct |
2 |
Correct |
19 ms |
24092 KB |
Output is correct |
3 |
Correct |
19 ms |
24064 KB |
Output is correct |
4 |
Correct |
18 ms |
23936 KB |
Output is correct |
5 |
Correct |
17 ms |
24064 KB |
Output is correct |
6 |
Correct |
23 ms |
24064 KB |
Output is correct |
7 |
Correct |
21 ms |
24064 KB |
Output is correct |
8 |
Correct |
18 ms |
24064 KB |
Output is correct |
9 |
Correct |
16 ms |
24832 KB |
Output is correct |
10 |
Correct |
19 ms |
24832 KB |
Output is correct |
11 |
Correct |
20 ms |
24832 KB |
Output is correct |
12 |
Correct |
23 ms |
24832 KB |
Output is correct |
13 |
Correct |
19 ms |
24832 KB |
Output is correct |
14 |
Correct |
30 ms |
28456 KB |
Output is correct |
15 |
Correct |
28 ms |
28544 KB |
Output is correct |
16 |
Correct |
30 ms |
28672 KB |
Output is correct |
17 |
Correct |
28 ms |
28672 KB |
Output is correct |
18 |
Correct |
29 ms |
28928 KB |
Output is correct |
19 |
Correct |
32 ms |
29312 KB |
Output is correct |
20 |
Correct |
32 ms |
29304 KB |
Output is correct |
21 |
Correct |
27 ms |
28544 KB |
Output is correct |
22 |
Correct |
41 ms |
28536 KB |
Output is correct |
23 |
Correct |
51 ms |
28544 KB |
Output is correct |
24 |
Correct |
31 ms |
29304 KB |
Output is correct |
25 |
Correct |
33 ms |
23928 KB |
Output is correct |
26 |
Correct |
18 ms |
24832 KB |
Output is correct |
27 |
Correct |
16 ms |
23936 KB |
Output is correct |
28 |
Correct |
28 ms |
28672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23936 KB |
Output is correct |
2 |
Correct |
18 ms |
24064 KB |
Output is correct |
3 |
Correct |
17 ms |
24064 KB |
Output is correct |
4 |
Correct |
16 ms |
23936 KB |
Output is correct |
5 |
Correct |
15 ms |
24064 KB |
Output is correct |
6 |
Correct |
19 ms |
24064 KB |
Output is correct |
7 |
Correct |
16 ms |
24064 KB |
Output is correct |
8 |
Correct |
18 ms |
24064 KB |
Output is correct |
9 |
Correct |
21 ms |
24832 KB |
Output is correct |
10 |
Correct |
48 ms |
24824 KB |
Output is correct |
11 |
Correct |
18 ms |
24832 KB |
Output is correct |
12 |
Correct |
29 ms |
24832 KB |
Output is correct |
13 |
Correct |
18 ms |
24880 KB |
Output is correct |
14 |
Correct |
33 ms |
28544 KB |
Output is correct |
15 |
Correct |
27 ms |
28544 KB |
Output is correct |
16 |
Correct |
29 ms |
28672 KB |
Output is correct |
17 |
Correct |
26 ms |
28672 KB |
Output is correct |
18 |
Correct |
36 ms |
28928 KB |
Output is correct |
19 |
Correct |
33 ms |
29312 KB |
Output is correct |
20 |
Correct |
40 ms |
29304 KB |
Output is correct |
21 |
Correct |
26 ms |
28544 KB |
Output is correct |
22 |
Correct |
25 ms |
28536 KB |
Output is correct |
23 |
Correct |
27 ms |
28544 KB |
Output is correct |
24 |
Correct |
339 ms |
77944 KB |
Output is correct |
25 |
Correct |
641 ms |
92792 KB |
Output is correct |
26 |
Correct |
508 ms |
92772 KB |
Output is correct |
27 |
Correct |
479 ms |
92536 KB |
Output is correct |
28 |
Correct |
265 ms |
72440 KB |
Output is correct |
29 |
Correct |
289 ms |
73208 KB |
Output is correct |
30 |
Correct |
330 ms |
73680 KB |
Output is correct |
31 |
Correct |
30 ms |
29312 KB |
Output is correct |
32 |
Correct |
455 ms |
92664 KB |
Output is correct |
33 |
Correct |
14 ms |
23936 KB |
Output is correct |
34 |
Correct |
15 ms |
24832 KB |
Output is correct |
35 |
Correct |
355 ms |
80888 KB |
Output is correct |
36 |
Correct |
14 ms |
23936 KB |
Output is correct |
37 |
Correct |
23 ms |
28672 KB |
Output is correct |
38 |
Correct |
263 ms |
76924 KB |
Output is correct |
39 |
Correct |
245 ms |
69240 KB |
Output is correct |