-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
191 lines (175 loc) · 6.24 KB
/
Copy pathmain.cpp
File metadata and controls
191 lines (175 loc) · 6.24 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
#include <iostream>
#define N 3
#define M 3
inline uint_fast64_t factorial(int n) {
uint_fast64_t c, fact = 1;
for (c = 1; c <= n; c++) {
fact = fact * c;
}
return fact;
}
void getInitBoard(uint_fast8_t board[N][M]) {
for (uint_fast8_t i=0; i<M; i++) {
for(uint_fast8_t j=0; j<N; j++) {
board[i][j] = static_cast<uint_fast8_t>(j + i * N + 1);
}
}
}
uint_fast64_t hashBoard(uint_fast8_t board[N][M]) {
uint_fast64_t boardHash = 0;
for(uint_fast8_t i = 0; i < M; i++) {
for(uint_fast8_t j = 0; j < N; j++) {
uint_fast64_t result = 1;
for (uint_fast8_t k = 1; k <= (i*N+j); k++) {
result *= N*M;
}
boardHash += board[i][j]*result;
}
}
return boardHash;
}
void unHashBoard(uint_fast64_t boardHash, uint_fast8_t board[N][M]) {
uint_fast64_t currentHash = boardHash;
for (uint_fast8_t i=0; i<M; i++) {
for(uint_fast8_t j=0; j<N; j++) {
auto current = static_cast<uint_fast8_t>(currentHash % (M * N));
if (current) {
board[i][j] = current;
currentHash /= N*M;
} else {
board[i][j] = N * M;
currentHash /= N*M;
currentHash--;
}
}
}
}
int main() {
clock_t start = clock();
#if ((N*M)%2)
uint_fast64_t numBoards = factorial(N * M);
#else
uint_fast64_t numBoards = factorial(N * M)/2;
#endif
auto * boards = static_cast<uint_fast64_t *>(malloc(numBoards * sizeof(uint_fast64_t)));
uint_fast64_t lastIndex = 1;
auto * currentBoards = static_cast<uint_fast64_t *>(malloc(1 * sizeof(uint_fast64_t)));
uint_fast64_t numberCurrentBoards = 1;
uint_fast8_t initBoard[N][M] = {{0}};
getInitBoard(initBoard);
boards[0] = hashBoard(initBoard);
currentBoards[0] = boards[0];
uint_fast8_t iteration = 0;
bool found = true;
uint_fast8_t board[N][M] = {{0}};
uint_fast8_t tmpBoard[N][M] = {{0}};
while (found) {
iteration++;
found = false;
auto * nextBoards = static_cast<uint_fast64_t *>(malloc(numberCurrentBoards*(N*2+M*2) * sizeof(uint_fast64_t)));
uint_fast64_t nextBoardsIndex = 0;
for (uint_fast64_t i = 0; i<numberCurrentBoards; i++) {
unHashBoard(currentBoards[i], board);
//up
for (uint_fast8_t j = 0; j<N; j++) {
memcpy(tmpBoard, board, sizeof(tmpBoard));
for (uint_fast8_t k = 1; k<M; k++) {
tmpBoard[j][k] = board[j][k-1];
}
tmpBoard[j][0] = board[j][M-1];
bool inBoards = false;
uint_fast64_t tmpBoardHash = hashBoard(tmpBoard);
for (uint_fast64_t k = 0; k<lastIndex; k++) {
if (boards[k]==tmpBoardHash) {
inBoards = true;
}
}
if (!inBoards) {
found = true;
boards[lastIndex++] = tmpBoardHash;
nextBoards[nextBoardsIndex++] = tmpBoardHash;
}
}
//down
for (uint_fast8_t j = 0; j<N; j++) {
memcpy(tmpBoard, board, sizeof(tmpBoard));
for (uint_fast8_t k = 0; k<M-1; k++) {
tmpBoard[j][k] = board[j][k+1];
}
tmpBoard[j][M-1] = board[j][0];
bool inBoards = false;
uint_fast64_t tmpBoardHash = hashBoard(tmpBoard);
for (uint_fast64_t k = 0; k<lastIndex; k++) {
if (boards[k]==tmpBoardHash) {
inBoards = true;
}
}
if (!inBoards) {
found = true;
boards[lastIndex++] = tmpBoardHash;
nextBoards[nextBoardsIndex++] = tmpBoardHash;
}
}
//left
for (uint_fast8_t j = 0; j<M; j++) {
memcpy(tmpBoard, board, sizeof(tmpBoard));
for (uint_fast8_t k = 0; k<N-1; k++) {
tmpBoard[k][j] = board[k+1][j];
}
tmpBoard[N-1][j] = board[0][j];
bool inBoards = false;
uint_fast64_t tmpBoardHash = hashBoard(tmpBoard);
for (uint_fast64_t k = 0; k<lastIndex; k++) {
if (boards[k]==tmpBoardHash) {
inBoards = true;
}
}
if (!inBoards) {
found = true;
boards[lastIndex++] = tmpBoardHash;
nextBoards[nextBoardsIndex++] = tmpBoardHash;
}
}
//right
for (uint_fast8_t j = 0; j<M; j++) {
memcpy(tmpBoard, board, sizeof(tmpBoard));
for (uint_fast8_t k = 1; k<N; k++) {
tmpBoard[k][j] = board[k-1][j];
}
tmpBoard[0][j] = board[N-1][j];
bool inBoards = false;
uint_fast64_t tmpBoardHash = hashBoard(tmpBoard);
for (uint_fast64_t k = 0; k<lastIndex; k++) {
if (boards[k]==tmpBoardHash) {
inBoards = true;
}
}
if (!inBoards) {
found = true;
boards[lastIndex++] = tmpBoardHash;
nextBoards[nextBoardsIndex++] = tmpBoardHash;
}
}
}
free(currentBoards);
currentBoards = nextBoards;
numberCurrentBoards = nextBoardsIndex;
for (uint_fast64_t i = 0; i<numberCurrentBoards; i++) {
printf("(");
unHashBoard(currentBoards[i], board);
for (auto &k : board) {
printf("(");
for (uint_fast8_t j : k) {
printf("%d, ", j);
}
printf(")");
}
printf(")\n");
}
printf("Iteration: %i\n\n",iteration);
}
clock_t stop = clock();
double elapsed = (double) (stop - start) / CLOCKS_PER_SEC;
printf("\nTime elapsed: %.5f\n", elapsed);
return 0;
}