-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.cpp
More file actions
executable file
·133 lines (119 loc) · 3.14 KB
/
Copy pathsol.cpp
File metadata and controls
executable file
·133 lines (119 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <vector>
using namespace std;
struct Node {
vector<Node*> adj;
int dist = 0;
char state;
bool on_fire = false;
int fire_time = 0;
};
void oneRun()
{
// Construct the grid according to input (+2 for boundaries)
int width, height;
cin >> width >> height;
Node nodes[width+2][height+2]; // Storing the nodes
vector<Node*> fire; // Storing references to the nodes where the fire is
Node* root; // For storing the starting point
// Fill the grid from console input
for (int y = 0; y < height + 2; y++) {
for (int x = 0; x < width + 2; x++) {
if (y == 0 || y == height + 1 || x == 0 || x == width + 1) {
nodes[x][y].state = 'F'; // F for FREEDOM!
} else { // Else fill in the input
cin >> nodes[x][y].state;
if (nodes[x][y].state == '@') root = &nodes[x][y];
if (nodes[x][y].state == '*') {
nodes[x][y].on_fire = true;
fire.push_back(&nodes[x][y]);
}
}
// Connect adjacent nodes
if (y != 0) nodes[x][y].adj.push_back(&nodes[x][y - 1]);
if (y != height + 1) nodes[x][y].adj.push_back(&nodes[x][y + 1]);
if (x != 0) nodes[x][y].adj.push_back(&nodes[x - 1][y]);
if (x != width + 1) nodes[x][y].adj.push_back(&nodes[x + 1][y]);
}
}
/** Spreading the fire
* Calculating the time it takes for a cell to become burning */
bool done_spreading = false;
int time = 1;
while (not done_spreading) {
// Step for the fire
vector<Node*> new_fire;
for (auto f: fire) {
for (auto neighbour: (*f).adj) {
if (!(*neighbour).on_fire && (*neighbour).state != '#' && (*neighbour).state != 'F') {
(*neighbour).on_fire = true;
(*neighbour).fire_time = time;
new_fire.push_back(neighbour);
}
}
}
if (not new_fire.size()) done_spreading = true;
fire = new_fire;
time++;
}
/** BFS */
vector<Node*> queue {root};
for (int i = 0; i < queue.size(); i++) {
Node* node = queue.at(i);
for (auto neighbour: (*node).adj) {
if ((*neighbour).dist > 0 || ((*neighbour).state != '.' && (*neighbour).state != 'F')) continue; // node already found
(*neighbour).dist = (*node).dist + 1;
if ((*neighbour).dist < (*neighbour).fire_time || !(*neighbour).on_fire) {
queue.push_back(neighbour);
}
if ((*neighbour).state == 'F') {
cout << (*neighbour).dist << endl;
return;
}
}
}
cout << "IMPOSSIBLE" << endl;
}
int main()
{
int n_cases;
cin >> n_cases;
while (n_cases--) oneRun();
return 0;
}
/** Test cases
5
4 3
####
#*@.
####
7 6
###.###
#*#.#*#
#.....#
#.....#
#..@..#
#######
7 4
###.###
#....*#
#@....#
.######
5 5
.....
.***.
.*@*.
.***.
.....
3 3
###
#@#
###
1
6 5
******
######
#@....
######
******
*/