-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrush_hour.cpp
490 lines (424 loc) · 13.2 KB
/
rush_hour.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#define BOARD_SIZE 6 //// 6x6 board size
#include <iostream> // cout, invalid_argument, getline
#include <fstream> // file open
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <algorithm> // find
#include <time.h> // clock_t, CLOCKS_PER_SEC
#include <stdlib.h> // exit
using namespace std;
class Car{
public:
int row, column, length;
string orient;
Car(int, int, int, string);
void printCar();
friend bool operator== (const Car &n1, const Car &n2);
};
Car::Car(int r, int c, int l, string orientation){
/*
Create a car.
Arguments:
row: Row # of the bottom left corner of the car (1-6)
column: Column # of the bottom left corner of the car (0-5)
length: Length of the car (2-5)
orient: Orientation of the car (vertical : v or horizontal : h)
*/
if ((r >= 1) && (r <= 6)) row = r;
else throw invalid_argument("Invalid row. Must be integer in [1, 6].");
if ((c >= 0) && (c <= 5)) column = c;
else throw invalid_argument("Invalid column. Must be integer in [0, 5].");
if ((l >= 2) && (l <= 5)) length = l;
else throw invalid_argument("Invalid length. Must be integer in [2, 5].");
if (orientation == "v"){
orient = orientation;
if (row - length < 0) throw invalid_argument("Invalid configuration. Car is out of bounds vertically.");
}
else if (orientation == "h"){
orient = orientation;
if (column + length > 6) throw invalid_argument("Invalid configuration. Car is out of bounds horizontally.");
}
else throw invalid_argument("Invalid configuration. Must be 'v' or 'h'.");
}
void Car::printCar(){
cout << "Car(" << row << ", " << column << ", " << length << ", " << orient << ")" << endl;
}
//// Check the equality of cars
bool operator== (const Car &c1, const Car &c2){
return ((c1.row == c2.row) && (c1.column == c2.column) && (c1.length == c2.length) && (c1.orient == c2.orient));
}
class Board{
public:
vector <Car> vehicles;
int carCount;
Board(vector <Car>);
vector <vector <string> > getBoard();
void printBoard();
bool isSolved();
vector <Board> getMoves();
friend bool operator== (const Board &n1, const Board &n2);
};
Board::Board(vector <Car> cs){
/*
Create a board.
Arguments:
cs: A vector of cars
*/
vehicles = cs;
carCount = vehicles.size();
}
//// Get board with cars (with their coordinates) in it
vector <vector <string> > Board::getBoard(){
//// Initialize board
vector <vector <string> > board(BOARD_SIZE);
for (int i=0; i<BOARD_SIZE; i++) {
for (int j=0; j<BOARD_SIZE; j++) {
board[i].push_back(" _ ");
}
}
//// Place the target car
for (int i=0; i<vehicles[0].length; i++) board[vehicles[0].row - 1][vehicles[0].column + i] = " X ";
//// Place the obstructive cars
string index;
string strName;
for (int i=1; i<carCount; i++) {
index = to_string(i);
strName = " O" + index;
if (vehicles[i].orient == "h"){
for (int j=0; j<vehicles[i].length; j++) board[vehicles[i].row - 1][vehicles[i].column + j] = strName;
}
else {
for (int k=0; k<vehicles[i].length; k++) board[vehicles[i].row - vehicles[i].length + k][vehicles[i].column] = strName;
}
}
return board;
}
//// Print the board on the console
void Board::printBoard(){
vector <vector <string> > board = getBoard();
for (int i=0; i<BOARD_SIZE; i++) {
for (int j=0; j<BOARD_SIZE; j++) {
cout << board[i][j];
}
cout << endl;
}
}
//// Check if the target car is out
bool Board::isSolved(){
return (vehicles[0].column + vehicles[0].length == BOARD_SIZE);
}
//// Get all possible moves available from one board state
//// One move means moving a car just one space (left or right for a car with 'h', up or down for a car with 'v')
vector <Board> Board::getMoves(){
vector <Board> moves; // Collection of all possible board states can be moved
vector <vector <string> > board = getBoard();
vector <Car> vehiclesCopy;
for (int i=0; i<carCount; i++) {
int r = vehicles[i].row;
int c = vehicles[i].column;
int l = vehicles[i].length;
string o = vehicles[i].orient;
if (o == "h"){
//// Move to the left
if ((c >= 1) && (board[r - 1][c - 1] == " _ ")){
Car newCar(r, c - 1, l, o);
vehiclesCopy = vehicles;
vehiclesCopy[i] = newCar;
moves.push_back(Board(vehiclesCopy));
}
//// Move to the right
if ((c + l <= 5) && (board[r - 1][c + l] == " _ ")){
Car newCar(r, c + 1, l, o);
vehiclesCopy = vehicles;
vehiclesCopy[i] = newCar;
moves.push_back(Board(vehiclesCopy));
}
}
else{
//// Move to the up
if ((r - l >= 1) && (board[r - l - 1][c] == " _ ")){
Car newCar(r - 1, c, l, o);
vehiclesCopy = vehicles;
vehiclesCopy[i] = newCar;
moves.push_back(Board(vehiclesCopy));
}
//// Move to the down
if ((r <= 5) && (board[r][c] == " _ ")){
Car newCar(r + 1, c, l, o);
vehiclesCopy = vehicles;
vehiclesCopy[i] = newCar;
moves.push_back(Board(vehiclesCopy));
}
}
}
return moves;
}
//// Check the equality of boards
//// All positions of the same cars must be equal
bool operator== (const Board &b1, const Board &b2){
return b1.vehicles == b2.vehicles;
}
///////////////////////////////////
// Breadth-First Search Function //
///////////////////////////////////
void bfs(Board &board, char *outputFile){
/*
Search the solutions by breadth first search.
Writes solution on output file as a sequence of board states.
Gives info(nodes, # of solution etc.) on the console.
Arguments:
board: Starting board
outputFile: File to write the solutions as a sequence of board states
*/
Board frontBoard(board.vehicles); //// Copy of board, first element of queue of boards
vector <Board> frontPath; //// First element of queue of paths
vector <Board> discovered;
vector <vector <Board> > solutions;
vector <Board> moves;
map <int, int> depthStates; //// The boards visited at each depth
queue <Board> boardQueue; //// Queue of boards
boardQueue.push(frontBoard);
queue <vector <Board> > pathQueue; //// Queue of paths
vector <Board> newPath;
while (boardQueue.size() != 0){
frontBoard = boardQueue.front();
boardQueue.pop();
if (pathQueue.size() != 0){
frontPath = pathQueue.front();
pathQueue.pop();
}
frontPath.push_back(frontBoard);
newPath = frontPath;
if (!depthStates[newPath.size()]) depthStates[newPath.size()] = 1;
else depthStates[newPath.size()] += 1;
//// If board is discovered, continue
if (find(discovered.begin(), discovered.end(), frontBoard) != discovered.end()) continue;
else discovered.push_back(frontBoard);
//// If board is solved
if (frontBoard.isSolved()) solutions.push_back(newPath);
else{
moves = frontBoard.getMoves();
for (int i=0; i<moves.size(); i++){
boardQueue.push(moves[i]);
pathQueue.push(newPath);
}
}
}
//// Sum of total nodes generated
int maxNodes = 0;
for (int i=0; i<depthStates.size(); i++){
maxNodes += depthStates[i];
}
/////////////////////////
// Checking for cycles //
/////////////////////////
int cycleCount = 0;
for (int i=0; i<solutions.size(); i++){
for (int j=0; j<solutions.size(); j++){
if (i != j){
if (solutions[i].back() == solutions[j].back()) cycleCount++;
}
}
}
//// Find the shortest solution
//// Write it on the output file
vector <Board> shortestSoln;
if (solutions.size() != 0){
shortestSoln = solutions[0];
for (int i=1; i<solutions.size(); i++){
if (solutions[i].size() < shortestSoln.size()) shortestSoln = solutions[i];
}
ofstream writefile;
writefile.open(outputFile);
if (writefile.is_open()){
for (int i=1; i<shortestSoln.size(); i++){
for (int j=0; j<shortestSoln[i].carCount; j++){
writefile << to_string(shortestSoln[i].vehicles[j].row);
writefile << " ";
writefile << to_string(shortestSoln[i].vehicles[j].column);
writefile << " ";
writefile << to_string(shortestSoln[i].vehicles[j].length);
writefile << " ";
writefile << shortestSoln[i].vehicles[j].orient;
writefile << "\n";
}
writefile << "\n";
}
writefile.close();
cout << "The shortest sequence file " << outputFile << " is created." << endl;
}
else cout << "Output file could not be created!" << endl;
}
cout << "TOTAL SOLUTIONS:" << solutions.size() << endl;
cout << "TOTAL CYCLES:" << cycleCount << endl;
cout << "MOVE # OF THE SHORTEST SOLUTION:" << shortestSoln.size() - 1 << endl;
cout << "TOTAL NODES GENERATED:" << discovered.size() << endl;
cout << "MAXIMUM # OF NODES KEPT IN MEMORY:" << maxNodes << endl;
}
///////////////////////////////////
// Depth-First Search Function //
///////////////////////////////////
void dfs(Board &board, char *outputFile){
/*
Search the solutions by depth first search.
Writes solution on output file as a sequence of board states.
Gives info(nodes, # of solution etc.) on the console.
Arguments:
board: Starting board
outputFile: File to write the solutions as a sequence of board states
*/
Board topBoard(board.vehicles); //// Copy of board, top element of stack of boards
vector <Board> topPath; //// Top element of stack of paths
topPath.push_back(topBoard);
vector <Board> discovered;
discovered.push_back(topBoard);
vector <vector <Board> > solutions;
vector <Board> moves;
map <int, int> depthStates; //// The boards visited at each depth
stack <Board> boardStack; //// Stack of boards
boardStack.push(topBoard);
stack <vector <Board> > pathStack; //// Stack of paths
vector <Board> newPath;
moves = topBoard.getMoves();
for (int i=0; i<moves.size(); i++){
boardStack.push(moves[i]);
pathStack.push(topPath);
}
while (boardStack.size() != 0){
topBoard = boardStack.top();
boardStack.pop();
if (pathStack.size() != 0){
topPath = pathStack.top();
pathStack.pop();
}
topPath.push_back(topBoard);
newPath = topPath;
if (!depthStates[newPath.size()]) depthStates[newPath.size()] = 1;
else depthStates[newPath.size()] += 1;
//// If board is discovered, continue
if (find(discovered.begin(), discovered.end(), topBoard) != discovered.end()) continue;
else discovered.push_back(topBoard);
//// If board is solved
if (topBoard.isSolved()) solutions.push_back(newPath);
else{
moves = topBoard.getMoves();
for (int i=0; i<moves.size(); i++){
boardStack.push(moves[i]);
pathStack.push(newPath);
}
}
}
//// Sum of total nodes generated
int maxNodes = 0;
for (int i=0; i<depthStates.size(); i++){
maxNodes += depthStates[i];
}
/////////////////////////
// Checking for cycles //
/////////////////////////
int cycleCount = 0;
for (int i=0; i<solutions.size(); i++){
for (int j=0; j<solutions.size(); j++){
if (i != j){
if (solutions[i].back() == solutions[j].back()) cycleCount++;
}
}
}
//// Find the shortest solution
//// Write it on the output file
vector <Board> shortestSoln;
if (solutions.size() != 0){
shortestSoln = solutions[0];
for (int i=1; i<solutions.size(); i++){
if (solutions[i].size() < shortestSoln.size()) shortestSoln = solutions[i];
}
ofstream writefile;
writefile.open(outputFile);
if (writefile.is_open()){
for (int i=1; i<shortestSoln.size(); i++){
for (int j=0; j<shortestSoln[i].carCount; j++){
writefile << to_string(shortestSoln[i].vehicles[j].row);
writefile << " ";
writefile << to_string(shortestSoln[i].vehicles[j].column);
writefile << " ";
writefile << to_string(shortestSoln[i].vehicles[j].length);
writefile << " ";
writefile << shortestSoln[i].vehicles[j].orient;
writefile << "\n";
}
writefile << "\n";
}
writefile.close();
cout << "The shortest sequence file " << outputFile << " is created." << endl;
}
else cout << "Output file could not be created!" << endl;
}
cout << "TOTAL SOLUTIONS:" << solutions.size() << endl;
cout << "TOTAL CYCLES:" << cycleCount << endl;
cout << "MOVE # OF THE SHORTEST SOLUTION:" << shortestSoln.size() - 1 << endl;
cout << "TOTAL NODES GENERATED:" << discovered.size() << endl;
cout << "MAXIMUM # OF NODES KEPT IN MEMORY:" << maxNodes << endl;
}
/////////////////////////
// Read file, get cars //
/////////////////////////
vector <Car> readCars(char *inputFile){
/*
Reads input file, gets cars.
Arguments:
inputFile: File to read the cars
*/
ifstream readfile;
//// Count the # of cars
int carCount;
readfile.open(inputFile);
if (readfile.is_open()){
string line;
int i;
for (i=0; getline(readfile, line); i++) ;
carCount = i;
readfile.close();
}
else{
cout << "File could not be opened!" << endl;
exit(1);
}
vector <Car> cars;
//// Get cars from file
int r, c, l;
string orientation;
readfile.open(inputFile);
if (readfile.is_open()){
while (!readfile.eof()){
for (int i=0; i<carCount; i++){
readfile >> r;
readfile >> c;
readfile >> l;
readfile >> orientation;
cars.push_back(Car(r, c, l, orientation));
}
}
readfile.close();
}
else{
cout << "File could not be opened!" << endl;
exit(1);
}
return cars;
}
int main(int argc, char* argv[]){
vector <Car> cars = readCars(argv[2]);
Board board(cars);
cout << "\nSTARTING BOARD STATE:\n" << endl;
board.printBoard();
cout << endl;
clock_t t0 = clock();
if (string(argv[1]) == "bfs") bfs(board, argv[3]);
else if (string(argv[1]) == "dfs") dfs(board, argv[3]);
else invalid_argument("Choose 'bfs' or 'dfs' as solution method");
cout << "TOTAL " << string(argv[1]) << " TIME:" << double(clock() - t0)/CLOCKS_PER_SEC << " seconds" << endl;
return 0;
}