#include <iostream>
#include <queue>
#define MAX 802
using namespace std;
struct bee
{
short int x;
short int y;
int t;
int r;
};
int N, S, sx, sy, ex, ey, B[MAX][MAX], vis[MAX][MAX][2], rilres = -1;
char A[MAX][MAX];
queue <bee> qiu;
bee temp;
void solve ()
{
while (!qiu.empty())
{
temp = qiu.front();
qiu.pop();
temp.t++;
if (B[temp.x - 1][temp.y] == -1 && A[temp.x - 1][temp.y] != 'T' && A[temp.x - 1][temp.y] != 'D')
{
B[temp.x - 1][temp.y] = temp.t;
temp.x--;
qiu.push(temp);
temp.x++;
}
if (B[temp.x + 1][temp.y] == -1 && A[temp.x + 1][temp.y] != 'T' && A[temp.x + 1][temp.y] != 'D')
{
B[temp.x + 1][temp.y] = temp.t;
temp.x++;
qiu.push(temp);
temp.x--;
}
if (B[temp.x][temp.y - 1] == -1 && A[temp.x][temp.y - 1] != 'T' && A[temp.x][temp.y - 1] != 'D')
{
B[temp.x][temp.y - 1] = temp.t;
temp.y--;
qiu.push(temp);
temp.y++;
}
if (B[temp.x][temp.y + 1] == -1 && A[temp.x][temp.y + 1] != 'T' && A[temp.x][temp.y + 1] != 'D')
{
B[temp.x][temp.y + 1] = temp.t;
temp.y++;
qiu.push(temp);
temp.y--;
}
}
}
int solve2(int x, int y, int t, int r)
{
temp.x = x;
temp.y = y;
temp.t = t;
temp.r = r;
qiu.push(temp);
while (!qiu.empty())
{
temp = qiu.front();
qiu.pop();
if (temp.x == ex && temp.y == ey)
rilres = max(rilres,temp.r);
if (vis[temp.x][temp.y][1] != temp.r)
continue;
if (temp.r + (temp.t / S) >= B[temp.x][temp.y])
temp.r -= ((temp.r + (temp.t / S)) - B[temp.x][temp.y] + 1);
if (temp.r < 0 || temp.r <= rilres)
continue;
temp.t++;
if (vis[temp.x + 1][temp.y][1] <= temp.r && A[temp.x + 1][temp.y] != 'T')
{
if ((vis[temp.x + 1][temp.y][1] == temp.r && vis[temp.x + 1][temp.y][0] > temp.t) || vis[temp.x + 1][temp.y][1] < temp.r)
{
vis[temp.x + 1][temp.y][1] = temp.r;
vis[temp.x + 1][temp.y][0] = temp.t;
temp.x++;
qiu.push(temp);
temp.x--;
}
}
if (vis[temp.x - 1][temp.y][1] <= temp.r && A[temp.x - 1][temp.y] != 'T')
{
if ((vis[temp.x - 1][temp.y][1] == temp.r && vis[temp.x - 1][temp.y][0] > temp.t) || vis[temp.x - 1][temp.y][1] < temp.r)
{
vis[temp.x - 1][temp.y][1] = temp.r;
vis[temp.x - 1][temp.y][0] = temp.t;
temp.x--;
qiu.push(temp);
temp.x++;
}
}
if (vis[temp.x][temp.y + 1][1] <= temp.r && A[temp.x][temp.y + 1] != 'T')
{
if ((vis[temp.x][temp.y + 1][1] == temp.r && vis[temp.x][temp.y + 1][0] > temp.t) || vis[temp.x][temp.y + 1][1] < temp.r)
{
vis[temp.x][temp.y + 1][1] = temp.r;
vis[temp.x][temp.y + 1][0] = temp.t;
temp.y++;
qiu.push(temp);
temp.y--;
}
}
if (vis[temp.x][temp.y - 1][1] <= temp.r && A[temp.x][temp.y - 1] != 'T')
{
if ((vis[temp.x][temp.y - 1][1] == temp.r && vis[temp.x][temp.y - 1][0] > temp.t) || vis[temp.x][temp.y - 1][1] < temp.r)
{
vis[temp.x][temp.y - 1][1] = temp.r;
vis[temp.x][temp.y - 1][0] = temp.t;
temp.y--;
qiu.push(temp);
temp.y++;
}
}
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> S;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
{
cin >> A[i][j];
vis[i][j][1] = -1;
vis[i][j][0] = 32700;
B[i][j] = -1;
if (A[i][j] == 'M')
{
sx = i;
sy = j;
}
if (A[i][j] == 'D')
{
ex = i;
ey = j;
}
if (A[i][j] == 'H')
{
temp.x = i;
temp.y = j;
temp.t = 0;
B[i][j] = 0;
qiu.push(temp);
}
}
solve();
vis[sx][sy][1] = 32700;
vis[sx][sy][0] = 0;
solve2(sx, sy, 0, B[sx][sy] - 1);
cout << rilres;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
7 |
Incorrect |
28 ms |
8704 KB |
Output isn't correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Incorrect |
0 ms |
512 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
512 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
896 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
768 KB |
Output isn't correct |
14 |
Correct |
1 ms |
896 KB |
Output is correct |
15 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
512 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
512 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
640 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
768 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
768 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
896 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
896 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
896 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
896 KB |
Output isn't correct |
29 |
Incorrect |
0 ms |
896 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
1024 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
1024 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
1024 KB |
Output isn't correct |
33 |
Incorrect |
6 ms |
3968 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
3968 KB |
Output isn't correct |
35 |
Incorrect |
6 ms |
3968 KB |
Output isn't correct |
36 |
Incorrect |
7 ms |
4480 KB |
Output isn't correct |
37 |
Incorrect |
7 ms |
4480 KB |
Output isn't correct |
38 |
Incorrect |
7 ms |
4500 KB |
Output isn't correct |
39 |
Incorrect |
8 ms |
4992 KB |
Output isn't correct |
40 |
Incorrect |
8 ms |
4992 KB |
Output isn't correct |
41 |
Incorrect |
9 ms |
4992 KB |
Output isn't correct |
42 |
Incorrect |
11 ms |
5504 KB |
Output isn't correct |
43 |
Incorrect |
9 ms |
5504 KB |
Output isn't correct |
44 |
Incorrect |
11 ms |
5504 KB |
Output isn't correct |
45 |
Incorrect |
14 ms |
6016 KB |
Output isn't correct |
46 |
Incorrect |
11 ms |
6016 KB |
Output isn't correct |
47 |
Incorrect |
12 ms |
6016 KB |
Output isn't correct |
48 |
Incorrect |
13 ms |
6400 KB |
Output isn't correct |
49 |
Incorrect |
12 ms |
6400 KB |
Output isn't correct |
50 |
Incorrect |
14 ms |
6528 KB |
Output isn't correct |
51 |
Incorrect |
15 ms |
6912 KB |
Output isn't correct |
52 |
Incorrect |
14 ms |
6912 KB |
Output isn't correct |
53 |
Incorrect |
17 ms |
7040 KB |
Output isn't correct |
54 |
Incorrect |
18 ms |
7424 KB |
Output isn't correct |
55 |
Incorrect |
16 ms |
7424 KB |
Output isn't correct |
56 |
Incorrect |
21 ms |
7552 KB |
Output isn't correct |
57 |
Incorrect |
20 ms |
7936 KB |
Output isn't correct |
58 |
Incorrect |
18 ms |
7936 KB |
Output isn't correct |
59 |
Incorrect |
23 ms |
8064 KB |
Output isn't correct |
60 |
Incorrect |
22 ms |
8448 KB |
Output isn't correct |
61 |
Incorrect |
20 ms |
8448 KB |
Output isn't correct |
62 |
Incorrect |
24 ms |
8448 KB |
Output isn't correct |
63 |
Incorrect |
32 ms |
8448 KB |
Output isn't correct |
64 |
Incorrect |
30 ms |
8448 KB |
Output isn't correct |
65 |
Incorrect |
30 ms |
8448 KB |
Output isn't correct |
66 |
Incorrect |
30 ms |
8448 KB |
Output isn't correct |
67 |
Correct |
30 ms |
8448 KB |
Output is correct |
68 |
Incorrect |
32 ms |
8576 KB |
Output isn't correct |
69 |
Incorrect |
33 ms |
8576 KB |
Output isn't correct |
70 |
Incorrect |
34 ms |
8576 KB |
Output isn't correct |
71 |
Incorrect |
32 ms |
8576 KB |
Output isn't correct |
72 |
Incorrect |
32 ms |
8568 KB |
Output isn't correct |
73 |
Incorrect |
31 ms |
9088 KB |
Output isn't correct |
74 |
Incorrect |
29 ms |
8972 KB |
Output isn't correct |
75 |
Incorrect |
28 ms |
8960 KB |
Output isn't correct |
76 |
Incorrect |
29 ms |
8952 KB |
Output isn't correct |
77 |
Incorrect |
29 ms |
8960 KB |
Output isn't correct |
78 |
Correct |
29 ms |
8832 KB |
Output is correct |
79 |
Incorrect |
29 ms |
8832 KB |
Output isn't correct |
80 |
Incorrect |
29 ms |
8832 KB |
Output isn't correct |
81 |
Incorrect |
29 ms |
8832 KB |
Output isn't correct |
82 |
Incorrect |
30 ms |
8832 KB |
Output isn't correct |
83 |
Incorrect |
29 ms |
8960 KB |
Output isn't correct |
84 |
Incorrect |
29 ms |
8832 KB |
Output isn't correct |
85 |
Incorrect |
29 ms |
8832 KB |
Output isn't correct |
86 |
Incorrect |
30 ms |
8824 KB |
Output isn't correct |
87 |
Incorrect |
29 ms |
8832 KB |
Output isn't correct |
88 |
Incorrect |
29 ms |
8828 KB |
Output isn't correct |
89 |
Incorrect |
28 ms |
8704 KB |
Output isn't correct |
90 |
Incorrect |
29 ms |
8704 KB |
Output isn't correct |
91 |
Incorrect |
29 ms |
8704 KB |
Output isn't correct |
92 |
Incorrect |
28 ms |
8704 KB |
Output isn't correct |