2 * Enumerate positions of the game Digit
4 * Martin Mares <mj@ucw.cz>, September 2011
13 #define SIZE (2*MATCHES+3)
15 typedef char conf[SIZE][SIZE];
17 static void print(conf c)
19 for (int i=0; i<SIZE; i++)
21 for (int j=0; j<SIZE; j++)
25 putchar((i%2) ? '|' : '-');
41 static void norm(conf from, conf to)
44 for (int i=0; i<SIZE; i++)
45 for (int j=0; j<SIZE; j++)
51 ASSERT(ii < SIZE && jj < SIZE);
53 int di = 2 - (ii & ~1U);
54 int dj = 2 - (jj & ~1U);
56 bzero(to, sizeof(conf));
57 for (int i=ii; i<SIZE; i++)
58 for (int j=jj; j<SIZE; j++)
63 ASSERT(fi >= 0 && fj >= 0 && fi < SIZE && fj < SIZE);
68 static void mirror(conf from, conf to)
70 for (int i=0; i<SIZE; i++)
71 for (int j=0; j<SIZE; j++)
72 to[i][j] = from[j][i];
75 static void rotate(conf from, conf to)
77 for (int i=0; i<SIZE; i++)
78 for (int j=0; j<SIZE; j++)
79 to[j][SIZE-1-i] = from[i][j];
82 static int rr(conf c, int i, int j)
84 if (i < 0 || i >= SIZE || j < 0 || j >= SIZE)
90 static int has_neighbor(conf c, int i, int j)
92 if ((i%2) ? (rr(c, i-2, j) || rr(c, i+2, j))
93 : (rr(c, i, j-2) || rr(c, i, j+2)))
95 if (rr(c, i-1, j-1) || rr(c, i-1, j+1) ||
96 rr(c, i+1, j-1) || rr(c, i+1, j+1))
110 #define MAXSTATES 1000
112 static struct state states[MAXSTATES];
115 static struct state *lookup_direct(conf c)
119 for (int i=0; i<wi; i++)
120 if (!memcmp(states[i].c, n, sizeof(conf)))
125 static struct state *lookup_rot(conf c)
128 memcpy(d[0], c, sizeof(conf));
129 for (int i=0; i<4; i++)
131 struct state *s = lookup_direct(d[i]);
135 rotate(d[i], d[i+1]);
140 static struct state *lookup(conf c)
142 struct state *s = lookup_rot(c);
148 return lookup_rot(d);
154 static void generate(void)
156 conf c = { [2][5] = 1 };
157 norm(c, states[0].c);
158 states[0].matches = 1;
164 struct state *s = &states[ri++];
166 memcpy(to, s->c, sizeof(conf));
167 printf("### State %d (%d matches) ###\n", s->id, s->matches);
169 if (s->matches < MATCHES)
171 for (int i=0; i<SIZE; i++)
172 for (int j=1-(i%2); j<SIZE; j+=2)
173 if (!to[i][j] && has_neighbor(to, i, j))
176 struct state *t = lookup(to);
181 t->matches = s->matches+1;
184 if (t->from != s->id)
186 printf("-> %d\n", t->id);
194 printf("Degree: %d in, %d out, from=%d\n\n", s->indegree, s->outdegree, s->from);