-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitx.c
373 lines (340 loc) · 7.99 KB
/
fitx.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
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
361
362
363
364
365
366
367
368
369
370
371
372
/*--------------------------------------------------------------*
* Fit words into a crossword grid
*--------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BLANK ' '
#define STOP_H '-'
#define STOP_V '|'
#define STOP_HV '#'
#define is_letter(c) isalpha(c)
#define MAXLEN 15
#define MAXWORD 20
#define CON_SCORE 10
typedef struct {
char word[MAXLEN+1];
int len;
int optional;
} Word;
typedef struct {
char pos[MAXLEN+2][MAXLEN+2];
int xmax, ymax;
int score;
} Grid;
typedef struct {
enum {INIT, HORIZCON, VERTCON, HORIZ, VERT, OMIT} state;
int x, y;
int xmax, ymax, len;
} Iter;
/*--------------------------------------------------------------*
* Implementation of Word
*--------------------------------------------------------------*/
void Word_Word(Word *word, const char *line)
{
word->optional = line[0]=='?';
strcpy(word->word, line+word->optional);
word->len = strlen(word->word);
}
/*--------------------------------------------------------------*
* Grid into which words are placed
*--------------------------------------------------------------*/
void Grid_Grid(Grid *grid, int xmax, int ymax)
{
int x, y;
grid->xmax = xmax;
grid->ymax = ymax;
for (y=0; y<ymax+2; y++)
for (x=0; x<xmax+2; x++)
grid->pos[y][x] = BLANK;
grid->score = 0;
}
void Grid_print(Grid *grid)
{
int x, y, c;
for (y=0; y<grid->ymax; y++) {
for (x=0; x<grid->xmax; x++) {
c = grid->pos[1+y][1+x];
printf(" %c", is_letter(c) ? c : '.');
}
printf("\n");
}
printf("Score: %d\n\n", grid->score);
}
/*--------------------------------------------------------------*
* Iteration through possible positions
*--------------------------------------------------------------*/
void Iter_Iter(Iter *iter, Grid *grid, Word *word)
{
iter->xmax = grid->xmax;
iter->ymax = grid->ymax;
iter->len = word->len;
iter->state = INIT;
}
int Iter_next(Iter *iter)
{
switch (iter->state) {
case INIT:
iter->y = 0;
hcy:
if (iter->y == iter->ymax) goto ehcy;
iter->x = 0;
hcx:
if (iter->x + iter->len > iter->xmax) goto ehcx;
iter->state = HORIZCON;
return 1;
case HORIZCON:
++iter->x;
goto hcx;
ehcx:
++iter->y;
goto hcy;
ehcy:
iter->x = 0;
vcx:
if (iter->x == iter->xmax) goto evcx;
iter->y = 0;
vcy:
if (iter->y + iter->len > iter->ymax) goto evcy;
iter->state = VERTCON;
return 1;
case VERTCON:
++iter->y;
goto vcy;
evcy:
++iter->x;
goto vcx;
evcx:
iter->y = 0;
hy:
if (iter->y == iter->ymax) goto ehy;
iter->x = 0;
hx:
if (iter->x + iter->len > iter->xmax) goto ehx;
iter->state = HORIZ;
return 1;
case HORIZ:
++iter->x;
goto hx;
ehx:
++iter->y;
goto hy;
ehy:
iter->x = 0;
vx:
if (iter->x == iter->xmax) goto evx;
iter->y = 0;
vy:
if (iter->y + iter->len > iter->ymax) goto evy;
iter->state = VERT;
return 1;
case VERT:
++iter->y;
goto vy;
evy:
++iter->x;
goto vx;
evx:
iter->state = OMIT;
return 1;
case OMIT:
;
}
return 0;
}
/*--------------------------------------------------------------*
* See if a word will fit in a given position
*--------------------------------------------------------------*/
int Grid_fitword_h(Grid *grid, Word *word, Iter *iter)
{
int x=iter->x, y=iter->y, len=iter->len;
int result, i;
/*
* First check the end positions
*/
if (is_letter(grid->pos[1+y][1+x-1])) return 0;
if (is_letter(grid->pos[1+y][1+x+len])) return 0;
/*
* Now check each letter in turn, noting if we connect
*/
result = 1;
for (i=0; i<len; i++) {
int c = grid->pos[1+y][1+x+i];
if (c==word->word[i]) { /* connects with old word */
result = 2;
}else if (is_letter(c) ||
c==STOP_H || c==STOP_HV) { /* blocked */
return 0;
} /* BLANK or STOP_V are OK */
}
return result;
}
int Grid_fitword_v(Grid *grid, Word *word, Iter *iter)
{
int x=iter->x, y=iter->y, len=iter->len;
int result, i;
/*
* First check the end positions
*/
if (is_letter(grid->pos[1+y-1][1+x])) return 0;
if (is_letter(grid->pos[1+y+len][1+x])) return 0;
/*
* Now check each letter in turn, noting if we connect
*/
result = 1;
for (i=0; i<len; i++) {
int c = grid->pos[1+y+i][1+x];
if (c==word->word[i]) { /* connects with old word */
result = 2;
}else if (is_letter(c) ||
c==STOP_V || c==STOP_HV) { /* blocked */
return 0;
} /* BLANK or STOP_V are OK */
}
return result;
}
int Grid_fitword(Grid *grid, Word *word, Iter *iter)
{
switch (iter->state) {
case INIT:
break;
case HORIZCON:
return Grid_fitword_h(grid, word, iter) == 2;
case VERTCON:
return Grid_fitword_v(grid, word, iter) == 2;
case HORIZ:
return Grid_fitword_h(grid, word, iter) == 1;
case VERT:
return Grid_fitword_v(grid, word, iter) == 1;
case OMIT:
return word->optional;
}
return 0;
}
void Grid_addword(Grid *new, const Grid *old,
const Word *word, const Iter *iter)
{
int x=iter->x, y=iter->y, len=word->len;
int i;
char *c;
*new = *old;
switch (iter->state) {
case INIT:
break;
case HORIZCON:
new->score += CON_SCORE;
case HORIZ:
new->score += len;
new->pos[1+y][1+x-1] = STOP_HV; /* guard start */
new->pos[1+y][1+x+len] = STOP_HV; /* guard end */
for (i=0; i<len; i++) {
new->pos[1+y][1+x+i] = word->word[i]; /* insert char of word */
c = &new->pos[1+y-1][1+x+i]; /* guard top side */
if (*c==BLANK) *c = STOP_H;
else if (*c==STOP_V) *c = STOP_HV;
c = &new->pos[1+y+1][1+x+i]; /* guard bottom side */
if (*c==BLANK) *c = STOP_H;
else if (*c==STOP_V) *c = STOP_HV;
}
break;
case VERTCON:
new->score += CON_SCORE;
case VERT:
new->score += len;
new->pos[1+y-1][1+x] = STOP_HV; /* guard start */
new->pos[1+y+len][1+x] = STOP_HV; /* guard end */
for (i=0; i<len; i++) {
new->pos[1+y+i][1+x] = word->word[i]; /* insert char of word */
c = &new->pos[1+y+i][1+x-1]; /* guard left side */
if (*c==BLANK) *c = STOP_V;
else if (*c==STOP_H) *c = STOP_HV;
c = &new->pos[1+y+i][1+x+1]; /* guard right side */
if (*c==BLANK) *c = STOP_V;
else if (*c==STOP_H) *c = STOP_HV;
}
break;
case OMIT:
break;
}
}
Grid grids[MAXWORD+1];
Word words[MAXWORD+1];
Iter iters[MAXWORD+1];
void findall(void (*use)(Grid *))
{
Grid *grid = grids;
Word *word = words;
Iter *iter = iters;
enum {CALL,REDO,EXIT,FAIL} state = CALL;
while (1) {
switch (state) {
case CALL:
if (word->len == 0) {
state = EXIT;
break;
}
Iter_Iter(iter, grid, word);
case REDO:
if (Iter_next(iter)) {
if (Grid_fitword(grid, word, iter)) {
Grid_addword(grid+1, grid, word, iter);
grid++;
word++;
iter++;
state = CALL;
}else{
state = REDO;
}
}else{
state = FAIL;
}
break;
case FAIL:
if (grid == grids) return;
grid--;
word--;
iter--;
state = REDO;
break;
case EXIT:
use(grid); /* call function passed */
state = FAIL;
break;
}
}
}
#define NBMAX 10
#define NBTYPE Grid
#define NBBETTER(a,b) ((a)->score > (b)->score)
#include "nbest.h"
void print_add(Grid *g)
{
Grid_print(g);
nb_add(g);
}
int main()
{
int i;
#if 1
Word_Word(&words[0], "?colin");
Word_Word(&words[1], "?trudy");
Word_Word(&words[2], "?stuart");
Word_Word(&words[3], "?helen");
Word_Word(&words[4], "?gordon");
Word_Word(&words[5], ""); /* empty to terminate */
Grid_Grid(&grids[0], 8, 6);
#elif 0
Word_Word(&words[0], "?pig");
Word_Word(&words[1], "?dog");
Word_Word(&words[2], "?cow");
Word_Word(&words[3], ""); /* empty to terminate */
Grid_Grid(&grids[0], 4, 3);
#endif
findall(nb_add);
nb_sort();
for (i=0; i<nb_count; i++) {
Grid_print(nb_sorted[i]);
}
return 0;
}