forked from arshdeep/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinding.cpp
More file actions
294 lines (248 loc) · 8.57 KB
/
PathFinding.cpp
File metadata and controls
294 lines (248 loc) · 8.57 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
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <cassert>
#include <time.h>
#include <map>
#include <list>
#include <queue>
using namespace std;
/* http://topcoder.bgcoder.com/print.php?id=180 */
#define SIZEOFINTARRAY(a) sizeof(a)/sizeof(int)
#define SIZEOFSTRINGARRAY(s) sizeof(s)/sizeof(s[0])
#define MAXELEMENTS 20
struct node
{
int player1X;
int player1Y;
int player2X;
int player2Y;
int steps;
node() : player1X(0), player1Y(0), player2X(0), player2Y(0), steps(0) {}
node(int x1, int y1, int x2, int y2, int steps) : player1X(x1), player1Y(y1), player2X(x2), player2Y(y2), steps(steps) {}
};
class PathFinding
{
private:
bool visited[MAXELEMENTS][MAXELEMENTS][MAXELEMENTS][MAXELEMENTS];
queue<node> bfs;
void pushToQueue(node n);
struct node getStart(vector<string> board);
public:
int minTurns(vector<string> board);
};
void PathFinding::pushToQueue(node n)
{
if (visited[n.player1X][n.player1Y][n.player2X][n.player2Y]) {
return;
}
bfs.push(n);
//cout<<" "<< n.player1X<< " "<< n.player1Y<<" "<< n.player2X<< " "<< n.player2Y<<endl;
visited[n.player1X][n.player1Y][n.player2X][n.player2Y] = 1;
}
struct node PathFinding::getStart(vector<string> board)
{
queue< pair<int, int> > bfs;
size_t height = board.size();
size_t width = board[0].length();
struct node start;
bool visited2[MAXELEMENTS][MAXELEMENTS];
memset(visited2, 0, sizeof(visited2));
bfs.push(make_pair(0, 0));
visited2[0][0] = true;
bool foundA = false;
bool foundB = 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 (board[n.first][n.second] == 'A')
{
start.player1X = n.first;
start.player1Y = n.second;
foundA = true;
}
if (board[n.first][n.second] == 'B')
{
start.player2X = n.first;
start.player2Y = n.second;
foundB = true;
}
if (foundA && foundB) break;
pair<int, int> right = make_pair(n.first + 1, n.second);
pair<int, int> down = make_pair(n.first, n.second + 1);
pair<int, int> diagonal = make_pair(n.first + 1, n.second + 1);
if (!visited2[right.first][right.second])
{
bfs.push(right);
visited2[right.first][right.second] = true;
}
if (!visited2[down.first][down.second])
{
bfs.push(down);
visited2[down.first][down.second] = true;
}
if (!visited2[diagonal.first][diagonal.second])
{
bfs.push(diagonal);
visited2[diagonal.first][diagonal.second] = true;
}
}
}
assert(foundA && foundB);
return start;
}
int PathFinding::minTurns(vector<string> board)
{
memset(visited, 0, sizeof(visited));
size_t height = board.size();
size_t width = board[0].length();
node start = getStart(board);
pushToQueue(start);
while (!bfs.empty()) {
node front = bfs.front();
bfs.pop();
//cout<<" "<< front.player1X<< " "<< front.player1Y<<" "<< front.player2X<< " "<< front.player2Y<<endl;
if (front.player2X == start.player1X && front.player2Y == start.player1Y
&& front.player1X == start.player2X && front.player1Y == start.player2Y) {
return front.steps;
}
for (int deltaX1 = -1; deltaX1 <= 1; ++deltaX1) {
for (int deltaY1 = -1; deltaY1 <= 1; ++deltaY1) {
for (int deltaX2 = -1; deltaX2 <= 1; ++deltaX2) {
for (int deltaY2 = -1; deltaY2 <= 1; ++deltaY2) {
int dX1 = front.player1X + deltaX1;
int dY1 = front.player1Y + deltaY1;
int dX2 = front.player2X + deltaX2;
int dY2 = front.player2Y + deltaY2;
if ((front.player2X == dX1 && front.player2Y == dY1
&& front.player1X == dX2 && front.player1Y == dY2) || ( dX1 == dX2 && dY1 == dY2)) {
continue;
}
if (dX1 >= 0 && dX1 < height && dY1 >= 0 && dY1 < width
&& dX2 >= 0 && dX2 < height && dY2 >= 0 && dY2 < width)
{
if (board[dX1][dY1] != 'X' && board[dX2][dY2] != 'X')
pushToQueue(node(dX1, dY1, dX2, dY2, front.steps + 1));
}
}
}
}
}
}
return -1;
}
vector<string> convert(string *list, int n)
{
vector<string> ret;
for (int i = 0; i< n; ++i)
{
ret.push_back(list[i]);
}
return ret;
}
void TEST(vector<string> board, int expected)
{
clock_t start, end;
double cpu_time_used;
start = clock();
PathFinding pathFinding;
long result = pathFinding.minTurns(board);
assert( result == expected );
end = clock();
cpu_time_used = ((double) (end - start));
cout<<"Time taken : "<<cpu_time_used<<endl;
}
void Test1()
{
string test[] = {"....",
".A..",
"..B.",
"...."};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), 2);
}
void Test2()
{
string test[] = {"XXXXXXXXX",
"A...X...B",
"XXXXXXXXX"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), -1);
}
void Test3()
{
string test[] = {"XXXXXXXXX",
"A.......B",
"XXXXXXXXX"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), -1);
}
void Test4()
{
string test[] = {"XXXXXXXXX",
"A.......B",
"XXXX.XXXX"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), 8);
}
void Test5()
{
string test[] = {"...A.XXXXX.....",
".....XXXXX.....",
"...............",
".....XXXXX.B...",
".....XXXXX....."};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), 13);
}
void Test6()
{
string test[] = {"AB.................X",
"XXXXXXXXXXXXXXXXXXX.",
"X..................X",
".XXXXXXXXXXXXXXXXXXX",
"X..................X",
"XXXXXXXXXXXXXXXXXXX.",
"X..................X",
".XXXXXXXXXXXXXXXXXXX",
"X..................X",
"XXXXXXXXXXXXXXXXXXX.",
"X..................X",
".XXXXXXXXXXXXXXXXXXX",
"X..................X",
"XXXXXXXXXXXXXXXXXXX.",
"X..................X",
".XXXXXXXXXXXXXXXXXXX",
"X..................X",
"XXXXXXXXXXXXXXXXXXX.",
"...................X",
".XXXXXXXXXXXXXXXXXXX"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), 379);
}
void Test7()
{
string test[] = {"..X.....XX........X", ".XX.....X.XXXXXXXX.", ".X......X.X.....X.X", ".X......X.X....X.X.", ".X......X.X...X.X..", "X.X.....X.X..X.X...", ".X.X....X.X.X.X....", "..X.X...X.XX.X.....", "...X.X..X.X.X......", "....X.X.XAXX.X.....", ".....X.XXBX.X.X....", "......X.X.X..X.X...", ".....X.XX.X...X.X..", "....X.X.X.X....X.X.", "...X.X..X.X.....X.X", "..X.X...X.X......X.", ".X.X....X.X......X.", "X.X.....X.X......X.", ".XXXXXXXX.X.....XX.", "X........XX.....X.."};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), 39);
}
void Test8()
{
string test[] = {"X.XXX.X....XX.X.", "..XXXX..X..XX.XX", "X...X..XX.......", ".X.....XX.XXX.XX", "AXXX.XXXX.X..X.X", "..X....X.X....X.", "............XX.X", ".XX.XXXX...XXX..", ".X....XX..XX..X.", ".XX.X..XXX.X..XX", "....XX.XX.X.XXX.", ".XX..XXX..XXXX..", "XX...X.X..X..XX.", ".X.XX.XX..X...X.", "...XXX.XX....XXX", "X.X...XX.X...X..", "X.X.X.XX....XX.B", "...XXX...X...XX.", "..XX....XXX.X.XX", "......X..X......"};
TEST(convert(test, SIZEOFSTRINGARRAY(test)), -1);
}
void Test9()
{
string test[] = {"..X.X.XX..X.X.", "AXXX.XXXX.XXXX", "X..XXXXX.X.XX.", ".X.XX.X.X..XXX", "..XXXXXX..X.XB", "X.XXXXX.XXXX.X", "XX.X...XX.XXXX", ".X..XXXXXX.XX.", "XXXXXXX.X.XXXX", "..XXXX..X.X.XX", ".XX.X.X..XX.XX", "X...XX........", "XXXXX.X.X.X..X", "..XX..XX..XXX.", "X.X.X.XX.....X", "X....X.X..XX..", "..X.X.XXX.XX.X", "..XXXX.XX.XX.X", "XXXXX..X......", "X.X.X.XX.X.X.."} ;
TEST(convert(test, SIZEOFSTRINGARRAY(test)), 21);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
return 0;
}