-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
239 lines (193 loc) · 6.6 KB
/
test.c
File metadata and controls
239 lines (193 loc) · 6.6 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
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#define NbRows 8
#define NbColumns 7
#define NbPieces 10
#define MaxRotations 4
int board[NbRows][NbColumns] = {0};
int total = 0;
int size;
int rotation = 4;
/*
bool matrixHasNoZero(int matrix[NbRows][NbColumns]) {
for (int i = 0; i < NbRows; i++) {
for (int j = 0; j < NbColumns; j++) {
if (matrix[i][j] == 0) {
return false;
}
}
}
return true;
}*/
int pieces[NbPieces][MaxRotations][5][2] = {
//Le L inversé
{{{0, 1}, {1, 1}, {2, 1}, {3, 0}, {3,1}}, {{0, 0}, {1, 0}, {1, 1}, {1, 2}, {1,3}}, {{0, 0}, {0, 1}, {1, 0}, {2, 0}, {3,0}}, {{0, 0}, {0, 1}, {0, 2}, {0, 3}, {1,3}}},
//Le T
{{{0, 1}, {1, 1}, {2, 0}, {2, 1}, {2,2}}, {{0, 0}, {1, 0}, {1, 1}, {1, 2}, {2,0}}, {{0, 0}, {0, 1}, {0, 2}, {1, 1}, {2,1}}, {{0, 2}, {1, 0}, {1, 1}, {1, 2}, {2,2}}},
//Le coin
{{{0,0}, {0,1}, {0,2}, {1,0}, {2,0}}, {{0,0}, {0,1}, {0,2}, {1, 2}, {2,2}}, {{0, 2}, {1, 2}, {2, 0}, {2,1}, {2,2}}, {{0, 0}, {1, 0}, {2, 0}, {2,1}, {2,2}}},
//Le Z (symétrie)
{{{0,0}, {0,1}, {1,1}, {2, 1}, {2,2}}, {{0, 2}, {1, 0}, {1, 1}, {1, 2}, {2,0}}},
//Le long Z
{{{0,1}, {1,1}, {2,0}, {2, 1}, {3,0}}, {{0, 0}, {0, 1}, {1, 1}, {1, 2}, {1,3}}, {{0,1}, {1,0}, {1,1}, {2, 0}, {3,0}}, {{0,0}, {0,1}, {0, 2}, {1, 2}, {1,3}}},
//LE U
{{{0,0}, {0,2}, {1,0}, {1, 1}, {1,2}}, {{0, 0}, {0, 1}, {1, 0}, {2,0}, {2,1}}, {{0,0}, {0,1}, {0,2}, {1,0}, {1,2}}, {{0,0},{0,1},{1,1},{2,0},{2,1}}},
//Le radio
{{{0, 0}, {1, 0}, {1, 1}, {2,0}, {2,1}}, {{0, 0}, {0, 1}, {0,2}, {1,0}, {1,1}}, {{0,0}, {0,1}, {1,0}, {1,1}, {2,1}}, {{0,1},{0,2},{1,0},{1,1},{1,2}}},
//Le petit L inversé
{{{0,1}, {1,1}, {2,0}, {2,1}}, {{0,0}, {1,0}, {1,1}, {1,2}}, {{0,0}, {0,1}, {1,0}, {2,0}}, {{0,0}, {0,1}, {0,2}, {1,2}}},
//Le Z inversé debout
{{{0,0}, {1,0}, {1,1}, {2,1}}, {{0,1}, {0,2}, {1,0}, {1,1}}},
//Le I renversé
{{{0,0}, {0,1}, {0,2}, {0,3}}, {{0,0}, {1,0}, {2,0}, {3,0}}},
//Le L
//{{{0,0}, {1,0}, {2,0}, {2,1}}, {{0,0}, {0,1}, {0,2}, {1,0}}, {{0,0}, {0,1}, {1,1}, {1,2}}, {{0,2}, {1,0}, {1,1}, {1,2}}},
//I
//{{{0,0}, {1,0}}, {{0,0}, {1,0}}},
//Le coin
//{{{0,0}, {1,0}, {1,1}}, {{0,0}, {0,1}, {1,0}}, {{0,0}, {0,1}, {1,1}}, {{0,1}, {1,0}, {1,1}}}
};
bool PlacePiece(int piece_num, int rotation_num, int x, int y) {
if(piece_num <=6)
{
size = 5;
if(piece_num == 3){
rotation = 2;
}
else{
rotation = 4;
}
}
else{
size = 4;
if(piece_num >= 8){
rotation = 2;
}else{
rotation = 4;
}
}
for (int i = 0; i < size; i++) {
int piece_x = pieces[piece_num][rotation_num][i][0];
int piece_y = pieces[piece_num][rotation_num][i][1];
if (x + piece_x < 0 || x + piece_x >= NbRows || y + piece_y < 0 || y + piece_y >= NbColumns) {
return false;
}
if (board[x + piece_x][y + piece_y] != 0) {
return false;
}
}
for (int i = 0; i < size; i++) {
//printf("%i\n", size);
int piece_x = pieces[piece_num][rotation_num][i][0];
int piece_y = pieces[piece_num][rotation_num][i][1];
board[x + piece_x][y + piece_y] = piece_num + 1;}
return true;
}
void remove_piece(int piece_num, int rotation_num, int x, int y) {
if(piece_num <=6)
{
size = 5;
if(piece_num == 3){
rotation = 2;
}
else{
rotation = 4;
}
}
else{
size = 4;
if(piece_num >= 8){
rotation = 2;
}else{
rotation = 4;
}
}
//printf("piece_num = %d, size = %d, rotation_num = %d, x = %d, y= %d\n", piece_num, size, rotation_num, x, y);
for (int i = 0; i < size; i++) {
int piece_x = pieces[piece_num][rotation_num][i][0];
int piece_y = pieces[piece_num][rotation_num][i][1];
board[x + piece_x][y + piece_y] = 0;
}
}
void solve_tetro_puzzle(int piece_num, int number) {
int r, i ,j;
if(piece_num <=6)
{
if(piece_num == 3){
rotation = 2;
}
else{
rotation = 4;
}
}
else{
if(piece_num >= 8){
rotation = 2;
}else{
rotation = 4;
}
}
//printf("Placing piece current number : %i\n", piece_num);
for(r = 0; r < rotation; r++){
for(i = 0; i < NbRows; i++){
for(j = 0; j < NbColumns; j++){
//printf("i = %i, j = %i, r = %i\n", i, j, r);
if(PlacePiece(piece_num, r, i, j))
{
/*for (int y = 0; y < NbRows; y++) {
for (int x = 0; x < NbColumns; x++) {
printf("%d ", board[y][x]);
}
printf("\n");
}
printf("\n");*/
//printf("Piece placed number : %i\n", piece_num);
if (piece_num == number)
{
total += 1;
/*printf("Solution found (%d)!\n", total);
for (int x = 0; x < NbRows; x++) {
for (int y = 0; y < NbColumns; y++) {
printf("%d ", board[x][y]);
}
printf("\n");
}
printf("\n");*/
}
else
{
//printf("%s\n", "Placing another piece");
solve_tetro_puzzle(piece_num + 1, number);
}
//printf("Removing piece number %i\n", piece_num+1);
remove_piece(piece_num, r, i, j);
}
}
}
}
//printf("%s\n", "all rotations done");
}
int main(){
// to store the execution time of code
double time_spent = 0.0;
clock_t begin = clock();
board[0][6] = 9;
board[1][6] = 9;
board[7][0] = 9;
board[7][1] = 9;
board[7][2] = 9;
board[7][3] = 9;
board[0][0] = 9;
board[2][0] = 9;
//board[6][3] = 9;
board[7][6] = 9;
solve_tetro_puzzle(0, NbPieces-1);
printf("total = %i\n", total);
clock_t end = clock();
// calculate elapsed time by finding difference (end - begin) and
// dividing the difference by CLOCKS_PER_SEC to convert to seconds
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
printf("The elapsed time is %f seconds", time_spent);
return 0;
}