forked from arshdeep/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBombMan.cpp
More file actions
297 lines (253 loc) · 10 KB
/
BombMan.cpp
File metadata and controls
297 lines (253 loc) · 10 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <sstream>
#include <set>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <time.h>
#include <queue>
using namespace std;
#define SIZEOFARRAY(s) sizeof(s)/sizeof(s[0])
#define MAXELEMENTS 50
/* https://www.topcoder.com/stat?c=problem_statement&pm=2274&rd=5009 */
struct node
{
size_t x;
size_t y;
size_t steps;
size_t bombs;
node() : x(0), y(0), steps(0), bombs(0) {}
node (size_t xx, int yy, size_t w, size_t s) : x(xx), y(yy), steps(w), bombs(s) {}
bool operator<(const node &rhs) const
{
return this->steps > rhs.steps;
}
};
class BombMan
{
bool visited[MAXELEMENTS][MAXELEMENTS];
priority_queue<node> pq;
node getStart(vector<string> board);
public:
int shortestPath(vector<string> maze, int bombs);
};
node BombMan::getStart(vector<string> maze)
{
queue< pair<int, int> > bfs;
size_t height = maze.size();
size_t width = maze[0].length();
memset(visited, 0, sizeof(visited));
node start;
bfs.push(make_pair(0, 0));
visited[0][0] = true;
bool foundA = false;
while (!bfs.empty()) {
pair<int, int> n = bfs.front();
bfs.pop();
if (n.first >= 0 && n.first < height
&& n.second >= 0 && n.second < width)
{
if (maze[n.first][n.second] == 'B')
{
start.x = n.first;
start.y = n.second;
foundA = true;
break;
}
pair<int, int> right = make_pair(n.first + 1, n.second);
pair<int, int> down = make_pair(n.first, n.second + 1);
if (!visited[right.first][right.second])
{
bfs.push(right);
visited[right.first][right.second] = true;
}
if (!visited[down.first][down.second])
{
bfs.push(down);
visited[down.first][down.second] = true;
}
}
}
assert(foundA);
return start;
}
int BombMan::shortestPath(vector<string> maze, int bombs)
{
size_t height = maze.size();
size_t width = maze[0].length();
node start = getStart(maze);
start.bombs = bombs;
pq.push(start);
memset(visited, 0, sizeof(visited));
int dX[] = {1,0,-1,0}, dY[] = {0,1,0,-1};
while (!pq.empty())
{
node top = pq.top();
pq.pop();
if (maze[top.x][top.y] == 'E')
return top.steps;
if (visited[top.x][top.y]) continue;
visited[top.x][top.y] = true;
for (size_t x = 0; x < 4; ++x)
{
int dx = top.x + dX[x];
int dy = top.y + dY[x];
if (dx >= 0 && dx < height
&& dy >= 0 && dy < width && (dx != top.x || dy != top.y))
{
if (maze[dx][dy] == '#' && top.bombs > 0)
{
pq.push(node(dx, dy, top.steps + 3, top.bombs - 1));
}
else if (maze[dx][dy] != '#')
{
pq.push(node(dx, dy, top.steps + 1, top.bombs));
}
}
}
}
return -1;
}
void TEST(vector<string> maze, int bombs, int expected)
{
clock_t start, end;
double cpu_time_used;
start = clock();
BombMan bombMan;
int result = bombMan.shortestPath(maze, bombs);
assert( result == expected );
end = clock();
cpu_time_used = ((double) (end - start));
cout<<"Time taken : "<<cpu_time_used<<endl;
}
vector< vector<int> > convert(string *list, int n)
{
vector< vector<int> > ret(n, vector<int> (n));
for (int i = 0; i< n; ++i)
{
string str = list[i];
for (int j = 0, len = str.length(); j < len; ++j) {
istringstream iss(str.substr(j,1));
iss >> ret[i][j];
}
}
return ret;
}
template <class T>
vector<T> convertEx(T *list, int n)
{
vector<T> ret;
for (int i = 0; i< n; ++i)
{
ret.push_back(list[i]);
}
return ret;
}
void Test1()
{
string maze[]= {".....B.",
".#####.",
".#...#.",
".#E#.#.",
".###.#.",
"......."};
TEST(convertEx(maze, SIZEOFARRAY(maze)), 1, 8);
}
void Test2()
{
string maze[]= {"B.#.#.#...E"};
TEST(convertEx(maze, SIZEOFARRAY(maze)), 2, -1);
}
void Test3()
{
string maze[]= {"..#####",
"B.#####",
"..#####",
"#######",
"####...",
"####.E."};
TEST(convertEx(maze, SIZEOFARRAY(maze)), 4, 17);
}
void Test4()
{
string maze[]=
{".#.#.#.#B#...#.#...#.#...#.#...#.#...#.#.#.......",
".#.#.#.#.#.###.###.#.###.#.#.###.###.#.#.#.###.##",
".#.#.#...#.#.#.#.#.#...#.....#.#.#...#...#.#.#...",
".#.#.###.#.#.#.#.#.###.#.#####.#.###.###.#.#.###.",
".............#.#...#...#.....#.#.#...#.#.#.....#.",
"##.#######.###.#.#####.#.#####.#.###.#.#.#.#.####",
".#.#.....#...#...#.#...#...#.#.#...#...#...#.....",
".#######.#.#####.#.#.#.#.###.#.###.#.#####.#.####",
".#.#.#.#...#.#.#.#.#.#.......#...#.#...#.#.#.....",
".#.#.#.###.#.#.#.#.#####.#####.###.###.#.#.######",
".....#...#.#...#...#...#...#...#...#.#.#.........",
"####.###.#.###.###.#.###.#.#.###.###.#.#.########",
".......#.........#.#.#.#.#.#.#.#.........#...#...",
".#.###.#########.#.#.#.#.###.#.#####.#.#.#.###.##",
".#.#.........#.#.#.#.#.....#.#.#.....#.#.........",
"############.#.#.#.#.#.#####.#.#.################",
".#...........#...#.#.#.#...#.#.#...#.#.#.....#...",
".#####.#####.###.#.#.#.#.###.#.#.###.#.#.#####.##",
".......#...#.#.#.....#...#...#.#.#.#.#...........",
"##########.#.#.#####.#.###.###.#.#.#.#.##########",
".....#...#.....#.#...#.......#.#...#.......#.....",
"##.#.###.#.###.#.#.#.#.#####.#.#.###.#######.####",
"...#...#...#.......#.....#.#...#...#.......#.....",
"####.#.#.#########.#.###.#.#####.###.#.#######.##",
".#...#...#.........#.#.....#.........#.#.#.#.....",
".#####.#.#.###.#######.#.###.#.#########.#.#.####",
".......#.#.#...#.......#.....#.#.#.......#.#.#.#.",
"########.#.#.#.#####.#.###.#.###.#.#######.#.#.#.",
".........#.#.#.#.....#...#.#.........#.#.........",
"################.#.#.#.#.#.#.#.#######.#.########",
".................#.#.#.#.#.#.#...........#.......",
"########.#####.#.###.#.#.#####.###.#.#####.###.##",
".........#...#.#...#.#.#...#.....#.#.........#...",
".#####.#####.#.###.#.###.#.#.#.#.#.#####.#.###.#.",
".#.....#.........#.#.#...#.#.#.#.#.#.....#...#.#.",
"####.#####.###.#.#.#.#.#.#.###.###.#.#.#.#.#####.",
".....#.....#.#.#.#.#.#.#.#.#...#...#.#.#.#...#...",
"####.#.#.###.#.#.###.#.###.#.#.#####.#.#.#.######",
".....#.#.#.#...#...#.#...#.#.#...#...#.#.#.......",
"##########.#.#.#.#####.###.#.#.###.#.###.#####.##",
"...#.#...#...#.#.....#.#...#.#...#.#.#.......#...",
".#.#.#.#.#.#.#.#.#.#.###.#.#########.###.#.#.#.#.",
".#.#...#...#.#.#.#.#...#.#...#.......#...#.#.#.#.",
"##.###.#.#.###.#.#.#.#.#####.#.#.#.###.#.########",
".......#.#...#.#.#.#.#.#.....#.#.#...#.#.........",
"####.#######.#.#####.#.###.#.#.###.#.#.#.########",
"E..#.......#.#.....#.#.#.#.#.#.#...#.#.#.........",
"##.#.#.#.###.###.###.###.#.#.###.#.#.#.#.#######.",
".....#.#...#.#.....#.#.....#...#.#.#.#.#.....#..."};
TEST(convertEx(maze, SIZEOFARRAY(maze)), 3, 76);
}
void Test5()
{
string maze[]= {".#..#......#..##.#...#..####......#.#...#.#...#..#", "..#...#..##..##...#.....##..##.#....##..##....#..#", ".#..##..##.......#...#..........##...####......###", "..###.##..#.....##..###...####.#....#..##...#.#...", "###.#.#.#..#........#.....#..#.#.#.#....#..#..#.#.", "#.#.##...####.###...#####.#.#.#..###.......#..##.#", "##.........#.##..##.#.###..##..#.#...#.##....###..", "###.###.#.##...####.##.#.#.###...#..#.###..#.#..##", "...#..#...#.#.###..#...###.#.####.#.###.#...##.#.#", "..........###...#.###.....#.##.##...#.#....#...#.#", "#..#.#.#.##.##.##.....##.###.##.#.##.###...####..#", "..#..#...#....#.##.#.##..##..#...##...#..#######..", "#.#...#.#..#....#..#####..#.##..#..##.#..#####...#", ".........#.###.##.#.##...#...#..##...#....##..#.##", "#...###...#......#.#.###..##..#.#####.####.##...##", "..###.##.#....###..........#.#.##....##..#.###...#", "#..###...##.##..###..#...#######....#.#..#...###..", "#.#####..###...#.##..#.#.#.###......#.###.#.#.#...", "...#..#.###.###..###.##..#.#..##...###..#.#..#..#.", "#.##.#......##...#.#.....##.##.#####..##.##..#.##.", "#.#.#..#.###..#...#...#..#....#...###......##..#..", "##.#.##.#..##..#..###..#..#####...#..###.#.#.#..#.", "#####...#.#...#.#.......##.##.#..#.#.##.#..##...#.", ".#.##.#.##..####..##...........#.###..#####.#.#..#", ".###.#.###.##.#.#..#.###..#..##.#.#.##.......##.#.", "##......#.#.#....###.....B.....##..#.###.#..###...", "#.###...##.####...##..###....#.#.##.#.#####....#.#", "..#####..#...##..........#.#.##...###..#.#...#...#", "..##.#...#.#.#.###...#.#..###.#..#..##.#..###...##", "#...##.....#.#....#....#.###.##.####..#..#.##.....", "#..####...#####.##...#..######.#.#.#..#......###..", "..#.#.#...#....#.......#..##.#.##...#.#.#...#..#.#", "###..##.##...##....#..#....#...#..#............#..", "#.######..###.#..#..#..#..#.#.#.#....####.#....#.#", "..####............#.#.#.####.#...##..##.###....#.#", ".#.#....#..#..###........###..##..#.#.#....##..#..", "..#.......##.##...####.#..###..#....#.#.##.......#", "####.#...#....#.##.#..#.#.##.#...###.##..#.#.#.###", "#..##..###.####..##......#....#.....#...###.###.##", ".#.####..##.#.##.##..#..#...##....##.....####.....", "####....######..#......#..#...##.##..####...###.#.", "#.##..##...##..###.#.#....#......###........#...#.", "##...#.#.#.#.#.....###...#####..#..#.##....#.##..#", ".#....###...#.#..##.##..##..##.#.#.#.##.##..##.#.#", "..########.....#..#...#.#.#.###..#..#.####.###..##", "####..#.#...#..#.####...###...####...##..#.#######", "##...#####.###...#......#.#.......#..##....#..#.##", "...##.##..#...##...#....#.##..#.....##...###..##..", "##...#.#.##..##.##........#..#...#..#..#.....##.#.", "E.##.#.###...#.#####.#.####..#..#..#..###..##.##.."};
TEST(convertEx(maze, SIZEOFARRAY(maze)), 5, 55);
}
void Test6()
{
string maze[]= {".#.....#.#.#...#.#.", ".###.###.#.#.###.#.", ".......#.#.#.#.....", "######.#.#.#.#.####", "...#.#...#.........", "##.#.###.###.######", ".......#.#...#.....", "######.#.#.###.####", ".#...........#.....", ".#######.#.###.####", ".........#.........", "##.#######.########", "...#.........#.#...", "########.###.#.#.#.", "...........#.....#.", "##########.#.###.#.", "...........#...#.#.", "####.###.#.#.######", ".....#...#.#.......", "####.#.###.###.####", ".....#...#.#.......", "##.#.#.#####.######", "...#.#.#...........", "##B#.#.#.#.###.####", "...#.#.#.#.#.......", ".#.###.#.#.#.###.##", ".#...#.#.#.#...#..E"};
TEST(convertEx(maze, SIZEOFARRAY(maze)), 0, 49);
}
void Test7()
{
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
cout<<"success";
getchar();
return 0;
}