#include <iostream>
#include <queue>
#define MAX 805
using namespace std;
struct bee
{
short int x;
short int y;
short int t;
short int r;
};
short int N, S, sx, sy, ex, ey, B[MAX][MAX], vis[MAX][MAX][2], rilres = -1;
char A[MAX][MAX];
queue <bee> qiu, qiu2;
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;
qiu2.push(temp);
while (!qiu2.empty())
{
temp = qiu2.front();
qiu2.pop();
//cout << temp.x << " " << temp.y << " " << temp.t << " " << temp.r << endl;
if (temp.x == ex && temp.y == ey)
rilres = max(rilres,temp.r);
if (temp.r + (temp.t / S) >= B[temp.x][temp.y])
temp.r -= ((temp.r + (temp.t / S)) - B[temp.x][temp.y] + 1);
//cout << temp.x << " " << temp.y << " " << temp.t << " " << temp.r << endl;
if (temp.r < 0 || temp.r <= rilres)
continue;
temp.t++;
//cout << vis[temp.x][temp.y][0] << " " << vis[temp.x][temp.y][1] << endl;
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)
{
//cout << "SI" << endl;
vis[temp.x + 1][temp.y][1] = temp.r;
vis[temp.x + 1][temp.y][0] = temp.t;
temp.x++;
qiu2.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--;
qiu2.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++;
qiu2.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--;
qiu2.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] = 1073741822;
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();
/*cout << endl;
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
cout << B[i][j] << " ";
}
cout << endl;
}*/
vis[sx][sy][1] = 1073741822;
vis[sx][sy][0] = 0;
solve2(sx, sy, 0, B[sx][sy] - 1);
cout << rilres;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:136:28: warning: overflow in conversion from 'int' to 'short int' changes value from '1073741822' to '-2' [-Woverflow]
136 | vis[i][j][0] = 1073741822;
| ^~~~~~~~~~
mecho.cpp:167:22: warning: overflow in conversion from 'int' to 'short int' changes value from '1073741822' to '-2' [-Woverflow]
167 | vis[sx][sy][1] = 1073741822;
| ^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
140 ms |
4984 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
0 ms |
512 KB |
Output is correct |
16 |
Correct |
0 ms |
512 KB |
Output is correct |
17 |
Correct |
0 ms |
512 KB |
Output is correct |
18 |
Correct |
0 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
0 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
768 KB |
Output is correct |
28 |
Correct |
1 ms |
768 KB |
Output is correct |
29 |
Correct |
1 ms |
768 KB |
Output is correct |
30 |
Correct |
1 ms |
768 KB |
Output is correct |
31 |
Correct |
1 ms |
768 KB |
Output is correct |
32 |
Correct |
1 ms |
768 KB |
Output is correct |
33 |
Incorrect |
5 ms |
2304 KB |
Output isn't correct |
34 |
Correct |
4 ms |
2304 KB |
Output is correct |
35 |
Correct |
120 ms |
2424 KB |
Output is correct |
36 |
Incorrect |
6 ms |
2560 KB |
Output isn't correct |
37 |
Incorrect |
5 ms |
2560 KB |
Output isn't correct |
38 |
Correct |
181 ms |
2680 KB |
Output is correct |
39 |
Incorrect |
7 ms |
2816 KB |
Output isn't correct |
40 |
Incorrect |
6 ms |
2816 KB |
Output isn't correct |
41 |
Correct |
255 ms |
3068 KB |
Output is correct |
42 |
Incorrect |
9 ms |
3072 KB |
Output isn't correct |
43 |
Incorrect |
8 ms |
3072 KB |
Output isn't correct |
44 |
Correct |
355 ms |
3320 KB |
Output is correct |
45 |
Incorrect |
11 ms |
3328 KB |
Output isn't correct |
46 |
Incorrect |
9 ms |
3328 KB |
Output isn't correct |
47 |
Correct |
473 ms |
3576 KB |
Output is correct |
48 |
Incorrect |
12 ms |
3712 KB |
Output isn't correct |
49 |
Incorrect |
11 ms |
3712 KB |
Output isn't correct |
50 |
Correct |
613 ms |
4088 KB |
Output is correct |
51 |
Incorrect |
14 ms |
3968 KB |
Output isn't correct |
52 |
Incorrect |
12 ms |
3968 KB |
Output isn't correct |
53 |
Correct |
786 ms |
4600 KB |
Output is correct |
54 |
Incorrect |
16 ms |
4224 KB |
Output isn't correct |
55 |
Incorrect |
14 ms |
4224 KB |
Output isn't correct |
56 |
Correct |
983 ms |
4836 KB |
Output is correct |
57 |
Incorrect |
18 ms |
4480 KB |
Output isn't correct |
58 |
Incorrect |
16 ms |
4460 KB |
Output isn't correct |
59 |
Execution timed out |
1085 ms |
5112 KB |
Time limit exceeded |
60 |
Incorrect |
21 ms |
4736 KB |
Output isn't correct |
61 |
Incorrect |
18 ms |
4736 KB |
Output isn't correct |
62 |
Execution timed out |
1094 ms |
5368 KB |
Time limit exceeded |
63 |
Correct |
42 ms |
4736 KB |
Output is correct |
64 |
Correct |
44 ms |
4736 KB |
Output is correct |
65 |
Correct |
43 ms |
4736 KB |
Output is correct |
66 |
Correct |
42 ms |
4736 KB |
Output is correct |
67 |
Correct |
37 ms |
4768 KB |
Output is correct |
68 |
Correct |
36 ms |
4736 KB |
Output is correct |
69 |
Correct |
35 ms |
4736 KB |
Output is correct |
70 |
Correct |
33 ms |
4736 KB |
Output is correct |
71 |
Correct |
32 ms |
4736 KB |
Output is correct |
72 |
Correct |
30 ms |
4736 KB |
Output is correct |
73 |
Correct |
30 ms |
4992 KB |
Output is correct |
74 |
Correct |
56 ms |
4984 KB |
Output is correct |
75 |
Correct |
31 ms |
4992 KB |
Output is correct |
76 |
Correct |
32 ms |
4992 KB |
Output is correct |
77 |
Correct |
31 ms |
4992 KB |
Output is correct |
78 |
Correct |
59 ms |
5120 KB |
Output is correct |
79 |
Correct |
242 ms |
4992 KB |
Output is correct |
80 |
Correct |
298 ms |
5240 KB |
Output is correct |
81 |
Correct |
200 ms |
4992 KB |
Output is correct |
82 |
Correct |
99 ms |
5180 KB |
Output is correct |
83 |
Correct |
70 ms |
4992 KB |
Output is correct |
84 |
Correct |
222 ms |
5112 KB |
Output is correct |
85 |
Correct |
160 ms |
4992 KB |
Output is correct |
86 |
Correct |
154 ms |
4992 KB |
Output is correct |
87 |
Correct |
115 ms |
5088 KB |
Output is correct |
88 |
Correct |
198 ms |
4984 KB |
Output is correct |
89 |
Correct |
342 ms |
4984 KB |
Output is correct |
90 |
Correct |
119 ms |
4984 KB |
Output is correct |
91 |
Correct |
319 ms |
5240 KB |
Output is correct |
92 |
Correct |
233 ms |
5240 KB |
Output is correct |