-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cpp
360 lines (327 loc) · 10.3 KB
/
Program.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
/*
* Program to solve any level of the game Pushing Machine
* Abraham Santos [email protected]
*/
#include <vector>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <fstream>
#include <sstream>
using namespace std;
enum Square { Empty, Block };
enum Direction { Up, Down, Left, Right };
struct Position {
int column, row;
};
struct Machine {
Position position;
Direction direction;
};
struct MachineState {
Position position;
int length;
};
struct MazeState {
std::vector<Position> boxes;
std::vector<MachineState> machines;
};
inline bool operator<(const MazeState &a, const MazeState &b)
{
return a.boxes != b.boxes ? a.boxes < b.boxes : a.machines < b.machines;
}
inline bool operator==(const Position &a, const Position &b)
{
return a.column == b.column && a.row == b.row;
}
inline bool operator<(const Position &a, const Position &b)
{
return a.row != b.row ? a.row < b.row : a.column < b.column;
}
inline bool operator<(const MachineState &a, const MachineState &b)
{
return a.length != b.length ? a.length < b.length : a.position < b.position;
}
Square maze[8][6]; // the maze of empty or block squares
std::vector<Position> boxes; // start position of the boxes
std::vector<Machine> machines; // start position of machines
std::vector<Position> targets; // position of targets
std::vector<Position> solution; // the solution where each step is a row and column element in the array
const string level_names[] = { "Practice", "Beginner", "Amateur", "Average",
"Experienced", "Skilled", "Professional", "Expert",
"Genius", "Extreme", "Ultimate", "Impossible" };
void error(const char *msg)
{
cerr << msg << endl;
exit(0);
}
void read_input()
{
cout << " *** PUSHING MACHINE SOLVER by Abraham Santos ***\n";
cout << "Levels:\n";
for (int i = 0; i < 12; ++i) {
cout << i+1 << ". " << level_names[i] << "\n";
}
cout << "Enter the number of the level: ";
int name_num = 0;
cin >> name_num;
if (cin.bad() || name_num <= 0 || name_num > 12)
error("Wrong number read");
cout << "Which puzzle number do you want to solve? ";
int puzzle_num = 0;
cin >> puzzle_num;
ostringstream oss;
oss << "./levels/" << level_names[name_num-1] << "/" << puzzle_num;
ifstream file(oss.str());
if (!file.is_open())
error("Level file not available");
string line;
getline(file, line);
getline(file, line);
for (int i = 0; i < 8; ++i) {
getline(file, line);
for (int j = 0; j < 6; ++j) {
if (line[2*j + 1] == 'T')
targets.push_back({ j, i });
}
}
getline(file, line);
for (int i = 0; i < 8; ++i) {
getline(file, line);
for (int j = 0; j < 6; ++j) {
switch (line[2*j + 1]) {
case '@': maze[i][j] = Block; break;
case '.': maze[i][j] = Empty; break;
case 'v':
machines.push_back({ { j, i }, Down });
maze[i][j] = Empty;
break;
case '^':
machines.push_back({ { j, i }, Up });
maze[i][j] = Empty;
break;
case '<':
machines.push_back({ { j, i }, Left });
maze[i][j] = Empty;
break;
case '>':
machines.push_back({ { j, i }, Right });
maze[i][j] = Empty;
break;
case 'T':
boxes.push_back({ j, i });
maze[i][j] = Empty;
break;
default:
error("Invalid character");
}
}
}
}
void create_start_state(MazeState &state)
{
state.boxes = boxes;
auto machineCount = machines.size();
state.machines.resize(machineCount);
for (unsigned i = 0; i < machineCount; ++i) {
state.machines[i].position = machines[i].position;
state.machines[i].length = 1;
}
}
void front_squares(const MachineState &machine,
Direction direction, Position &pos1, Position &pos2)
{
pos1 = pos2 = machine.position;
switch (direction) {
case Up:
pos1.row -= machine.length;
pos2.row -= machine.length + 1;
break;
case Down:
pos1.row += machine.length;
pos2.row += machine.length + 1;
break;
case Left:
pos1.column -= machine.length;
pos2.column -= machine.length + 1;
break;
case Right:
pos1.column += machine.length;
pos2.column += machine.length + 1;
break;
}
}
bool out_of_limits(const Position &position)
{
return (position.column < 0 || position.column >= 6) || (position.row < 0 || position.row >= 8);
}
bool is_block(const Position &position)
{
return maze[position.row][position.column] == Block;
}
bool value_between(int x, int a, int b)
{
return a <= x && x <= b;
}
bool expanded_machine_placed(const MazeState &state, const Position &position)
{
auto machineCount = machines.size();
for (unsigned i = 0; i < machineCount; ++i) {
auto &machine = state.machines[i];
switch (machines[i].direction) {
case Up:
if (machine.position.column == position.column &&
value_between(position.row, machine.position.row - machine.length + 1, machine.position.row))
return true;
break;
case Down:
if (machine.position.column == position.column &&
value_between(position.row, machine.position.row, machine.position.row + machine.length - 1))
return true;
break;
case Left:
if (machine.position.row == position.row &&
value_between(position.column, machine.position.column - machine.length + 1, machine.position.column))
return true;
break;
case Right:
if (machine.position.row == position.row &&
value_between(position.column, machine.position.column, machine.position.column + machine.length - 1))
return true;
break;
}
}
return false;
}
bool unexpanded_machine_placed(const MazeState &state,
const Position &position, unsigned *machineIndex)
{
unsigned index = 0;
for (const auto &machine : state.machines) {
if (machine.length == 1 && machine.position == position) {
if (machineIndex != nullptr)
*machineIndex = index;
return true;
}
++index;
}
return false;
}
bool box_placed(const MazeState &state, const Position &position, unsigned *boxIndex = nullptr)
{
auto boxCount = state.boxes.size();
for (unsigned i = 0; i < boxCount; ++i) {
if (state.boxes[i] == position) {
if (boxIndex != nullptr)
*boxIndex = i;
return true;
}
}
return false;
}
bool grow_machine(MazeState &state, int machineIndex)
{
auto &machine = state.machines[machineIndex];
Position frontalPos1, frontalPos2;
Direction direction = machines[machineIndex].direction;
unsigned index;
front_squares(machine, direction, frontalPos1, frontalPos2);
if (out_of_limits(frontalPos1) || is_block(frontalPos1))
return false;
if (box_placed(state, frontalPos1, &index)) {
if (out_of_limits(frontalPos2) || is_block(frontalPos2) || box_placed(state, frontalPos2)
|| expanded_machine_placed(state, frontalPos2))
return false;
++machine.length;
state.boxes[index] = frontalPos2;
return true;
}
if (unexpanded_machine_placed(state, frontalPos1, &index)) {
if (out_of_limits(frontalPos2) || is_block(frontalPos2) || box_placed(state, frontalPos2)
|| expanded_machine_placed(state, frontalPos2))
return false;
++machine.length;
state.machines[index].position = frontalPos2;
return true;
}
if (expanded_machine_placed(state, frontalPos1))
return false;
++machine.length;
return true;
}
bool raise_machine(MazeState &state, int machineIndex)
{
auto &machineLength = state.machines[machineIndex].length;
if (machineLength > 1) {
machineLength = 1;
return true;
}
if (machineLength == 1) {
while (grow_machine(state, machineIndex));
return machineLength > 1;
}
return false;
}
bool is_solved(const MazeState &state)
{
for (const auto &box : state.boxes) {
auto begin = targets.begin();
auto end = targets.end();
if (find(begin, end, box) == end) {
return false;
}
}
return true;
}
bool solve()
{
typedef map<MazeState, vector<Position>> MapStates;
MapStates mapStates;
vector<MazeState> vector1, vector2;
MazeState start;
auto machineCount = machines.size();
create_start_state(start);
vector1.push_back(start);
mapStates[start];
do {
vector2.clear();
for (const auto &parentState : vector1) {
if (is_solved(parentState)) {
solution = mapStates[parentState];
return true;
}
for (unsigned i = 0; i < machineCount; ++i) {
MazeState childState = parentState;
if (raise_machine(childState, i)) {
if (mapStates.find(childState) == mapStates.end()) {
auto newpath = mapStates[parentState];
newpath.push_back(parentState.machines[i].position);
mapStates[childState] = newpath;
vector2.push_back(childState);
}
}
}
}
vector1.swap(vector2);
} while (!vector1.empty());
return false;
}
void show_solution()
{
cout << "The solution is the following (rows and columns start from 1):\n";
for (const auto &pos : solution) {
cout << "Tap machine on row " << pos.row + 1 << " and column " << pos.column + 1 << "\n";
}
cout << solution.size() << " moves\n";
}
int main()
{
read_input();
cout << "Solving maze...\n";
if (solve())
show_solution();
else
error("Unable to solve the given puzzle");
return 0;
}