forked from wenxichang/calendar_puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar_puzzle.c
307 lines (270 loc) · 5.34 KB
/
calendar_puzzle.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef struct cord_st {
int y;
int x;
} cord_st;
typedef struct pile_st {
cord_st cord;
char pile[4][4];
} pile_st;
typedef struct block_st {
int nr_pile;
pile_st piles[8];
} block_st;
static block_st blocks[8] = {
{
.nr_pile = 4, .piles = {{
.pile = {
{ 1, 1 },
{ 0, 1 },
{ 1, 1 },
}
}}
},
{
.nr_pile = 2, .piles = {{
.pile = {
{ 3, 3, 3 },
{ 3, 3, 3 },
}
}}
},
{
.nr_pile = 8, .piles = {{
.pile = {
{ 5, 0 },
{ 5, 5 },
{ 5, 5 },
}
}}
},
{
.nr_pile = 8, .piles = {{
.pile = {
{ 0, 0, 7 },
{ 7, 7, 7},
{ 7, 0, 0 },
}
}}
},
{
.nr_pile = 8, .piles = {{
.pile = {
{ 0, 0, 9 },
{ 0, 0, 9 },
{ 9, 9, 9 },
}
}}
},
{
.nr_pile = 8, .piles = {{
.pile = {
{ 11, 11 },
{ 11, 0 },
{ 11, 0 },
{ 11, 0 },
}
}}
},
{
.nr_pile = 8, .piles = {{
.pile = {
{ 13, 13, 13, 0 },
{ 0, 0, 13, 13 },
}
}}
},
{
.nr_pile = 8, .piles = {{
.pile = {
{ 15, 15, 15, 15 },
{ 0, 15, 0, 0 },
}
}}
}
};
typedef struct map_st {
int used;
char m[13][13];
} map_st;
static const map_st origin_map = {
.used = 0,
.m = {
{ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 },
{ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 },
{ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 },
{ 17, 17, 17, 0, 0, 0, 0, 0, 0, 17, 17, 17, 17 },
{ 17, 17, 17, 0, 0, 0, 0, 0, 0, 17, 17, 17, 17 },
{ 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 17, 17, 17 },
{ 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 17, 17, 17 },
{ 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 17, 17, 17 },
{ 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 17, 17, 17 },
{ 17, 17, 17, 0, 0, 0, 17, 17, 17, 17, 17, 17, 17 },
{ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 },
{ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 },
{ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 },
}
};
static void rotate(char f[4][4], char t[4][4])
{
int i, j;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
t[j][3 - i] = f[i][j];
}
}
}
static void flip(char f[4][4], char t[4][4])
{
int i, j;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
t[i][3 - j] = f[i][j];
}
}
}
static void init_cord(pile_st *p)
{
int i, j;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
if (p->pile[i][j]) {
p->cord.y = i;
p->cord.x = j;
return;
}
}
}
assert(0);
}
static void init_block(block_st *b)
{
rotate(b->piles[0].pile, b->piles[1].pile);
if (b->nr_pile > 2) {
rotate(b->piles[1].pile, b->piles[2].pile);
rotate(b->piles[2].pile, b->piles[3].pile);
if (b->nr_pile > 4) {
flip(b->piles[0].pile, b->piles[4].pile);
flip(b->piles[1].pile, b->piles[5].pile);
flip(b->piles[2].pile, b->piles[6].pile);
flip(b->piles[3].pile, b->piles[7].pile);
}
}
int i;
for (i = 0; i < b->nr_pile; ++i) {
init_cord(&b->piles[i]);
}
}
static void init_blocks(void)
{
int i;
for (i = 0; i < 8; ++i) {
init_block(&blocks[i]);
}
}
static map_st *fill(map_st *old, int y, int x, pile_st *p)
{
int sy = y - p->cord.y;
int sx = x - p->cord.x;
if (((*(unsigned int *)(&old->m[sy][sx])) & (*(unsigned int *)(&p->pile[0][0]))) != 0)
return NULL;
if (((*(unsigned int *)(&old->m[sy + 1][sx])) & (*(unsigned int *)(&p->pile[1][0]))) != 0)
return NULL;
if (((*(unsigned int *)(&old->m[sy + 2][sx])) & (*(unsigned int *)(&p->pile[2][0]))) != 0)
return NULL;
if (((*(unsigned int *)(&old->m[sy + 3][sx])) & (*(unsigned int *)(&p->pile[3][0]))) != 0)
return NULL;
map_st *news = malloc(sizeof(map_st));
memcpy(news, old, sizeof(map_st));
int i, j;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
if (p->pile[i][j]) {
news->m[sy + i][sx + j] = p->pile[i][j];
}
}
}
return news;
}
static void show_map(map_st *s)
{
const char *str = " A B C D E F G J ";
int i, j;
for (i = 3; i < 10; ++i) {
for (j = 3; j < 10; ++j) {
printf(" %c", str[(int)s->m[i][j]]);
}
printf("\n");
}
}
static int fill_and_check(map_st *s)
{
int i, j;
int x, y;
for (i = 3; i < 10; ++i) {
for (j = 3; j < 10; ++j) {
if (s->m[i][j] == 0) {
y = i;
x = j;
goto start_fill;
}
}
}
start_fill:
for (i = 0; i < 8; ++i) {
if (s->used & (1 << i))
continue;
for (j = 0; j < blocks[i].nr_pile; ++j) {
map_st *news = fill(s, y, x, &blocks[i].piles[j]);
if (!news)
continue;
news->used |= (1 << i);
if (news->used == 0xff) {
printf("found\n");
show_map(news);
free(news);
return 0;
}
if (fill_and_check(news) == 0) {
free(news);
return 0;
}
free(news);
}
}
return -1;
}
static int solve(int month, int day)
{
map_st *start = malloc(sizeof(map_st));
memcpy(start, &origin_map, sizeof(map_st));
month--;
day--;
start->m[3 + month / 6][3 + month % 6] = 17;
start->m[5 + day / 7][3 + day % 7] = 17;
int res = fill_and_check(start);
free(start);
return res;
}
int main(int argc, char **argv)
{
if (argc != 3) {
printf("Usage: %s <month(1-12)> <day(1-31)>\n", argv[0]);
return 1;
}
init_blocks();
int month = atoi(argv[1]);
int day = atoi(argv[2]);
if (month < 1 || month > 12) {
printf("Invalid month: %s\n", argv[1]);
return 1;
}
if (day < 1 || day > 31) {
printf("Invalid day: %s\n", argv[2]);
return 1;
}
return solve(month, day);
}