#include <iostream>
#include <queue>
#include <stack>
#include <vector>
// Global variables
int N{ 0 };
int S{ 0 };
bool isInBounds(int i, int j) {
return (i >= 0 && i < N && j >= 0 && j < N);
}
bool isBearWalkable(int i, int j, const std::vector<std::string>& forest) {
return (forest[i][j] == 'G' || forest[i][j] == 'D');
}
bool isBeeWalkable(int i, int j, const std::vector<std::string>& forest) {
return (forest[i][j] == 'G' || forest[i][j] == 'M');
}
bool isNotVisited(int i, int j, const std::vector<std::vector<bool>>& visited) {
return (!visited[i][j]);
}
bool isBearCandidate(int i, int j, const std::vector<std::string>& forest, const std::vector<std::vector<bool>>& visited) {
return ( isInBounds(i, j)
&& isBearWalkable(i, j, forest)
&& isNotVisited(i, j, visited) );
}
bool isBeeCandidate(int i, int j, const std::vector<std::string>& forest) {
return ( isInBounds(i, j)
&& isBeeWalkable(i, j, forest));
}
void addBeeCandidate(int i, int j, int current_i, int current_j, std::vector<std::string>& forest, std::stack<std::pair<int,int>>& new_bees_positions) {
if (isBeeCandidate(i, j, forest)) {
forest[i][j] = 'H';
new_bees_positions.push(std::make_pair(i, j));
}
}
std::stack<std::pair<int, int>> spreadBees(std::stack<std::pair<int, int>>& current_bees_positions, std::vector<std::string>& forest) {
std::stack<std::pair<int, int>> new_bees_positions;
while (!current_bees_positions.empty()) {
int current_i{ current_bees_positions.top().first };
int current_j{ current_bees_positions.top().second };
current_bees_positions.pop();
addBeeCandidate(current_i - 1, current_j , current_i, current_j, forest, new_bees_positions);
addBeeCandidate(current_i + 1, current_j , current_i, current_j, forest, new_bees_positions);
addBeeCandidate(current_i , current_j - 1, current_i, current_j, forest, new_bees_positions);
addBeeCandidate(current_i , current_j + 1, current_i, current_j, forest, new_bees_positions);
}
return new_bees_positions;
}
void addBearCandidate( int i, int j, int current_i, int current_j
, const std::vector<std::string>& forest
, std::vector<std::vector<bool>>& visited
, std::vector<std::vector<int>>& dist
, std::vector<std::vector<std::pair<int, int>>>& trajectory
, std::queue<std::pair<int,int>>& q) {
if (isBearCandidate(i, j, forest, visited)) {
q.push(std::make_pair(i, j));
visited[i][j] = true;
dist[i][j] = dist[current_i][current_j] + 1;
trajectory[i][j] = std::make_pair(current_i, current_j);
}
}
int findShortestPath(const std::vector<std::string>& forest, const std::pair<int, int>& start, const std::pair<int, int>& end) {
std::vector<std::vector<bool>> visited(N, std::vector<bool>(N, false));
std::vector<std::vector<int>> dist(N, std::vector<int>(N, 0));
std::vector<std::vector<std::pair<int, int>>> trajectory(N, std::vector<std::pair<int, int>>(N));
std::queue<std::pair<int, int>> q;
q.push(start);
int current_i{ start.first };
int current_j{ start.second };
visited[current_i][current_j] = true;
while (!q.empty() && forest[current_i][current_j] != 'D') {
current_i = q.front().first;
current_j = q.front().second;
q.pop();
if (forest[current_i][current_j] != 'D') {
addBearCandidate(current_i - 1, current_j , current_i, current_j, forest, visited, dist, trajectory, q);
addBearCandidate(current_i + 1, current_j , current_i, current_j, forest, visited, dist, trajectory, q);
addBearCandidate(current_i , current_j - 1, current_i, current_j, forest, visited, dist, trajectory, q);
addBearCandidate(current_i , current_j + 1, current_i, current_j, forest, visited, dist, trajectory, q);
}
}
/*
std::cout << dist[3][6] << std::endl;
int i{ 3 };
int j{ 6 };
while (! (i == start.first && j == start.second)) {
std::cout << trajectory[i][j].first << " " << trajectory[i][j].second << std::endl;
int temp_i{ trajectory[i][j].first };
int temp_j{ trajectory[i][j].second };
i = temp_i;
j = temp_j;
}
*/
return dist[end.first][end.second];
}
int timeToHome(int pathLength) {
int time { pathLength / S };
if (pathLength % S != 0)
++time;
return time;
}
void parser(std::vector<std::string>& forest) {
std::cin >> N >> S;
for (int i = 0; i < N; ++i) {
std::string row;
std::cin >> row;
forest.push_back(row);
}
}
void getPositions(std::vector<std::string>& forest, std::stack < std::pair<int, int>>& current_bees_positions, std::pair<int, int>& start, std::pair<int, int>& end) {
for (int i=0; i<N; ++i)
for (int j=0; j<N; ++j)
if (forest[i][j] == 'M') {
start.first = i;
start.second = j;
}
else if (forest[i][j] == 'D') {
end.first = i;
end.second = j;
}
else if (forest[i][j] == 'H')
current_bees_positions.push(std::make_pair(i, j));
}
void printForest(const std::vector<std::string>& forest) {
for (size_t i = 0; i < N; ++i)
std::cout << forest[i] << std::endl;
}
int main() {
std::vector<std::string> forest;
parser(forest);
std::stack < std::pair<int, int>> current_bees_positions;
std::pair<int, int> start{ 0,0 };
std::pair<int, int> end{ 0,0 };
getPositions(forest, current_bees_positions, start, end);
int dist{ 0 };
int temp_dist{ 0 };
int bees_time{ 0 };
do {
temp_dist = findShortestPath(forest, start, end);
if (temp_dist != 0) {
current_bees_positions = spreadBees(current_bees_positions, forest);
//printForest(forest);
++bees_time;
dist = temp_dist;
}
} while (temp_dist !=0 );
--bees_time;
--bees_time;
// int time{ timeToHome(dist) - bees_time + 1};
//std::cout << timeToHome(dist) << std::endl;
std::cout << bees_time;// << std::endl;
//std::cout << time << std::endl;
/*
findShortestPath(forest, start);
std::stack < std::pair<int, int>> next_bees_positions;
spreadBees(current_bees_positions, next_bees_positions, forest);
std::stack < std::pair<int, int>> next_next_bees_positions;
spreadBees(next_bees_positions, next_next_bees_positions, forest);
*/
}
Compilation message
mecho.cpp: In function 'void printForest(const std::vector<std::__cxx11::basic_string<char> >&)':
mecho.cpp:153:24: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
153 | for (size_t i = 0; i < N; ++i)
| ~~^~~
mecho.cpp: In function 'int main()':
mecho.cpp:166:7: warning: variable 'dist' set but not used [-Wunused-but-set-variable]
166 | int dist{ 0 };
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Execution timed out |
1069 ms |
9616 KB |
Time limit exceeded |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
12 |
Execution timed out |
1089 ms |
340 KB |
Time limit exceeded |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
15 |
Correct |
2 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
300 KB |
Output is correct |
19 |
Correct |
4 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
296 KB |
Output is correct |
21 |
Correct |
9 ms |
328 KB |
Output is correct |
22 |
Correct |
6 ms |
212 KB |
Output is correct |
23 |
Correct |
19 ms |
332 KB |
Output is correct |
24 |
Correct |
15 ms |
300 KB |
Output is correct |
25 |
Correct |
37 ms |
300 KB |
Output is correct |
26 |
Correct |
24 ms |
340 KB |
Output is correct |
27 |
Correct |
25 ms |
340 KB |
Output is correct |
28 |
Correct |
21 ms |
340 KB |
Output is correct |
29 |
Correct |
32 ms |
340 KB |
Output is correct |
30 |
Correct |
28 ms |
308 KB |
Output is correct |
31 |
Correct |
44 ms |
340 KB |
Output is correct |
32 |
Correct |
35 ms |
340 KB |
Output is correct |
33 |
Execution timed out |
1079 ms |
2324 KB |
Time limit exceeded |
34 |
Execution timed out |
1088 ms |
2440 KB |
Time limit exceeded |
35 |
Incorrect |
30 ms |
2060 KB |
Output isn't correct |
36 |
Execution timed out |
1081 ms |
2864 KB |
Time limit exceeded |
37 |
Execution timed out |
1074 ms |
2852 KB |
Time limit exceeded |
38 |
Incorrect |
42 ms |
2600 KB |
Output isn't correct |
39 |
Execution timed out |
1077 ms |
3680 KB |
Time limit exceeded |
40 |
Execution timed out |
1084 ms |
3464 KB |
Time limit exceeded |
41 |
Incorrect |
52 ms |
3164 KB |
Output isn't correct |
42 |
Execution timed out |
1086 ms |
4144 KB |
Time limit exceeded |
43 |
Execution timed out |
1071 ms |
4300 KB |
Time limit exceeded |
44 |
Incorrect |
64 ms |
3980 KB |
Output isn't correct |
45 |
Execution timed out |
1077 ms |
5008 KB |
Time limit exceeded |
46 |
Execution timed out |
1074 ms |
4844 KB |
Time limit exceeded |
47 |
Incorrect |
83 ms |
4580 KB |
Output isn't correct |
48 |
Execution timed out |
1075 ms |
5696 KB |
Time limit exceeded |
49 |
Execution timed out |
1083 ms |
5804 KB |
Time limit exceeded |
50 |
Incorrect |
97 ms |
5484 KB |
Output isn't correct |
51 |
Execution timed out |
1068 ms |
6572 KB |
Time limit exceeded |
52 |
Execution timed out |
1081 ms |
6580 KB |
Time limit exceeded |
53 |
Incorrect |
111 ms |
6256 KB |
Output isn't correct |
54 |
Execution timed out |
1085 ms |
7528 KB |
Time limit exceeded |
55 |
Execution timed out |
1078 ms |
7468 KB |
Time limit exceeded |
56 |
Incorrect |
134 ms |
7336 KB |
Output isn't correct |
57 |
Execution timed out |
1082 ms |
8672 KB |
Time limit exceeded |
58 |
Execution timed out |
1087 ms |
8544 KB |
Time limit exceeded |
59 |
Incorrect |
138 ms |
8216 KB |
Output isn't correct |
60 |
Execution timed out |
1079 ms |
9576 KB |
Time limit exceeded |
61 |
Execution timed out |
1093 ms |
9476 KB |
Time limit exceeded |
62 |
Incorrect |
171 ms |
9296 KB |
Output isn't correct |
63 |
Execution timed out |
1077 ms |
9532 KB |
Time limit exceeded |
64 |
Execution timed out |
1077 ms |
9604 KB |
Time limit exceeded |
65 |
Execution timed out |
1077 ms |
9368 KB |
Time limit exceeded |
66 |
Execution timed out |
1082 ms |
9516 KB |
Time limit exceeded |
67 |
Execution timed out |
1090 ms |
9496 KB |
Time limit exceeded |
68 |
Incorrect |
338 ms |
9408 KB |
Output isn't correct |
69 |
Incorrect |
329 ms |
9320 KB |
Output isn't correct |
70 |
Incorrect |
337 ms |
9324 KB |
Output isn't correct |
71 |
Incorrect |
320 ms |
9372 KB |
Output isn't correct |
72 |
Execution timed out |
1091 ms |
9504 KB |
Time limit exceeded |
73 |
Correct |
92 ms |
9440 KB |
Output is correct |
74 |
Execution timed out |
1090 ms |
9596 KB |
Time limit exceeded |
75 |
Execution timed out |
1077 ms |
9652 KB |
Time limit exceeded |
76 |
Execution timed out |
1088 ms |
9836 KB |
Time limit exceeded |
77 |
Execution timed out |
1084 ms |
9548 KB |
Time limit exceeded |
78 |
Execution timed out |
1078 ms |
9612 KB |
Time limit exceeded |
79 |
Execution timed out |
1086 ms |
9556 KB |
Time limit exceeded |
80 |
Execution timed out |
1090 ms |
9560 KB |
Time limit exceeded |
81 |
Execution timed out |
1083 ms |
9620 KB |
Time limit exceeded |
82 |
Execution timed out |
1083 ms |
9528 KB |
Time limit exceeded |
83 |
Execution timed out |
1084 ms |
9508 KB |
Time limit exceeded |
84 |
Execution timed out |
1060 ms |
9496 KB |
Time limit exceeded |
85 |
Execution timed out |
1047 ms |
9604 KB |
Time limit exceeded |
86 |
Execution timed out |
1083 ms |
9616 KB |
Time limit exceeded |
87 |
Execution timed out |
1086 ms |
9496 KB |
Time limit exceeded |
88 |
Execution timed out |
1050 ms |
9464 KB |
Time limit exceeded |
89 |
Execution timed out |
1020 ms |
9508 KB |
Time limit exceeded |
90 |
Incorrect |
972 ms |
9452 KB |
Output isn't correct |
91 |
Execution timed out |
1028 ms |
9464 KB |
Time limit exceeded |
92 |
Execution timed out |
1063 ms |
9596 KB |
Time limit exceeded |