#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
//#define int long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=1005;
const int INF=1e9;
int32_t main() {
LCBorz;
int n,m;cin>>n>>m;
vector<vector<char>> v(n+2,vector<char>(m+2,'#'));
auto id=[&](int a,int b){
return a*N+b;
};
auto valid=[&](int a,int b){
return v[a][b]!='#';
};
auto rev=[&](int id){
return make_pair(id/N,id%N);
};
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>v[i][j];
}
}
vector<int> adj[N*N];
for(int i=1;i<=n;i++){
int last=0;
for(int j=1;j<=m;j++){
if(!valid(i,j)){
last=j;
continue;
}
adj[id(i,j)].push_back(id(i,last+1));
}
last=m+1;
for(int j=m;j>=1;j--){
if(!valid(i,j)){
last=j;
continue;
}
adj[id(i,j)].push_back(id(i,last-1));
}
}
for(int i=1;i<=m;i++){
int last=0;
for(int j=1;j<=n;j++){
if(!valid(j,i)){
last=j;
continue;
}
adj[id(j,i)].push_back(id(last+1,i));
}
last=n+1;
for(int j=n;j>=1;j--){
if(!valid(j,i)){
last=j;
continue;
}
adj[id(j,i)].push_back(id(last-1,i));
}
}
vector<int> dx={0,0,-1,1},dy={1,-1,0,0};
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!valid(i,j))continue;
for(int k=0;k<4;k++){
int a=i+dx[k],b=j+dy[k];
if(!valid(a,b))continue;
adj[id(i,j)].push_back(id(a,b));
}
}
}
queue<int> q;
vector<int> dis(N*N,INF);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(v[i][j-1]=='#'||v[i][j+1]=='#'||v[i-1][j]=='#'||v[i+1][j]=='#'){
q.push(id(i,j));
dis[id(i,j)]=0;
}
}
}
while(q.size()){
int p=q.front();
q.pop();
for(int j:adj[p]){
if(dis[j]==INF){
dis[j]=dis[p]+1;
q.push(j);
}
}
}
priority_queue<pair<int,int>> pq;
vector dis1(N*N,INF);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(v[i][j]=='S'){
dis1[id(i,j)]=0;
pq.push({0,id(i,j)});
}
}
}
while(pq.size()){
auto [dis2,t]=pq.top();
pq.pop();
dis2=-dis2;
if(dis2!=dis1[t])continue;
for(int j:adj[t]){
int cost=dis1[t];
if(abs(j-t)==N||abs(j-t)==1)cost+=1;
else cost+=dis[t]+1;
if(dis1[j]>cost){
dis1[j]=cost;
pq.push({-dis1[j],j});
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(v[i][j]=='C'){
cout<<dis1[id(i,j)]<<endl;
}
}
}
return 0;
}
Compilation message
portals.cpp: In function 'int32_t main()':
portals.cpp:20:10: warning: variable 'rev' set but not used [-Wunused-but-set-variable]
20 | auto rev=[&](int id){
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
32036 KB |
Output is correct |
2 |
Correct |
24 ms |
32088 KB |
Output is correct |
3 |
Correct |
19 ms |
31836 KB |
Output is correct |
4 |
Correct |
18 ms |
31836 KB |
Output is correct |
5 |
Correct |
18 ms |
31832 KB |
Output is correct |
6 |
Correct |
19 ms |
31824 KB |
Output is correct |
7 |
Correct |
24 ms |
31984 KB |
Output is correct |
8 |
Correct |
19 ms |
32092 KB |
Output is correct |
9 |
Correct |
18 ms |
31864 KB |
Output is correct |
10 |
Correct |
21 ms |
31840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
31996 KB |
Output is correct |
2 |
Correct |
20 ms |
31840 KB |
Output is correct |
3 |
Correct |
18 ms |
32092 KB |
Output is correct |
4 |
Correct |
20 ms |
32088 KB |
Output is correct |
5 |
Correct |
19 ms |
32092 KB |
Output is correct |
6 |
Correct |
20 ms |
31984 KB |
Output is correct |
7 |
Correct |
20 ms |
32092 KB |
Output is correct |
8 |
Correct |
18 ms |
32092 KB |
Output is correct |
9 |
Correct |
19 ms |
32092 KB |
Output is correct |
10 |
Correct |
19 ms |
32092 KB |
Output is correct |
11 |
Correct |
19 ms |
32088 KB |
Output is correct |
12 |
Correct |
19 ms |
32088 KB |
Output is correct |
13 |
Correct |
19 ms |
32092 KB |
Output is correct |
14 |
Correct |
19 ms |
32024 KB |
Output is correct |
15 |
Correct |
19 ms |
32092 KB |
Output is correct |
16 |
Correct |
18 ms |
32096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
31832 KB |
Output is correct |
2 |
Correct |
19 ms |
32088 KB |
Output is correct |
3 |
Correct |
18 ms |
32088 KB |
Output is correct |
4 |
Correct |
24 ms |
32100 KB |
Output is correct |
5 |
Correct |
27 ms |
33372 KB |
Output is correct |
6 |
Correct |
28 ms |
33400 KB |
Output is correct |
7 |
Correct |
30 ms |
33540 KB |
Output is correct |
8 |
Correct |
34 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31836 KB |
Output is correct |
2 |
Correct |
18 ms |
32092 KB |
Output is correct |
3 |
Correct |
19 ms |
32092 KB |
Output is correct |
4 |
Correct |
22 ms |
32092 KB |
Output is correct |
5 |
Correct |
18 ms |
32044 KB |
Output is correct |
6 |
Correct |
19 ms |
31836 KB |
Output is correct |
7 |
Correct |
24 ms |
32092 KB |
Output is correct |
8 |
Correct |
19 ms |
32092 KB |
Output is correct |
9 |
Correct |
19 ms |
32092 KB |
Output is correct |
10 |
Correct |
19 ms |
32092 KB |
Output is correct |
11 |
Correct |
19 ms |
32092 KB |
Output is correct |
12 |
Correct |
20 ms |
31944 KB |
Output is correct |
13 |
Correct |
19 ms |
32092 KB |
Output is correct |
14 |
Correct |
27 ms |
33372 KB |
Output is correct |
15 |
Correct |
27 ms |
33364 KB |
Output is correct |
16 |
Correct |
29 ms |
33432 KB |
Output is correct |
17 |
Correct |
27 ms |
33372 KB |
Output is correct |
18 |
Correct |
29 ms |
33620 KB |
Output is correct |
19 |
Correct |
35 ms |
33880 KB |
Output is correct |
20 |
Correct |
39 ms |
33936 KB |
Output is correct |
21 |
Correct |
26 ms |
33372 KB |
Output is correct |
22 |
Correct |
28 ms |
33368 KB |
Output is correct |
23 |
Correct |
28 ms |
33372 KB |
Output is correct |
24 |
Correct |
29 ms |
33884 KB |
Output is correct |
25 |
Correct |
19 ms |
32088 KB |
Output is correct |
26 |
Correct |
19 ms |
32092 KB |
Output is correct |
27 |
Correct |
23 ms |
31848 KB |
Output is correct |
28 |
Correct |
32 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
32076 KB |
Output is correct |
2 |
Correct |
22 ms |
32092 KB |
Output is correct |
3 |
Correct |
19 ms |
32088 KB |
Output is correct |
4 |
Correct |
18 ms |
31836 KB |
Output is correct |
5 |
Correct |
25 ms |
32088 KB |
Output is correct |
6 |
Correct |
18 ms |
31924 KB |
Output is correct |
7 |
Correct |
19 ms |
32088 KB |
Output is correct |
8 |
Correct |
26 ms |
32092 KB |
Output is correct |
9 |
Correct |
20 ms |
32180 KB |
Output is correct |
10 |
Correct |
19 ms |
32200 KB |
Output is correct |
11 |
Correct |
23 ms |
32116 KB |
Output is correct |
12 |
Correct |
19 ms |
32088 KB |
Output is correct |
13 |
Correct |
19 ms |
32092 KB |
Output is correct |
14 |
Correct |
33 ms |
33372 KB |
Output is correct |
15 |
Correct |
27 ms |
33372 KB |
Output is correct |
16 |
Correct |
28 ms |
33360 KB |
Output is correct |
17 |
Correct |
27 ms |
33372 KB |
Output is correct |
18 |
Correct |
29 ms |
33616 KB |
Output is correct |
19 |
Correct |
35 ms |
33884 KB |
Output is correct |
20 |
Correct |
33 ms |
33872 KB |
Output is correct |
21 |
Correct |
35 ms |
33324 KB |
Output is correct |
22 |
Correct |
29 ms |
33360 KB |
Output is correct |
23 |
Correct |
28 ms |
33372 KB |
Output is correct |
24 |
Correct |
337 ms |
68020 KB |
Output is correct |
25 |
Correct |
498 ms |
81336 KB |
Output is correct |
26 |
Correct |
512 ms |
81396 KB |
Output is correct |
27 |
Correct |
463 ms |
80988 KB |
Output is correct |
28 |
Correct |
295 ms |
66768 KB |
Output is correct |
29 |
Correct |
321 ms |
67152 KB |
Output is correct |
30 |
Correct |
393 ms |
67860 KB |
Output is correct |
31 |
Correct |
31 ms |
33872 KB |
Output is correct |
32 |
Correct |
461 ms |
80940 KB |
Output is correct |
33 |
Correct |
23 ms |
31828 KB |
Output is correct |
34 |
Correct |
20 ms |
32092 KB |
Output is correct |
35 |
Correct |
438 ms |
71224 KB |
Output is correct |
36 |
Correct |
18 ms |
32088 KB |
Output is correct |
37 |
Correct |
24 ms |
33368 KB |
Output is correct |
38 |
Correct |
272 ms |
68044 KB |
Output is correct |
39 |
Correct |
223 ms |
57684 KB |
Output is correct |