#include<bits/stdc++.h>
using namespace std;
#define cIO ios_base::sync_with_stdio(0);cin.tie(0)
#define fileIO(x) ifstream fin(string(x)+".in"); ofstream fout(string(x)+".out")
#define ll long long
#define coor pair<int,int>
#define row first
#define col second
#define sz(x) (int) (x).size()
#define v vector
#define f first
#define s second
#define pb push_back
#define at(arr, loc) arr[loc.first][loc.second]
int N, S;//size of map and maximum number of steps Mecho can take
int mr, mc, dr, dc;//row and colums of Mecho and his home
string forest[802];//the map of the forest
int d_to_H[802][802];//minimum distance of grass tile to hive, times S
int steps_M[802][802];//minimum, how many steps it takes for Mecho to get to a tile
int steps_rest[802][802];//minimum, how many STEPS Mecho can eat honey for so that he can still get to the tile before the bees
queue<coor> bfs_H;//bfs for hive
queue<coor> bfs_M;
string filler;//"TTTTT...T"
int rd[] = { 1,0,-1,0 }, cd[] = { 0,1,0,-1 };//delta r and c for a move
void debug(string name, int x)
{
cout << name << ": " << x << "\n";
}
void debug(string name, coor x)
{
cout << name << ": " << x.row << "," << x.col << "\n";
}
int main()
{
for (int i = 0; i <= 801; i++)
{
fill_n(d_to_H[i], 802, INT_MAX - 5);
fill_n(steps_M[i], 802, INT_MAX - 5);
fill_n(steps_rest[i], 802, 0);
}
//input
cIO;
cin >> N >> S;
for (int i = 0; i <= N + 1; i++) filler += "T";
forest[0] = forest[N + 1] = filler;
for (int i = 1; i <= N; i++)
{
cin >> forest[i];
forest[i] = string("T") + forest[i] + string("T");
for (int j = 1; j <= N; j++)
{
if (forest[i][j] == 'M')
{
mr = i; mc = j;
bfs_M.push({ i,j });
steps_M[i][j] = 0;
steps_rest[i][j] = INT_MAX - 5;
}//Mecho's location
else if (forest[i][j] == 'D') { dr = i; dc = j; }
else if (forest[i][j] == 'H')
{
bfs_H.push({ i,j });
d_to_H[i][j] = 0;
}
}
}
//compute d_to_H
while (!bfs_H.empty())
{
coor cur = bfs_H.front(); bfs_H.pop();
for (int i = 0; i <= 3; i++)
{
coor move;//next time, bees will try to move here (x4 cause 4 directions)
move.row = cur.row + rd[i];
move.col = cur.col + cd[i];
if (at(forest, move)=='G' && at(d_to_H, cur) + S < at(d_to_H, move))//if bees can move there, try
{
at(d_to_H, move) = at(d_to_H, cur) + S;
bfs_H.push(move);
}
}
}
//bfs on Mecho
while (!bfs_M.empty())
{
coor cur = bfs_M.front(); bfs_M.pop();
int next_st = at(steps_M, cur) + 1;
//debug("cur", cur);
for (int i = 0; i <= 3; i++)
{
coor move;//next time, bees will try to move here (x4 cause 4 directions)
move.row = cur.row + rd[i];
move.col = cur.col + cd[i];
//debug("move", move);
if (at(forest, move) == 'G'
|| at(forest, move) == 'D')//whether moving onto it is allowed
if (min(at(d_to_H, move) - 1 - next_st, at(steps_rest, cur)) > at(steps_rest, move)
&& next_st < at(d_to_H, move)
)//whether has more rest than current route, and can reach before bees
{
at(steps_M, move) = next_st;
at(steps_rest, move) = min(at(d_to_H, move) - 1 - next_st, at(steps_rest, cur));
//debug("d_to_H[move]", at(d_to_H, move));
//debug("steps_M[move]", at(steps_M, move));
//debug("steps_rest[move]", at(steps_rest, move));
bfs_M.push(move);
}
}
}
if (at(steps_rest, coor(dr, dc)) == INT_MAX - 5)
cout << "-1\n";
else cout << at(steps_rest, coor(dr, dc))/S << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7900 KB |
Output is correct |
2 |
Correct |
4 ms |
7892 KB |
Output is correct |
3 |
Correct |
4 ms |
7892 KB |
Output is correct |
4 |
Correct |
4 ms |
7904 KB |
Output is correct |
5 |
Correct |
4 ms |
7892 KB |
Output is correct |
6 |
Correct |
5 ms |
7892 KB |
Output is correct |
7 |
Correct |
45 ms |
9920 KB |
Output is correct |
8 |
Incorrect |
4 ms |
7892 KB |
Output isn't correct |
9 |
Correct |
4 ms |
7892 KB |
Output is correct |
10 |
Correct |
4 ms |
7892 KB |
Output is correct |
11 |
Correct |
5 ms |
7892 KB |
Output is correct |
12 |
Incorrect |
5 ms |
7892 KB |
Output isn't correct |
13 |
Incorrect |
5 ms |
7892 KB |
Output isn't correct |
14 |
Incorrect |
5 ms |
7904 KB |
Output isn't correct |
15 |
Correct |
4 ms |
7892 KB |
Output is correct |
16 |
Correct |
4 ms |
7900 KB |
Output is correct |
17 |
Correct |
4 ms |
7836 KB |
Output is correct |
18 |
Correct |
4 ms |
7892 KB |
Output is correct |
19 |
Correct |
5 ms |
7892 KB |
Output is correct |
20 |
Correct |
5 ms |
7892 KB |
Output is correct |
21 |
Correct |
4 ms |
7800 KB |
Output is correct |
22 |
Correct |
4 ms |
7892 KB |
Output is correct |
23 |
Correct |
4 ms |
7892 KB |
Output is correct |
24 |
Correct |
4 ms |
7892 KB |
Output is correct |
25 |
Correct |
4 ms |
7892 KB |
Output is correct |
26 |
Correct |
5 ms |
7892 KB |
Output is correct |
27 |
Correct |
5 ms |
7892 KB |
Output is correct |
28 |
Correct |
4 ms |
7796 KB |
Output is correct |
29 |
Correct |
4 ms |
7892 KB |
Output is correct |
30 |
Correct |
4 ms |
7908 KB |
Output is correct |
31 |
Correct |
4 ms |
7908 KB |
Output is correct |
32 |
Correct |
5 ms |
7988 KB |
Output is correct |
33 |
Correct |
6 ms |
8168 KB |
Output is correct |
34 |
Correct |
6 ms |
8276 KB |
Output is correct |
35 |
Correct |
117 ms |
8312 KB |
Output is correct |
36 |
Correct |
7 ms |
8276 KB |
Output is correct |
37 |
Correct |
7 ms |
8296 KB |
Output is correct |
38 |
Correct |
133 ms |
8492 KB |
Output is correct |
39 |
Correct |
10 ms |
8404 KB |
Output is correct |
40 |
Correct |
9 ms |
8404 KB |
Output is correct |
41 |
Correct |
213 ms |
8692 KB |
Output is correct |
42 |
Correct |
8 ms |
8660 KB |
Output is correct |
43 |
Correct |
8 ms |
8660 KB |
Output is correct |
44 |
Correct |
259 ms |
8836 KB |
Output is correct |
45 |
Correct |
9 ms |
8788 KB |
Output is correct |
46 |
Correct |
9 ms |
8788 KB |
Output is correct |
47 |
Correct |
370 ms |
9104 KB |
Output is correct |
48 |
Correct |
11 ms |
9004 KB |
Output is correct |
49 |
Correct |
10 ms |
8916 KB |
Output is correct |
50 |
Correct |
473 ms |
9308 KB |
Output is correct |
51 |
Correct |
11 ms |
9172 KB |
Output is correct |
52 |
Correct |
11 ms |
9172 KB |
Output is correct |
53 |
Correct |
605 ms |
9420 KB |
Output is correct |
54 |
Correct |
12 ms |
9272 KB |
Output is correct |
55 |
Correct |
11 ms |
9372 KB |
Output is correct |
56 |
Correct |
742 ms |
9728 KB |
Output is correct |
57 |
Correct |
15 ms |
9572 KB |
Output is correct |
58 |
Correct |
13 ms |
9556 KB |
Output is correct |
59 |
Execution timed out |
1061 ms |
9872 KB |
Time limit exceeded |
60 |
Correct |
14 ms |
9704 KB |
Output is correct |
61 |
Correct |
14 ms |
9804 KB |
Output is correct |
62 |
Execution timed out |
1084 ms |
10224 KB |
Time limit exceeded |
63 |
Correct |
33 ms |
9812 KB |
Output is correct |
64 |
Correct |
39 ms |
9812 KB |
Output is correct |
65 |
Correct |
36 ms |
9808 KB |
Output is correct |
66 |
Correct |
33 ms |
9808 KB |
Output is correct |
67 |
Incorrect |
29 ms |
9812 KB |
Output isn't correct |
68 |
Correct |
24 ms |
9796 KB |
Output is correct |
69 |
Correct |
27 ms |
9828 KB |
Output is correct |
70 |
Correct |
24 ms |
9796 KB |
Output is correct |
71 |
Correct |
26 ms |
9836 KB |
Output is correct |
72 |
Incorrect |
27 ms |
9840 KB |
Output isn't correct |
73 |
Incorrect |
25 ms |
10104 KB |
Output isn't correct |
74 |
Correct |
103 ms |
10068 KB |
Output is correct |
75 |
Correct |
22 ms |
10028 KB |
Output is correct |
76 |
Correct |
23 ms |
10092 KB |
Output is correct |
77 |
Correct |
23 ms |
10088 KB |
Output is correct |
78 |
Incorrect |
22 ms |
10068 KB |
Output isn't correct |
79 |
Correct |
277 ms |
10064 KB |
Output is correct |
80 |
Correct |
47 ms |
10036 KB |
Output is correct |
81 |
Correct |
25 ms |
10068 KB |
Output is correct |
82 |
Correct |
77 ms |
10068 KB |
Output is correct |
83 |
Correct |
26 ms |
9940 KB |
Output is correct |
84 |
Correct |
107 ms |
10024 KB |
Output is correct |
85 |
Correct |
104 ms |
10016 KB |
Output is correct |
86 |
Correct |
130 ms |
10024 KB |
Output is correct |
87 |
Correct |
40 ms |
10028 KB |
Output is correct |
88 |
Correct |
74 ms |
9940 KB |
Output is correct |
89 |
Correct |
296 ms |
10032 KB |
Output is correct |
90 |
Correct |
40 ms |
9928 KB |
Output is correct |
91 |
Correct |
62 ms |
9968 KB |
Output is correct |
92 |
Correct |
31 ms |
9972 KB |
Output is correct |