-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
168 lines (132 loc) · 3.97 KB
/
Copy pathmain.cpp
File metadata and controls
168 lines (132 loc) · 3.97 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
#include <iostream>
#include "Vector3.h"
#include <iomanip>
#include <ctime>
#ifdef DEBUG
int depth = 0;
int maxdepth = 0;
int startingpos = 0;
#endif
using namespace std;
bool solve(const char *rods, Vector3 pos, Vector3 facing, Vector3 *solution);
bool placeRod(Vector3 pos, Vector3 dir, char rod);
void removeRod(Vector3 pos, Vector3 dir, char rod);
bool tryMove(Vector3 dir, const char *rods, Vector3 pos, Vector3 *solution);
bool space[4][4][4] = {};
const char rodsN = 39;
const char rods[rodsN + 1] = {3, 1, 3, 1, 1,
1, 1, 2, 1, 1,
1, 1, 1, 2, 1,
3, 1, 2, 2, 3,
1, 2, 1, 1, 1,
1, 1, 1, 1, 1,
1, 3, 1, 3, 1,
3, 3, 3, 2, -1};
int main() {
time_t start = time(0);
cout << "A program for solving THE SNAKE CUBE" << endl << endl;
cout << "start: " << ctime(&start);
Vector3 solution[rodsN];
Vector3 facing = Vector3(0, 0, 0);
bool solved = false;
Vector3 pos;
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
for (int z = 0; y < 2; y++) {
#ifdef DEBUG
startingpos++;
#endif
pos = Vector3(x, y, z);
solved = solve(rods, pos, facing, solution);
#ifdef DEBUG
maxdepth=0;
depth=0;
#endif
if (solved) { break; }
}
if (solved) { break; }
}
if (solved) { break; }
}
time_t endTime = time(0);
cout << "end: " << ctime(&endTime);
cout << endl << "Start at position " << pos << endl << endl;
for (int i = 0; i < rodsN; i++) {
cout << solution[i] << endl;
}
return 0;
}
bool solve(const char *rods, Vector3 pos, Vector3 facing, Vector3 *solution) {
if (*rods == -1) return true;
#ifdef DEBUG
depth++;
if (depth > maxdepth) { maxdepth = depth; };
cout << setw(depth) << "|" << depth << endl;
cout << setw(maxdepth) << "|" << maxdepth << endl;
cout << startingpos;
#endif
Vector3 dir;
if (facing.x == 0) {
dir = Vector3(1, 0, 0);
if (tryMove(dir, rods, pos, solution)) { return true; }
dir = Vector3(-1, 0, 0);
if (tryMove(dir, rods, pos, solution)) { return true; }
}
if (facing.y == 0) {
dir = Vector3(0, 1, 0);
if (tryMove(dir, rods, pos, solution)) { return true; }
dir = Vector3(0, -1, 0);
if (tryMove(dir, rods, pos, solution)) { return true; }
}
if (facing.z == 0) {
dir = Vector3(0, 0, 1);
if (tryMove(dir, rods, pos, solution)) { return true; }
dir = Vector3(0, 0, -1);
if (tryMove(dir, rods, pos, solution)) { return true; }
}
#ifdef DEBUG
depth--;
#endif
return false;
}
bool tryMove(Vector3 dir, const char *rods, Vector3 pos, Vector3 *solution) {
bool fits = placeRod(pos, dir, *rods);
if (fits) {
Vector3 newPos = pos + (dir * *rods);
if (solve(rods + 1, newPos, dir, solution + 1)) {
*solution = dir;
return true;
} else {
removeRod(pos, dir, *rods);
}
} else {
}
return false;
}
bool placeRod(Vector3 pos, Vector3 dir, char rod) {
//check if rod is within bounds
Vector3 newPos = pos + (dir * rod);
if (!(newPos < 4 && newPos > -1)) {
return false;
}
//check for collision with placed rods
for (int i = 1; i <= rod; i++) {
Vector3 p = pos + dir * i;
if (space[p.x][p.y][p.z]) {
return false;
}
}
//place rod
for (int i = 0; i <= rod; i++) {
Vector3 p = pos + dir * i;
space[p.x][p.y][p.z] = true;
}
return true;
}
void removeRod(Vector3 pos, Vector3 dir, char rod) {
Vector3 newPos = pos + (dir * rod);
for (int i = 0; i <= rod; i++) {
Vector3 p = pos + (dir * i);
space[p.x][p.y][p.z] = false;
}
}