#include <bits/stdc++.h>
using namespace std;
const int mxN=1e3;
int n, m, nb[4][2]={{-1, 0}, {0, -1}, {1, 0}, {0, 1}}, b[mxN][mxN][4], d1[mxN][mxN], d2[mxN][mxN];
string g[mxN];
vector<array<int, 2>> tp[mxN*mxN];
__attribute__((always_inline)) bool a(int i, int j) {
return i>=0&&i<n&&j>=0&&j<m&&g[i][j]!='#';
}
void bfs(int d[mxN][mxN], bool c) {
for(int it=0; it<n*m; ++it) {
for(array<int, 2> u : tp[it]) {
int i=u[0], j=u[1];
if(c&&g[i][j]=='C') {
cout << d[i][j];
exit(0);
}
if(d[i][j]<it)
continue;
// if(c)
// cout << i << " " << j << " " << it << endl;
for(int k=0; k<4; ++k) {
if(!a(i+nb[k][0], j+nb[k][1]))
continue;
int ni=i+nb[k][0], nj=j+nb[k][1];
if(d[ni][nj]>it+1) {
tp[it+1].push_back({ni, nj});
d[ni][nj]=it+1;
}
ni=i+nb[k][0]*b[i][j][k], nj=j+nb[k][1]*b[i][j][k];
if(c&&d[ni][nj]>it+d1[i][j]) {
tp[it+d1[i][j]].push_back({ni, nj});
d[ni][nj]=it+d1[i][j];
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=0; i<n; ++i) {
cin >> g[i];
for(int j=0; j<m; ++j)
if(a(i, j))
for(int k=0; k<2; ++k)
b[i][j][k]=a(i+nb[k][0], j+nb[k][1])?b[i+nb[k][0]][j+nb[k][1]][k]+1:0;
}
for(int i=n-1; i>=0; --i) {
memset(d1[i], 0x3F, 4*m);
for(int j=m-1; j>=0; --j) {
if(!a(i, j))
continue;
for(int k=2; k<4; ++k)
b[i][j][k]=a(i+nb[k][0], j+nb[k][1])?b[i+nb[k][0]][j+nb[k][1]][k]+1:0;
for(int k=0; k<4; ++k) {
if(!a(i+nb[k][0], j+nb[k][1])) {
d1[i][j]=1;
tp[1].push_back({i, j});
break;
}
}
}
}
bfs(d1, 0);
// for(int i=0; i<n; ++i) {
// for(int j=0; j<m; ++j)
// cout << d1[i][j] << " ";
// cout << endl;
// }
for(int i=0; i<n; ++i) {
memset(d2[i], 0x3F, 4*m);
for(int j=0; j<m; ++j) {
tp[i*m+j].clear();
if(g[i][j]=='S') {
d2[i][j]=0;
tp[0].push_back({i, j});
}
}
}
bfs(d2, 1);
}
Compilation message
portals.cpp:9:37: warning: always_inline function might not be inlinable [-Wattributes]
__attribute__((always_inline)) bool a(int i, int j) {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
25 ms |
24160 KB |
Output is correct |
3 |
Correct |
23 ms |
24208 KB |
Output is correct |
4 |
Correct |
23 ms |
24208 KB |
Output is correct |
5 |
Correct |
23 ms |
24208 KB |
Output is correct |
6 |
Correct |
22 ms |
24208 KB |
Output is correct |
7 |
Correct |
23 ms |
24208 KB |
Output is correct |
8 |
Correct |
23 ms |
24256 KB |
Output is correct |
9 |
Correct |
23 ms |
24256 KB |
Output is correct |
10 |
Correct |
23 ms |
24256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
24256 KB |
Output is correct |
2 |
Correct |
23 ms |
24256 KB |
Output is correct |
3 |
Correct |
23 ms |
24256 KB |
Output is correct |
4 |
Correct |
23 ms |
24256 KB |
Output is correct |
5 |
Correct |
23 ms |
24256 KB |
Output is correct |
6 |
Correct |
23 ms |
24256 KB |
Output is correct |
7 |
Correct |
23 ms |
24256 KB |
Output is correct |
8 |
Correct |
22 ms |
24256 KB |
Output is correct |
9 |
Correct |
23 ms |
24840 KB |
Output is correct |
10 |
Correct |
23 ms |
24844 KB |
Output is correct |
11 |
Correct |
25 ms |
24848 KB |
Output is correct |
12 |
Correct |
24 ms |
24852 KB |
Output is correct |
13 |
Correct |
26 ms |
24856 KB |
Output is correct |
14 |
Correct |
22 ms |
24856 KB |
Output is correct |
15 |
Correct |
23 ms |
24864 KB |
Output is correct |
16 |
Correct |
24 ms |
24864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
24864 KB |
Output is correct |
2 |
Correct |
23 ms |
24864 KB |
Output is correct |
3 |
Correct |
22 ms |
24864 KB |
Output is correct |
4 |
Correct |
23 ms |
24864 KB |
Output is correct |
5 |
Correct |
31 ms |
27688 KB |
Output is correct |
6 |
Correct |
30 ms |
27688 KB |
Output is correct |
7 |
Correct |
29 ms |
27688 KB |
Output is correct |
8 |
Correct |
31 ms |
28196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
28196 KB |
Output is correct |
2 |
Correct |
23 ms |
28196 KB |
Output is correct |
3 |
Correct |
25 ms |
28196 KB |
Output is correct |
4 |
Correct |
23 ms |
28196 KB |
Output is correct |
5 |
Correct |
23 ms |
28196 KB |
Output is correct |
6 |
Correct |
22 ms |
28196 KB |
Output is correct |
7 |
Correct |
23 ms |
28196 KB |
Output is correct |
8 |
Correct |
24 ms |
28196 KB |
Output is correct |
9 |
Correct |
24 ms |
28196 KB |
Output is correct |
10 |
Correct |
23 ms |
28196 KB |
Output is correct |
11 |
Correct |
24 ms |
28196 KB |
Output is correct |
12 |
Correct |
24 ms |
28196 KB |
Output is correct |
13 |
Correct |
25 ms |
28196 KB |
Output is correct |
14 |
Correct |
31 ms |
28196 KB |
Output is correct |
15 |
Correct |
30 ms |
28196 KB |
Output is correct |
16 |
Correct |
31 ms |
28196 KB |
Output is correct |
17 |
Correct |
30 ms |
28196 KB |
Output is correct |
18 |
Correct |
31 ms |
28196 KB |
Output is correct |
19 |
Correct |
29 ms |
28196 KB |
Output is correct |
20 |
Correct |
29 ms |
28196 KB |
Output is correct |
21 |
Correct |
32 ms |
28196 KB |
Output is correct |
22 |
Correct |
31 ms |
28196 KB |
Output is correct |
23 |
Correct |
32 ms |
28196 KB |
Output is correct |
24 |
Correct |
33 ms |
28472 KB |
Output is correct |
25 |
Correct |
24 ms |
28472 KB |
Output is correct |
26 |
Correct |
28 ms |
28472 KB |
Output is correct |
27 |
Correct |
24 ms |
28472 KB |
Output is correct |
28 |
Correct |
31 ms |
28904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
28904 KB |
Output is correct |
2 |
Correct |
23 ms |
28904 KB |
Output is correct |
3 |
Correct |
23 ms |
28904 KB |
Output is correct |
4 |
Correct |
23 ms |
28904 KB |
Output is correct |
5 |
Correct |
23 ms |
28904 KB |
Output is correct |
6 |
Correct |
24 ms |
28904 KB |
Output is correct |
7 |
Correct |
23 ms |
28904 KB |
Output is correct |
8 |
Correct |
25 ms |
28904 KB |
Output is correct |
9 |
Correct |
24 ms |
28904 KB |
Output is correct |
10 |
Correct |
25 ms |
28904 KB |
Output is correct |
11 |
Correct |
25 ms |
28904 KB |
Output is correct |
12 |
Correct |
26 ms |
28904 KB |
Output is correct |
13 |
Correct |
24 ms |
28904 KB |
Output is correct |
14 |
Correct |
31 ms |
28904 KB |
Output is correct |
15 |
Correct |
31 ms |
28904 KB |
Output is correct |
16 |
Correct |
31 ms |
28904 KB |
Output is correct |
17 |
Correct |
30 ms |
28904 KB |
Output is correct |
18 |
Correct |
32 ms |
28904 KB |
Output is correct |
19 |
Correct |
30 ms |
28904 KB |
Output is correct |
20 |
Correct |
30 ms |
28904 KB |
Output is correct |
21 |
Correct |
31 ms |
28904 KB |
Output is correct |
22 |
Correct |
31 ms |
28904 KB |
Output is correct |
23 |
Correct |
30 ms |
28904 KB |
Output is correct |
24 |
Correct |
184 ms |
63288 KB |
Output is correct |
25 |
Correct |
218 ms |
64604 KB |
Output is correct |
26 |
Correct |
134 ms |
64604 KB |
Output is correct |
27 |
Correct |
174 ms |
64704 KB |
Output is correct |
28 |
Correct |
193 ms |
66560 KB |
Output is correct |
29 |
Correct |
190 ms |
67020 KB |
Output is correct |
30 |
Correct |
176 ms |
67020 KB |
Output is correct |
31 |
Correct |
31 ms |
67020 KB |
Output is correct |
32 |
Correct |
230 ms |
73368 KB |
Output is correct |
33 |
Correct |
24 ms |
73368 KB |
Output is correct |
34 |
Correct |
26 ms |
73368 KB |
Output is correct |
35 |
Correct |
165 ms |
73368 KB |
Output is correct |
36 |
Correct |
24 ms |
73368 KB |
Output is correct |
37 |
Correct |
33 ms |
73368 KB |
Output is correct |
38 |
Correct |
213 ms |
85672 KB |
Output is correct |
39 |
Correct |
112 ms |
85672 KB |
Output is correct |