#include<bits/stdc++.h>
using namespace std;
const int N = 1009;
int n, m, D[N][N], F[N][N], qu[N * N];
int up[N][N], dn[N][N], le[N][N], ri[N][N];
int gx[] = {-1, 1, 0, 0}, gy[] = {0, 0, -1, 1};
char A[N][N];
priority_queue < pair < int , int > > Pq;
inline void Relax(int i, int j, int w)
{
if (A[i][j] == '.' && D[i][j] > w)
{
D[i][j] = w;
Pq.push({-D[i][j], i * N + j});
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%s", &A[i][1]);
pair < int , int > st, en;
memset(F, 63, sizeof(F));
int l = 0, r = 0;
for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= m + 1; j++)
{
if (A[i][j] == 'S')
st = {i, j}, A[i][j] = '.';
else if (A[i][j] == 'C')
en = {i, j}, A[i][j] = '.';
else if (A[i][j] != '.')
F[i][j] = 0, qu[r ++] = i * N + j;
}
while (r - l)
{
int i = qu[l] / N, j = qu[l ++] % N;
for (int h = 0; h < 4; h ++)
{
int tx = i + gx[h], ty = j + gy[h];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m)
if (F[tx][ty] > F[i][j] + 1)
F[tx][ty] = F[i][j] + 1, qu[r ++] = tx * N + ty;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
le[i][j] = (A[i][j] == '#') ? (0) : (le[i][j - 1] + 1);
for (int j = m; j; j --)
ri[i][j] = (A[i][j] == '#') ? (0) : (ri[i][j + 1] + 1);
}
for (int j = 1; j <= m; j++)
{
for (int i = 1; i <= n; i++)
up[i][j] = (A[i][j] == '#') ? (0) : (up[i - 1][j] + 1);
for (int i = n; i; i--)
dn[i][j] = (A[i][j] == '#') ? (0) : (dn[i + 1][j] + 1);
}
memset(D, 63, sizeof(D));
D[st.first][st.second] = 0;
Pq.push({0, st.first * N + st.second});
while (Pq.size())
{
int i = Pq.top().second / N, j = Pq.top().second % N;
int d = - Pq.top().first; Pq.pop();
if (d > D[i][j]) continue;
for (int h = 0; h < 4; h ++)
Relax(i + gx[h], j + gy[h], D[i][j] + 1);
Relax(i, j - le[i][j] + 1, D[i][j] + F[i][j]);
Relax(i, j + ri[i][j] - 1, D[i][j] + F[i][j]);
Relax(i - up[i][j] + 1, j, D[i][j] + F[i][j]);
Relax(i + dn[i][j] - 1, j, D[i][j] + F[i][j]);
}
return !printf("%d\n", D[en.first][en.second]);
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
portals.cpp:21:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", &A[i][1]);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8440 KB |
Output is correct |
2 |
Correct |
9 ms |
8448 KB |
Output is correct |
3 |
Correct |
1 ms |
8448 KB |
Output is correct |
4 |
Correct |
9 ms |
8448 KB |
Output is correct |
5 |
Correct |
11 ms |
8576 KB |
Output is correct |
6 |
Correct |
10 ms |
8448 KB |
Output is correct |
7 |
Correct |
10 ms |
8448 KB |
Output is correct |
8 |
Correct |
9 ms |
8576 KB |
Output is correct |
9 |
Correct |
11 ms |
8448 KB |
Output is correct |
10 |
Correct |
10 ms |
8320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8320 KB |
Output is correct |
2 |
Correct |
9 ms |
8448 KB |
Output is correct |
3 |
Correct |
9 ms |
8448 KB |
Output is correct |
4 |
Correct |
9 ms |
8448 KB |
Output is correct |
5 |
Correct |
10 ms |
8576 KB |
Output is correct |
6 |
Correct |
9 ms |
8420 KB |
Output is correct |
7 |
Correct |
10 ms |
8476 KB |
Output is correct |
8 |
Correct |
9 ms |
8448 KB |
Output is correct |
9 |
Correct |
12 ms |
9216 KB |
Output is correct |
10 |
Correct |
11 ms |
9216 KB |
Output is correct |
11 |
Correct |
10 ms |
9216 KB |
Output is correct |
12 |
Correct |
10 ms |
9216 KB |
Output is correct |
13 |
Correct |
10 ms |
9216 KB |
Output is correct |
14 |
Correct |
11 ms |
8448 KB |
Output is correct |
15 |
Correct |
11 ms |
9216 KB |
Output is correct |
16 |
Correct |
10 ms |
8320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8292 KB |
Output is correct |
2 |
Correct |
9 ms |
8448 KB |
Output is correct |
3 |
Correct |
10 ms |
8524 KB |
Output is correct |
4 |
Correct |
9 ms |
8448 KB |
Output is correct |
5 |
Correct |
17 ms |
11904 KB |
Output is correct |
6 |
Correct |
20 ms |
11904 KB |
Output is correct |
7 |
Correct |
22 ms |
11904 KB |
Output is correct |
8 |
Correct |
14 ms |
11824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8448 KB |
Output is correct |
2 |
Correct |
10 ms |
8448 KB |
Output is correct |
3 |
Correct |
9 ms |
8448 KB |
Output is correct |
4 |
Correct |
9 ms |
8448 KB |
Output is correct |
5 |
Correct |
11 ms |
8448 KB |
Output is correct |
6 |
Correct |
11 ms |
8448 KB |
Output is correct |
7 |
Correct |
10 ms |
8448 KB |
Output is correct |
8 |
Correct |
12 ms |
8448 KB |
Output is correct |
9 |
Correct |
12 ms |
9216 KB |
Output is correct |
10 |
Correct |
12 ms |
9216 KB |
Output is correct |
11 |
Correct |
11 ms |
9216 KB |
Output is correct |
12 |
Correct |
11 ms |
9216 KB |
Output is correct |
13 |
Correct |
10 ms |
9216 KB |
Output is correct |
14 |
Correct |
23 ms |
11904 KB |
Output is correct |
15 |
Correct |
22 ms |
11896 KB |
Output is correct |
16 |
Correct |
19 ms |
11904 KB |
Output is correct |
17 |
Correct |
17 ms |
11904 KB |
Output is correct |
18 |
Correct |
19 ms |
11904 KB |
Output is correct |
19 |
Correct |
20 ms |
11904 KB |
Output is correct |
20 |
Correct |
18 ms |
11904 KB |
Output is correct |
21 |
Correct |
16 ms |
11904 KB |
Output is correct |
22 |
Correct |
18 ms |
11896 KB |
Output is correct |
23 |
Correct |
18 ms |
11904 KB |
Output is correct |
24 |
Correct |
17 ms |
11904 KB |
Output is correct |
25 |
Correct |
9 ms |
8448 KB |
Output is correct |
26 |
Correct |
9 ms |
9336 KB |
Output is correct |
27 |
Correct |
10 ms |
8320 KB |
Output is correct |
28 |
Correct |
15 ms |
11904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
8348 KB |
Output is correct |
2 |
Correct |
11 ms |
8448 KB |
Output is correct |
3 |
Correct |
11 ms |
8448 KB |
Output is correct |
4 |
Correct |
11 ms |
8448 KB |
Output is correct |
5 |
Correct |
8 ms |
8448 KB |
Output is correct |
6 |
Correct |
9 ms |
8448 KB |
Output is correct |
7 |
Correct |
9 ms |
8448 KB |
Output is correct |
8 |
Correct |
10 ms |
8448 KB |
Output is correct |
9 |
Correct |
11 ms |
9088 KB |
Output is correct |
10 |
Correct |
11 ms |
9216 KB |
Output is correct |
11 |
Correct |
11 ms |
9088 KB |
Output is correct |
12 |
Correct |
10 ms |
9088 KB |
Output is correct |
13 |
Correct |
6 ms |
9216 KB |
Output is correct |
14 |
Correct |
20 ms |
11904 KB |
Output is correct |
15 |
Correct |
19 ms |
11904 KB |
Output is correct |
16 |
Correct |
19 ms |
11904 KB |
Output is correct |
17 |
Correct |
19 ms |
11904 KB |
Output is correct |
18 |
Correct |
21 ms |
11904 KB |
Output is correct |
19 |
Correct |
25 ms |
11904 KB |
Output is correct |
20 |
Correct |
24 ms |
11904 KB |
Output is correct |
21 |
Correct |
10 ms |
11904 KB |
Output is correct |
22 |
Correct |
20 ms |
11904 KB |
Output is correct |
23 |
Correct |
18 ms |
11904 KB |
Output is correct |
24 |
Correct |
233 ms |
30256 KB |
Output is correct |
25 |
Correct |
457 ms |
30388 KB |
Output is correct |
26 |
Correct |
291 ms |
30336 KB |
Output is correct |
27 |
Correct |
271 ms |
30200 KB |
Output is correct |
28 |
Correct |
167 ms |
30200 KB |
Output is correct |
29 |
Correct |
203 ms |
30068 KB |
Output is correct |
30 |
Correct |
217 ms |
30076 KB |
Output is correct |
31 |
Correct |
17 ms |
11884 KB |
Output is correct |
32 |
Correct |
273 ms |
30220 KB |
Output is correct |
33 |
Correct |
10 ms |
8448 KB |
Output is correct |
34 |
Correct |
11 ms |
9216 KB |
Output is correct |
35 |
Correct |
210 ms |
30096 KB |
Output is correct |
36 |
Correct |
10 ms |
8448 KB |
Output is correct |
37 |
Correct |
19 ms |
11904 KB |
Output is correct |
38 |
Correct |
160 ms |
29944 KB |
Output is correct |
39 |
Correct |
146 ms |
30080 KB |
Output is correct |