|
| 1 | +# LOJ 1175 - Jane and the Frost Giants |
| 2 | + |
| 3 | +## Prerequisite |
| 4 | +[BFS (Bredth First Search)](https://cp-algorithms.com/graph/breadth-first-search.html) |
| 5 | + |
| 6 | +## Solution |
| 7 | +If we forget about the movable fire cells that grows every minute, then the problem reduces to calculating how many minutes it takes to reach a cell without going through an obstacle, which can be done easily with BFS alone. |
| 8 | + |
| 9 | +Similarly, with BFS, we can also compute the shortest time it takes |
| 10 | +fire to spread to a cell. Jane can only move through a cell if she |
| 11 | +reaches there before fire does. |
| 12 | + |
| 13 | +We can utilize this observation while traversing through the grid |
| 14 | +to find a route for Jane. If fire gets to an empty cell before |
| 15 | +she does, we can consider the cell an obstacle and move on. |
| 16 | +Otherwise, consider it empty and try to move through there. |
| 17 | + |
| 18 | +## Complexity |
| 19 | +- Time Complexity: O(T * R * C). |
| 20 | +- Memory Complexity: O(R * C). |
| 21 | + |
| 22 | +## Code |
| 23 | + |
| 24 | +### C++ |
| 25 | + |
| 26 | +```cpp |
| 27 | +#include <bits/stdc++.h> |
| 28 | + |
| 29 | +using namespace std; |
| 30 | + |
| 31 | +const int INF = numeric_limits <int>:: max(); // Indicating Unreachable state |
| 32 | + |
| 33 | +int r, c; |
| 34 | +vector <string> grid; |
| 35 | + |
| 36 | +// An Efficient (and quite common) Way to Navigate Grid Problems: https://codeforces.com/blog/entry/78827 |
| 37 | +const int dr[] = {-1, 0, 1, 0}; |
| 38 | +const int dc[] = {0, 1, 0, -1}; |
| 39 | + |
| 40 | +inline bool valid(int x, int y) { |
| 41 | + // 0-based index grid |
| 42 | + return x >= 0 && x < r && y >= 0 && y < c && grid[x][y] != '#'; |
| 43 | +} |
| 44 | + |
| 45 | +struct Cell { |
| 46 | + bool flag; // Indicating whther this cell is on fire or not |
| 47 | + int x, y; // Row and column respectively |
| 48 | + |
| 49 | + Cell() {} // Default constrcutor |
| 50 | + Cell(bool flag, int x, int y) : flag(flag), x(x), y(y) {} |
| 51 | +}; |
| 52 | + |
| 53 | +int main() { |
| 54 | + |
| 55 | + // For fast I/O |
| 56 | + ios_base::sync_with_stdio(false); |
| 57 | + cin.tie(nullptr); |
| 58 | + |
| 59 | + int t; |
| 60 | + cin >> t; |
| 61 | + |
| 62 | + for(int ts = 1; ts <= t; ++ts) { |
| 63 | + cin >> r >> c; |
| 64 | + |
| 65 | + grid.resize(r); |
| 66 | + |
| 67 | + pair <int, int> source; |
| 68 | + queue <Cell> Q; |
| 69 | + // dist[i][j] = Minimum minutes needed to move to cell[i][j] from starting cell |
| 70 | + vector <vector <int>> dist(r, vector <int> (c, INF)); |
| 71 | + for(int i = 0; i < r; ++i) { |
| 72 | + cin >> grid[i]; |
| 73 | + for(int j = 0; j < c; ++j) { |
| 74 | + if (grid[i][j] == 'J') { |
| 75 | + source = {i, j}; |
| 76 | + } |
| 77 | + else if (grid[i][j] == 'F') { |
| 78 | + // Turning into a movable obstacle |
| 79 | + grid[i][j] = '#'; |
| 80 | + Q.push(Cell(true, i, j)); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // All the fire cells have been added to the queue so that they always move to the adjacent cells first not, Jane |
| 86 | + dist[source.first][source.second] = 0; |
| 87 | + Q.push(Cell(false, source.first, source.second)); |
| 88 | + |
| 89 | + while (!Q.empty()) { |
| 90 | + Cell u = Q.front(); |
| 91 | + Q.pop(); |
| 92 | + |
| 93 | + for(int i = 0; i < 4; ++i) { |
| 94 | + int x = u.x + dr[i]; |
| 95 | + int y = u.y + dc[i]; |
| 96 | + |
| 97 | + // Checking for not an obstacle yet |
| 98 | + if (valid(x, y) && dist[x][y] == INF) { |
| 99 | + if (u.flag) { |
| 100 | + // Now an obstacle that can move |
| 101 | + grid[x][y] = '#'; |
| 102 | + Q.push(Cell(true, x, y)); |
| 103 | + } |
| 104 | + else { |
| 105 | + // That's jane moving |
| 106 | + dist[x][y] = dist[u.x][u.y] + 1; |
| 107 | + Q.push(Cell(false, x, y)); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + int best = INF; |
| 114 | + // Topmost and downmost rows |
| 115 | + for(int j = 0; j < c; ++j) { |
| 116 | + best = min({best, dist[0][j], dist[r-1][j]}); |
| 117 | + } |
| 118 | + // Leftmost and rightmost columns |
| 119 | + for(int i = 0; i < r; ++i) { |
| 120 | + best = min({best, dist[i][0], dist[i][c-1]}); |
| 121 | + } |
| 122 | + |
| 123 | + cout << "Case " << ts << ": "; |
| 124 | + |
| 125 | + if (best == INF) { |
| 126 | + cout << "IMPOSSIBLE\n"; |
| 127 | + } |
| 128 | + else { |
| 129 | + // Adding 1 because of getting out of the maze completely |
| 130 | + cout << best+1 << '\n'; |
| 131 | + } |
| 132 | + |
| 133 | + grid.clear(); |
| 134 | + } |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
| 138 | +``` |
0 commit comments