#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
using namespace std;
void solve(){
int n, m;cin>>n>>m;n+=2;m+=2;
vector<vector<char>> v(n, vector<char>(m, '#'));
int s = -1, c = -1;
for(int i = 1;i<n-1;i++){
for(int j = 1;j<m-1;j++){
cin>>v[i][j];
if(v[i][j]=='S') s = i*m+j;
if(v[i][j]=='C') c = i*m+j;
}
}
vector<vector<pair<int, int>>> e(n*m);
vector<int> levo(n*m), desno(n*m), gore(n*m), dole(n*m);
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(v[i][j]=='#'){
levo[i*m+j] = j;
gore[i*m+j] = i;
} else{
levo[i*m+j] = levo[i*m+j-1];
gore[i*m+j] = gore[(i-1)*m+j];
}
}
}
for(int i = n-1;i>=0;i--){
for(int j = m-1;j>=0;j--){
if(v[i][j]=='#'){
desno[i*m+j] = j;
dole[i*m+j] = i;
} else{
desno[i*m+j] = desno[i*m+j+1];
dole[i*m+j] = dole[(i+1)*m+j];
}
}
}
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(v[i][j]=='#') continue;
e[i*m+j].push_back({(i-1)*m+j, 1});
e[i*m+j].push_back({i*m+j+1, 1});
e[i*m+j].push_back({(i+1)*m+j, 1});
e[i*m+j].push_back({i*m+j-1, 1});
int ii = dole[i*m+j], iii = gore[i*m+j], jj = desno[i*m+j], jjj = levo[i*m+j];
int x = min(min(ii-i, i-iii), min(jj-j, j-jjj));
ii--;
iii++;
jj--;
jjj++;
e[i*m+j].push_back({ii*m+j, x});
e[i*m+j].push_back({iii*m+j, x});
e[i*m+j].push_back({i*m+jj, x});
e[i*m+j].push_back({i*m+jjj, x});
}
}
priority_queue<pair<int, int>> q;
vector<int> d(n*m, 1'000'000'000);
d[s] = 0;
q.push({-d[s], s});
while(!q.empty()){
int u = q.top().second;
q.pop();
for(int i = 0;i<e[u].size();i++){
int v = e[u][i].first;
if(d[u]+e[u][i].second<d[v]){
d[v] = d[u]+e[u][i].second;
q.push({-d[v], v});
}
}
}
cout<<d[c]<<endl;
return;
}
int32_t main(){
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
int t = 1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
portals.cpp: In function 'void solve()':
portals.cpp:68:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i = 0;i<e[u].size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
504 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
9 ms |
3928 KB |
Output is correct |
6 |
Correct |
8 ms |
4164 KB |
Output is correct |
7 |
Correct |
8 ms |
4132 KB |
Output is correct |
8 |
Correct |
4 ms |
4332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
756 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
6 ms |
3932 KB |
Output is correct |
15 |
Correct |
9 ms |
3932 KB |
Output is correct |
16 |
Correct |
10 ms |
4340 KB |
Output is correct |
17 |
Correct |
10 ms |
4264 KB |
Output is correct |
18 |
Correct |
8 ms |
4700 KB |
Output is correct |
19 |
Correct |
12 ms |
5332 KB |
Output is correct |
20 |
Correct |
12 ms |
5428 KB |
Output is correct |
21 |
Correct |
7 ms |
3936 KB |
Output is correct |
22 |
Correct |
9 ms |
4088 KB |
Output is correct |
23 |
Correct |
8 ms |
4188 KB |
Output is correct |
24 |
Correct |
8 ms |
5360 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
5 ms |
4268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
368 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
612 KB |
Output is correct |
10 |
Correct |
1 ms |
616 KB |
Output is correct |
11 |
Correct |
1 ms |
616 KB |
Output is correct |
12 |
Correct |
1 ms |
616 KB |
Output is correct |
13 |
Correct |
1 ms |
616 KB |
Output is correct |
14 |
Correct |
7 ms |
3944 KB |
Output is correct |
15 |
Correct |
8 ms |
4184 KB |
Output is correct |
16 |
Correct |
8 ms |
4200 KB |
Output is correct |
17 |
Correct |
9 ms |
4200 KB |
Output is correct |
18 |
Correct |
9 ms |
4532 KB |
Output is correct |
19 |
Correct |
15 ms |
5436 KB |
Output is correct |
20 |
Correct |
10 ms |
5412 KB |
Output is correct |
21 |
Correct |
8 ms |
3944 KB |
Output is correct |
22 |
Correct |
8 ms |
3944 KB |
Output is correct |
23 |
Correct |
9 ms |
4200 KB |
Output is correct |
24 |
Correct |
235 ms |
98216 KB |
Output is correct |
25 |
Correct |
356 ms |
123160 KB |
Output is correct |
26 |
Correct |
313 ms |
123228 KB |
Output is correct |
27 |
Correct |
297 ms |
122928 KB |
Output is correct |
28 |
Correct |
176 ms |
89420 KB |
Output is correct |
29 |
Correct |
216 ms |
90404 KB |
Output is correct |
30 |
Correct |
225 ms |
91380 KB |
Output is correct |
31 |
Correct |
11 ms |
5208 KB |
Output is correct |
32 |
Correct |
305 ms |
122792 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
258 ms |
103516 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
4 ms |
4188 KB |
Output is correct |
38 |
Correct |
125 ms |
96852 KB |
Output is correct |
39 |
Correct |
201 ms |
83796 KB |
Output is correct |