-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout_mxn.c
215 lines (206 loc) · 5.82 KB
/
layout_mxn.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
/*
* sku - analysis tool for Sudoku puzzles
* Copyright (C) 2005 Richard P. Curnow
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "sku.h"
const static char symbols_9[9] = {/*{{{*/
'1', '2', '3', '4', '5', '6', '7', '8', '9'
};
/*}}}*/
const static char symbols_16[16] = {/*{{{*/
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
/*}}}*/
const static char symbols_25[25] = {/*{{{*/
'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'J', 'K',
'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z'
};
/*}}}*/
void layout_MxN(int M, int N, int x_layout, struct layout *lay, int options) /*{{{*/
{
/* This function is REQUIRED to return the cells in raster scan order.
* Obviously this is necessary for the grid reader, but it's also
* required for fusing the sub-grids together when setting up a
* 5-gattai layout and similar horrors.
M is the no. of rows in a block
N is the no. of cols in a block
*/
int i, j, m, n;
int k;
int MN = M*N;
int NC, NG, NS;
char buffer[32];
lay->ns = NS = MN;
NG = 3*MN;
if (x_layout) NG += 2;
lay->ng = NG;
lay->nc = NC = MN*MN;
lay->prows = MN + (N-1);
lay->pcols = MN + (M-1);
if (MN <= 9) {
lay->symbols = symbols_9;
} else if (MN <= 16) {
lay->symbols = symbols_16;
} else if (MN == 25) {
lay->symbols = symbols_25;
} else {
fprintf(stderr, "No symbol table for MxN=%d\n", MN);
exit(1);
}
lay->cells = new_array(struct cell, lay->nc);
lay->groups = new_array(short, NG * NS);
lay->is_block = new_array(char, NG);
for (i=0; i<N; i++) {
for (j=0; j<M; j++) {
int row = M*i+j;
for (m=0; m<M; m++) {
for (n=0; n<N; n++) {
int col = N*m+n;
int block = M*i+m;
int ic = MN*row + col;
sprintf(buffer, "%c%d", 'A'+row, 1+col);
lay->cells[ic].name = strdup(buffer);
lay->cells[ic].group[0] = row;
lay->cells[ic].group[1] = MN + col;
lay->cells[ic].group[2] = 2*MN + block;
for (k=3; k<NDIM; k++) {
lay->cells[ic].group[k] = -1;
}
/* Put spacers every N rows/cols in the printout. */
lay->cells[ic].prow = row + (row / M);
lay->cells[ic].pcol = col + (col / N);
lay->cells[ic].rrow = row;
lay->cells[ic].rcol = col;
lay->groups[MN*(row) + col] = ic;
lay->groups[MN*(MN+col) + row] = ic;
lay->groups[MN*(2*MN+block) + N*j + n] = ic;
lay->cells[ic].is_overlap = 0;
}
}
}
}
lay->group_names = new_array(char *, NG);
for (i=0; i<MN; i++) {
char buffer[32];
sprintf(buffer, "row-%c", 'A' + i);
lay->group_names[i] = strdup(buffer);
lay->is_block[i] = 0;
sprintf(buffer, "col-%d", 1 + i);
lay->group_names[i+MN] = strdup(buffer);
lay->is_block[i+MN] = 0;
sprintf(buffer, "blk-%c%d", 'A' + M*(i/M), 1 + N*(i%M));
lay->group_names[i+2*MN] = strdup(buffer);
lay->is_block[i+2*MN] = 1;
}
if (x_layout) {
short *base0, *base1;
int ci0, ci1;
lay->group_names[NG-2] = strdup("diag-\\");
lay->group_names[NG-1] = strdup("diag-/");
base0 = lay->groups + NS*(NG-2);
base1 = lay->groups + NS*(NG-1);
ci0 = 0;
ci1 = NS - 1;
for (i=0; i<NS; i++) {
lay->cells[ci0].group[3] = NG - 2;
if (ci0 == ci1) {
/* Central square if MN is odd */
lay->cells[ci1].group[4] = NG - 1;
} else {
lay->cells[ci1].group[3] = NG - 1;
}
base0[i] = ci0;
base1[i] = ci1;
ci0 += (NS + 1);
ci1 += (NS - 1);
}
}
lay->n_thinlines = (MN - M) + (MN - N);
lay->n_mediumlines = (M + 1) + (N + 1) - 4;
lay->n_thicklines = 4;
lay->thinlines = new_array(struct dline, lay->n_thinlines);
lay->mediumlines = new_array(struct dline, lay->n_mediumlines);
lay->thicklines = new_array(struct dline, lay->n_thicklines);
j = m = n = 0;
for (i=0; i<MN+1; i++) {
struct dline *d;
/* horizontals */
if ((i==0) || (i==MN)) {
d = lay->thicklines + j;
j++;
} else if ((i%M) == 0) {
d = lay->mediumlines + n;
n++;
} else {
d = lay->thinlines + m;
m++;
}
d->x0 = 0, d->x1 = MN;
d->y0 = d->y1 = i;
/* verticals */
if ((i==0) || (i==MN)) {
d = lay->thicklines + j;
j++;
} else if ((i%N) == 0) {
d = lay->mediumlines + n;
n++;
} else {
d = lay->thinlines + m;
m++;
}
d->x0 = d->x1 = i;
d->y0 = 0, d->y1 = MN;
}
find_symmetries(lay, options);
}
/*}}}*/
void free_layout_lite(struct layout *lay)/*{{{*/
{
free(lay->thinlines);
free(lay->mediumlines);
free(lay->thicklines);
free(lay->groups);
free(lay->group_names);
free(lay->cells);
if (lay->name) free(lay->name);
free(lay->is_block);
}
/*}}}*/
void free_layout(struct layout *lay)/*{{{*/
{
int i;
free(lay->thinlines);
free(lay->mediumlines);
free(lay->thicklines);
free(lay->groups);
for (i=0; i<lay->ng; i++) {
free(lay->group_names[i]);
}
free(lay->group_names);
for (i=0; i<lay->nc; i++) {
free(lay->cells[i].name);
}
free(lay->cells);
free(lay->name);
free(lay->is_block);
free(lay);
}
/*}}}*/