#include <bits/stdc++.h>
using namespace std;
const int N = 1003;
const int xx[4] = {1, 0, -1, 0};
const int yy[4] = {0, 1, 0, -1};
struct ban
{
int x, y, d;
ban(){}
ban(int x, int y, int d)
{
this->x = x;
this->y = y;
this->d = d;
}
};
bool operator<(const ban& a, const ban& b)
{
return a.d > b.d;
}
int n, m;
char a[N][N];
int sx, sy;
int cx, cy;
int l[N][N], r[N][N], u[N][N], d[N][N];
int minu[N][N];
bool c[N][N];
int djk()
{
priority_queue<ban> q;
q.push(ban(sx, sy, 0));
while (1)
{
ban t;
do
{
t = q.top();
q.pop();
} while (c[t.x][t.y]);
c[t.x][t.y] = true;
if (t.x == cx && t.y == cy)
return t.d;
for (int i = 0; i < 4; ++i)
{
ban h = t;
h.x += xx[i];
h.y += yy[i];
h.d++;
if (h.x >= 1 && h.x <= n && h.y >= 1 && h.y <= m && a[h.x][h.y] == '.' && !c[h.x][h.y])
{
q.push(h);
}
}
if (!c[t.x][r[t.x][t.y] - 1])
q.push(ban(t.x, r[t.x][t.y] - 1, t.d + minu[t.x][t.y]));
if (!c[t.x][l[t.x][t.y] + 1])
q.push(ban(t.x, l[t.x][t.y] + 1, t.d + minu[t.x][t.y]));
if (!c[u[t.x][t.y] + 1][t.y])
q.push(ban(u[t.x][t.y] + 1, t.y, t.d + minu[t.x][t.y]));
if (!c[d[t.x][t.y] - 1][t.y])
q.push(ban(d[t.x][t.y] - 1, t.y, t.d + minu[t.x][t.y]));
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> a[i][j];
if (a[i][j] == 'S')
{
a[i][j] = '.';
sx = i;
sy = j;
}
if (a[i][j] == 'C')
{
a[i][j] = '.';
cx = i;
cy = j;
}
}
}
for (int i = 0; i <= n + 1; ++i)
{
a[i][0] = '#';
a[i][m + 1] = '#';
}
for (int j = 0; j <= m + 1; ++j)
{
a[0][j] = '#';
a[n + 1][j] = '#';
}
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j <= m; ++j)
{
if (a[i][j] == '#')
l[i][j] = j;
else
l[i][j] = l[i][j - 1];
}
for (int j = m + 1; j >= 1; --j)
{
if (a[i][j] == '#')
r[i][j] = j;
else
r[i][j] = r[i][j + 1];
}
}
for (int j = 1; j <= m; ++j)
{
for (int i = 0; i <= n; ++i)
{
if (a[i][j] == '#')
u[i][j] = i;
else
u[i][j] = u[i - 1][j];
}
for (int i = n + 1; i >= 1; --i)
{
if (a[i][j] == '#')
d[i][j] = i;
else
d[i][j] = d[i + 1][j];
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (a[i][j] == '.')
minu[i][j] = min(min(j - l[i][j], r[i][j] - j), min(i - u[i][j], d[i][j] - i));
}
}
cout << djk() << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
2 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
632 KB |
Output is correct |
3 |
Correct |
2 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
3 ms |
636 KB |
Output is correct |
9 |
Correct |
4 ms |
1528 KB |
Output is correct |
10 |
Correct |
4 ms |
1656 KB |
Output is correct |
11 |
Correct |
3 ms |
1400 KB |
Output is correct |
12 |
Correct |
3 ms |
1400 KB |
Output is correct |
13 |
Correct |
3 ms |
1528 KB |
Output is correct |
14 |
Correct |
2 ms |
508 KB |
Output is correct |
15 |
Correct |
4 ms |
1400 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
632 KB |
Output is correct |
3 |
Correct |
2 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
11 ms |
4728 KB |
Output is correct |
6 |
Correct |
14 ms |
4728 KB |
Output is correct |
7 |
Correct |
11 ms |
4728 KB |
Output is correct |
8 |
Correct |
8 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
632 KB |
Output is correct |
3 |
Correct |
2 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
2 ms |
632 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
1528 KB |
Output is correct |
10 |
Correct |
4 ms |
1656 KB |
Output is correct |
11 |
Correct |
3 ms |
1528 KB |
Output is correct |
12 |
Correct |
3 ms |
1528 KB |
Output is correct |
13 |
Correct |
4 ms |
1528 KB |
Output is correct |
14 |
Correct |
11 ms |
4728 KB |
Output is correct |
15 |
Correct |
13 ms |
4752 KB |
Output is correct |
16 |
Correct |
11 ms |
4728 KB |
Output is correct |
17 |
Correct |
13 ms |
4728 KB |
Output is correct |
18 |
Correct |
14 ms |
4744 KB |
Output is correct |
19 |
Correct |
8 ms |
4728 KB |
Output is correct |
20 |
Correct |
17 ms |
6384 KB |
Output is correct |
21 |
Correct |
11 ms |
4728 KB |
Output is correct |
22 |
Correct |
11 ms |
4728 KB |
Output is correct |
23 |
Correct |
13 ms |
4816 KB |
Output is correct |
24 |
Correct |
15 ms |
4728 KB |
Output is correct |
25 |
Correct |
2 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
1532 KB |
Output is correct |
27 |
Correct |
3 ms |
504 KB |
Output is correct |
28 |
Correct |
8 ms |
4856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
632 KB |
Output is correct |
3 |
Correct |
2 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
6 |
Correct |
2 ms |
632 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
1528 KB |
Output is correct |
10 |
Correct |
4 ms |
1656 KB |
Output is correct |
11 |
Correct |
4 ms |
1528 KB |
Output is correct |
12 |
Correct |
4 ms |
1528 KB |
Output is correct |
13 |
Correct |
4 ms |
1400 KB |
Output is correct |
14 |
Correct |
11 ms |
4728 KB |
Output is correct |
15 |
Correct |
12 ms |
4728 KB |
Output is correct |
16 |
Correct |
11 ms |
4728 KB |
Output is correct |
17 |
Correct |
14 ms |
4728 KB |
Output is correct |
18 |
Correct |
16 ms |
4856 KB |
Output is correct |
19 |
Correct |
9 ms |
4728 KB |
Output is correct |
20 |
Correct |
17 ms |
6384 KB |
Output is correct |
21 |
Correct |
12 ms |
4728 KB |
Output is correct |
22 |
Correct |
11 ms |
4728 KB |
Output is correct |
23 |
Correct |
12 ms |
4728 KB |
Output is correct |
24 |
Correct |
280 ms |
23292 KB |
Output is correct |
25 |
Correct |
379 ms |
23796 KB |
Output is correct |
26 |
Correct |
102 ms |
28644 KB |
Output is correct |
27 |
Correct |
323 ms |
47432 KB |
Output is correct |
28 |
Correct |
161 ms |
23028 KB |
Output is correct |
29 |
Correct |
213 ms |
23004 KB |
Output is correct |
30 |
Correct |
192 ms |
23032 KB |
Output is correct |
31 |
Correct |
15 ms |
4728 KB |
Output is correct |
32 |
Correct |
335 ms |
23148 KB |
Output is correct |
33 |
Correct |
3 ms |
504 KB |
Output is correct |
34 |
Correct |
4 ms |
1528 KB |
Output is correct |
35 |
Correct |
161 ms |
23132 KB |
Output is correct |
36 |
Correct |
2 ms |
504 KB |
Output is correct |
37 |
Correct |
8 ms |
4728 KB |
Output is correct |
38 |
Correct |
109 ms |
23064 KB |
Output is correct |
39 |
Correct |
109 ms |
22908 KB |
Output is correct |