//曇り空 のぞいた予感
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1010;
const ll mod = 1e9+7;
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int n , m;
char c[maxn][maxn];
int dist[maxn][maxn];
int nie[maxn][maxn];
struct tr{
int x , y , w;
friend bool operator < (tr a , tr b){
return a.w > b.w;
}
};
pii s , t;
vector < tr > adj[maxn][maxn];
priority_queue < tr > pq;
queue < pii > q;
bool mark[maxn][maxn];
void dijk(){
ms(dist , 63);
dist[s.first][s.second] = 0;
pq.push({s.first, s.second , 0});
while(pq.size()){
auto v = pq.top();
pq.pop();
if(mark[v.x][v.y])continue;
if(v.x == t.first and v.y == t.second)dokme(v.w);
mark[v.x][v.y] = 1;
for(tr u : adj[v.x][v.y])
if(dist[u.x][u.y] > v.w + u.w)
dist[u.x][u.y] = v.w + u.w , pq.push({u.x , u.y , v.w + u.w});
}
}
pii tmp[maxn][maxn];
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n >> m;
for(int i = 1 ; i <= n ; i ++){
cin >> (c[i]+1);
for(int j = 1 ; j <= m ; j ++)
if(c[i][j] == 'C')
t = {i , j};
else if(c[i][j] == 'S')
s = {i , j};
}
ms(nie , -1);
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 ; j <= m ; j ++)
if(c[i][j] == '#')
nie[i][j] = 0 , q.push({i , j});
for(int i = 1 ; i <= m ; i ++)
nie[0][i] = 0 , q.push({0 , i}),
nie[n + 1][i] = 0 , q.push({n + 1 , i});
for(int i = 1 ; i <= n ; i ++)
nie[i][0] = 0 , q.push({i , 0}),
nie[i][m+1] = 0 , q.push({i , m+1});
while(q.size()){
pii v = q.front();
q.pop();
if(v.first > 1){
if(nie[v.first - 1][v.second] == -1)
nie[v.first - 1][v.second] = nie[v.first][v.second] + 1 ,
q.push({v.first - 1 , v.second});
}
if(v.first < n){
if(nie[v.first + 1][v.second] == -1)
nie[v.first + 1][v.second] = nie[v.first][v.second] + 1,
q.push({v.first + 1 , v.second});
}
if(v.second > 1){
if(nie[v.first][v.second - 1] == -1)
nie[v.first][v.second - 1] = nie[v.first][v.second] + 1,
q.push({v.first , v.second-1});
}
if(v.second < m){
if(nie[v.first][v.second + 1] == -1)
nie[v.first][v.second + 1] = nie[v.first][v.second] + 1,
q.push({v.first , v.second+1});
}
}
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 ; j <= m ; j ++){
if(c[i][j] == '#')continue;
if(i > 1){
if(c[i-1][j] != '#')adj[i][j].pb({i-1 , j , 1});
}
if(i < n){
if(c[i+1][j] != '#')adj[i][j].pb({i+1 , j , 1});
}
if(j > 1){
if(c[i][j-1] != '#')adj[i][j].pb({i , j-1 , 1});
}
if(j < m){
if(c[i][j+1] != '#')adj[i][j].pb({i , j+1 , 1});
}
}
for(int i = 1 ; i <= n; i ++){
c[i][0] = '#';
for(int j = 1 ; j <= m ; j ++){
if(c[i][j] == '#')continue;
if(c[i][j-1] == '#')tmp[i][j] = {i , j};
else tmp[i][j] = tmp[i][j-1];
adj[i][j].pb({tmp[i][j].first , tmp[i][j].second , nie[i][j]});
}
}
for(int i = 1 ; i <= n; i ++){
c[i][m+1] = '#';
for(int j = m ; j >= 1; j --){
if(c[i][j] == '#')continue;
if(c[i][j+1] == '#')tmp[i][j] = {i , j};
else tmp[i][j] = tmp[i][j+1];
adj[i][j].pb({tmp[i][j].first , tmp[i][j].second , nie[i][j]});
}
}
for(int i = 1 ; i <= m; i ++){
c[0][i] = '#';
for(int j = 1 ; j <= n ; j ++){
if(c[j][i] == '#')continue;
if(c[j-1][i] == '#')tmp[j][i] = {j , i};
else tmp[j][i] = tmp[j-1][i];
adj[j][i].pb({tmp[j][i].first , tmp[j][i].second , nie[j][i]});
}
}
for(int i = 1 ; i <= m; i ++){
c[n+1][i] = '#';
for(int j = n ; j >= 1 ; j --){
if(c[j][i] == '#')continue;
if(c[j+1][i] == '#')tmp[j][i] = {j , i};
else tmp[j][i] = tmp[j+1][i];
adj[j][i].pb({tmp[j][i].first , tmp[j][i].second , nie[j][i]});
}
}
dijk();
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
32204 KB |
Output is correct |
2 |
Correct |
19 ms |
32268 KB |
Output is correct |
3 |
Correct |
19 ms |
32332 KB |
Output is correct |
4 |
Correct |
20 ms |
32276 KB |
Output is correct |
5 |
Correct |
19 ms |
32332 KB |
Output is correct |
6 |
Correct |
22 ms |
32332 KB |
Output is correct |
7 |
Correct |
20 ms |
32332 KB |
Output is correct |
8 |
Correct |
21 ms |
32336 KB |
Output is correct |
9 |
Correct |
20 ms |
32232 KB |
Output is correct |
10 |
Correct |
21 ms |
32296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
32204 KB |
Output is correct |
2 |
Correct |
20 ms |
32332 KB |
Output is correct |
3 |
Correct |
20 ms |
32224 KB |
Output is correct |
4 |
Correct |
23 ms |
32204 KB |
Output is correct |
5 |
Correct |
20 ms |
32360 KB |
Output is correct |
6 |
Correct |
23 ms |
32264 KB |
Output is correct |
7 |
Correct |
27 ms |
32340 KB |
Output is correct |
8 |
Correct |
24 ms |
32360 KB |
Output is correct |
9 |
Correct |
22 ms |
32864 KB |
Output is correct |
10 |
Correct |
22 ms |
32844 KB |
Output is correct |
11 |
Correct |
22 ms |
32744 KB |
Output is correct |
12 |
Correct |
22 ms |
32744 KB |
Output is correct |
13 |
Correct |
22 ms |
32844 KB |
Output is correct |
14 |
Correct |
20 ms |
32208 KB |
Output is correct |
15 |
Correct |
21 ms |
32740 KB |
Output is correct |
16 |
Correct |
22 ms |
32264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
32228 KB |
Output is correct |
2 |
Correct |
21 ms |
32344 KB |
Output is correct |
3 |
Correct |
20 ms |
32332 KB |
Output is correct |
4 |
Correct |
21 ms |
32352 KB |
Output is correct |
5 |
Correct |
35 ms |
36556 KB |
Output is correct |
6 |
Correct |
37 ms |
36576 KB |
Output is correct |
7 |
Correct |
38 ms |
36892 KB |
Output is correct |
8 |
Correct |
37 ms |
36736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
32228 KB |
Output is correct |
2 |
Correct |
21 ms |
32332 KB |
Output is correct |
3 |
Correct |
22 ms |
32324 KB |
Output is correct |
4 |
Correct |
21 ms |
32228 KB |
Output is correct |
5 |
Correct |
21 ms |
32332 KB |
Output is correct |
6 |
Correct |
20 ms |
32356 KB |
Output is correct |
7 |
Correct |
20 ms |
32332 KB |
Output is correct |
8 |
Correct |
20 ms |
32232 KB |
Output is correct |
9 |
Correct |
21 ms |
32904 KB |
Output is correct |
10 |
Correct |
22 ms |
32916 KB |
Output is correct |
11 |
Correct |
24 ms |
32740 KB |
Output is correct |
12 |
Correct |
22 ms |
32796 KB |
Output is correct |
13 |
Correct |
23 ms |
32844 KB |
Output is correct |
14 |
Correct |
40 ms |
36524 KB |
Output is correct |
15 |
Correct |
40 ms |
36548 KB |
Output is correct |
16 |
Correct |
37 ms |
36804 KB |
Output is correct |
17 |
Correct |
36 ms |
36936 KB |
Output is correct |
18 |
Correct |
39 ms |
37416 KB |
Output is correct |
19 |
Correct |
36 ms |
38092 KB |
Output is correct |
20 |
Correct |
43 ms |
38204 KB |
Output is correct |
21 |
Correct |
36 ms |
36444 KB |
Output is correct |
22 |
Correct |
36 ms |
36560 KB |
Output is correct |
23 |
Correct |
40 ms |
36804 KB |
Output is correct |
24 |
Correct |
41 ms |
38216 KB |
Output is correct |
25 |
Correct |
21 ms |
32224 KB |
Output is correct |
26 |
Correct |
20 ms |
32844 KB |
Output is correct |
27 |
Correct |
20 ms |
32228 KB |
Output is correct |
28 |
Correct |
35 ms |
36812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
32204 KB |
Output is correct |
2 |
Correct |
21 ms |
32328 KB |
Output is correct |
3 |
Correct |
21 ms |
32332 KB |
Output is correct |
4 |
Correct |
21 ms |
32224 KB |
Output is correct |
5 |
Correct |
20 ms |
32332 KB |
Output is correct |
6 |
Correct |
20 ms |
32292 KB |
Output is correct |
7 |
Correct |
19 ms |
32332 KB |
Output is correct |
8 |
Correct |
21 ms |
32332 KB |
Output is correct |
9 |
Correct |
22 ms |
32868 KB |
Output is correct |
10 |
Correct |
23 ms |
32920 KB |
Output is correct |
11 |
Correct |
24 ms |
32744 KB |
Output is correct |
12 |
Correct |
21 ms |
32748 KB |
Output is correct |
13 |
Correct |
21 ms |
32852 KB |
Output is correct |
14 |
Correct |
36 ms |
36468 KB |
Output is correct |
15 |
Correct |
38 ms |
36560 KB |
Output is correct |
16 |
Correct |
40 ms |
36840 KB |
Output is correct |
17 |
Correct |
36 ms |
36820 KB |
Output is correct |
18 |
Correct |
39 ms |
37444 KB |
Output is correct |
19 |
Correct |
36 ms |
38072 KB |
Output is correct |
20 |
Correct |
39 ms |
38244 KB |
Output is correct |
21 |
Correct |
35 ms |
36464 KB |
Output is correct |
22 |
Correct |
36 ms |
36544 KB |
Output is correct |
23 |
Correct |
37 ms |
36780 KB |
Output is correct |
24 |
Correct |
663 ms |
124292 KB |
Output is correct |
25 |
Correct |
915 ms |
153136 KB |
Output is correct |
26 |
Correct |
499 ms |
152260 KB |
Output is correct |
27 |
Correct |
631 ms |
152860 KB |
Output is correct |
28 |
Correct |
609 ms |
109924 KB |
Output is correct |
29 |
Correct |
623 ms |
111892 KB |
Output is correct |
30 |
Correct |
627 ms |
113512 KB |
Output is correct |
31 |
Correct |
41 ms |
38212 KB |
Output is correct |
32 |
Correct |
785 ms |
152784 KB |
Output is correct |
33 |
Correct |
21 ms |
32204 KB |
Output is correct |
34 |
Correct |
23 ms |
32804 KB |
Output is correct |
35 |
Correct |
570 ms |
126976 KB |
Output is correct |
36 |
Correct |
20 ms |
32204 KB |
Output is correct |
37 |
Correct |
36 ms |
36724 KB |
Output is correct |
38 |
Correct |
593 ms |
121672 KB |
Output is correct |
39 |
Correct |
441 ms |
97092 KB |
Output is correct |