#include <bits/stdc++.h>
//#define int int64_t
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
int n, m;
vector<pair<int, int>>delta = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct wall{
int up, down, left, right;
};
int check(int i, int j, auto&graph, auto&dist, int op){
if(i < n && i>=0 && j < m && j>=0 && graph[i][j] != '#'){
if(op){
if(dist[i][j] == 1e9) return 1;
return 0;
}
return 1;
}
return 0;
}
vector<vector<int>>bfs(auto&graph, auto&start){
queue<pair<int, int>>q;
vector<vector<int>>dist(n, vector<int>(m, (int)1e9));
for(auto [x, y]: start){
q.push({x, y});
dist[x][y] = 0;
}
while(q.size()){
int i = q.front().first;
int j = q.front().second;
q.pop();
for(auto [x, y]: delta){
if(check(i+x, j+y, graph, dist, 1)){
dist[i+x][j+y] = dist[i][j] + 1;
q.push({i+x, j+y});
}
}
}
return dist;
}
int spfa(auto&next, auto&w, auto&graph, pair<int, int>s, pair<int, int>e){
vector<vector<int>>dist(n, vector<int>(m, 1e9));
queue<pair<int, int>>q;
q.push({s.first, s.second});
dist[s.first][s.second] = 0;
while(q.size()){
auto [i, j] = q.front();
q.pop();
for(auto [x, y]: delta){
if(check(i+x, j+y, graph, dist, 0) && dist[i][j]+1 < dist[i+x][j+y]){
dist[i+x][j+y] = dist[i][j]+1;
q.push({i+x, j+y});
}
}
int cur = next[i][j];
int nd = dist[i][j]+cur;
if(check(w[i][j].up, j, graph, dist, 0) && nd < dist[w[i][j].up][j]){
dist[w[i][j].up][j] = nd;
q.push({w[i][j].up, j});
}
if(check(w[i][j].down, j, graph, dist, 0) && nd < dist[w[i][j].down][j]){
dist[w[i][j].down][j] = nd;
q.push({w[i][j].down, j});
}
if(check(i, w[i][j].right, graph, dist, 0) && nd < dist[i][w[i][j].right]){
dist[i][w[i][j].right] = nd;
q.push({i, w[i][j].right});
}
if(check(i, w[i][j].left, graph, dist, 0) && nd < dist[i][w[i][j].left]){
dist[i][w[i][j].left] = nd;
q.push({i, w[i][j].left});
}
}
return dist[e.first][e.second];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin ("input.in");
//ofstream cout ("output.out");
cin>>n>>m;
m += 2;
vector<string>graph;
string border;
for(int i = 0; i<m; ++i) border += '#';
graph.push_back(border);
for(int i = 0; i<n; ++i){
string s; cin>>s;
graph.push_back("#");
for(auto u: s) graph[i+1] += u;
graph[i+1] += '#';
}
graph.push_back(border);
/*
for(auto u: graph){
for(auto v: u) cout<<v;
cout<<endl;
}
cout<<endl;
*/
n += 2;
vector<vector<wall>>w(n, vector<wall>(m));
pair<int, int>s, e;
vector<pair<int, int>>start;
for(int i = 0; i<n; ++i){
int cur = 0;
for(int j = 0; j<m; ++j){
if(graph[i][j] == 'S') s = {i, j};
if(graph[i][j] == 'C') e = {i, j};
if(graph[i][j] == '#'){
cur = j;
start.push_back({i, j});
}
w[i][j].left = cur+1;
}
cur = m-1;
for(int j = m-1; j>=0; --j){
if(graph[i][j] == '#') cur = j;
w[i][j].right = cur-1;
}
}
for(int j = 0; j<m; ++j){
int cur = 0;
for(int i = 0; i<n; ++i){
if(graph[i][j] == '#') cur = i;
w[i][j].up = cur+1;
}
cur = n-1;
for(int i = n-1; i>=0; --i){
if(graph[i][j] == '#') cur = i;
w[i][j].down = cur-1;
}
}
vector<vector<int>>next = bfs(graph, start);
/*
for(auto u: next){
for(auto v: u) cout<<v<<" ";
cout<<endl;
}
cout<<endl;
*/
cout<<spfa(next, w, graph, s, e);
cout<<'\n';
}
Compilation message
portals.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("O3")
|
portals.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
7 | #pragma GCC optimization ("unroll-loops")
|
portals.cpp:16:25: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
16 | int check(int i, int j, auto&graph, auto&dist, int op){
| ^~~~
portals.cpp:16:37: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
16 | int check(int i, int j, auto&graph, auto&dist, int op){
| ^~~~
portals.cpp:27:24: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
27 | vector<vector<int>>bfs(auto&graph, auto&start){
| ^~~~
portals.cpp:27:36: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
27 | vector<vector<int>>bfs(auto&graph, auto&start){
| ^~~~
portals.cpp: In function 'std::vector<std::vector<int> > bfs(auto:3&, auto:4&)':
portals.cpp:30:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
30 | for(auto [x, y]: start){
| ^
portals.cpp:40:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
40 | for(auto [x, y]: delta){
| ^
portals.cpp: At global scope:
portals.cpp:51:10: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
51 | int spfa(auto&next, auto&w, auto&graph, pair<int, int>s, pair<int, int>e){
| ^~~~
portals.cpp:51:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
51 | int spfa(auto&next, auto&w, auto&graph, pair<int, int>s, pair<int, int>e){
| ^~~~
portals.cpp:51:29: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
51 | int spfa(auto&next, auto&w, auto&graph, pair<int, int>s, pair<int, int>e){
| ^~~~
portals.cpp: In function 'int spfa(auto:5&, auto:6&, auto:7&, std::pair<int, int>, std::pair<int, int>)':
portals.cpp:58:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | auto [i, j] = q.front();
| ^
portals.cpp:61:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | for(auto [x, y]: delta){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
1476 KB |
Output is correct |
6 |
Correct |
6 ms |
1520 KB |
Output is correct |
7 |
Correct |
5 ms |
1492 KB |
Output is correct |
8 |
Correct |
4 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
1488 KB |
Output is correct |
15 |
Correct |
7 ms |
1488 KB |
Output is correct |
16 |
Correct |
5 ms |
1492 KB |
Output is correct |
17 |
Correct |
5 ms |
1492 KB |
Output is correct |
18 |
Correct |
6 ms |
1492 KB |
Output is correct |
19 |
Correct |
5 ms |
1400 KB |
Output is correct |
20 |
Correct |
6 ms |
1356 KB |
Output is correct |
21 |
Correct |
5 ms |
1492 KB |
Output is correct |
22 |
Correct |
6 ms |
1492 KB |
Output is correct |
23 |
Correct |
7 ms |
1496 KB |
Output is correct |
24 |
Correct |
4 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
320 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
6 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
216 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
6 ms |
1560 KB |
Output is correct |
15 |
Correct |
6 ms |
1488 KB |
Output is correct |
16 |
Correct |
9 ms |
1492 KB |
Output is correct |
17 |
Correct |
5 ms |
1492 KB |
Output is correct |
18 |
Correct |
7 ms |
1500 KB |
Output is correct |
19 |
Correct |
6 ms |
1364 KB |
Output is correct |
20 |
Correct |
6 ms |
1356 KB |
Output is correct |
21 |
Correct |
6 ms |
1488 KB |
Output is correct |
22 |
Correct |
5 ms |
1556 KB |
Output is correct |
23 |
Correct |
5 ms |
1496 KB |
Output is correct |
24 |
Correct |
159 ms |
29492 KB |
Output is correct |
25 |
Correct |
255 ms |
27148 KB |
Output is correct |
26 |
Correct |
186 ms |
27060 KB |
Output is correct |
27 |
Correct |
229 ms |
27012 KB |
Output is correct |
28 |
Correct |
129 ms |
31156 KB |
Output is correct |
29 |
Correct |
128 ms |
31100 KB |
Output is correct |
30 |
Correct |
141 ms |
31020 KB |
Output is correct |
31 |
Correct |
6 ms |
1364 KB |
Output is correct |
32 |
Correct |
135 ms |
26904 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
111 ms |
28872 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
4 ms |
1492 KB |
Output is correct |
38 |
Correct |
92 ms |
31172 KB |
Output is correct |
39 |
Correct |
97 ms |
31116 KB |
Output is correct |