#include #define SIZE 20 char veld[SIZE*SIZE]; #define ONBEKEND '?' #define VOL '*' #define LEEG ' ' #define LEN 11 char row[SIZE][LEN] = { { 3, 0 }, { 3, 2, 5, 3, 0 }, { 7, 1, 3, 6, 0 }, { 1, 2, 0 }, { 3, 3, 2, 2, 2, 0 }, { 8, 1, 1, 1, 3, 0 }, { 1, 1, 0 }, { 2, 3, 3, 1, 0 }, { 9, 1, 2, 0 }, { 2, 1, 1, 0 }, { 1, 2, 1, 0 }, { 11, 1, 1, 0, }, { 1, 1, 1, 1, 2, 2, 1, 0 }, { 2, 1, 1, 1, 1, 2, 1, 1, 0 }, { 1, 1, 1, 1, 1, 1, 1, 1, 0 }, { 9, 1, 1, 2, 0 }, { 1, 1, 4, 0 }, { 9, 1, 2, 0 }, { 2, 1, 2, 4, 1, 0 }, { 2, 1, 1, 1, 2, 1, 0 } }; char col[SIZE][LEN] = { { 1, 2, 1, 0 }, { 2, 2, 2, 1, 0 }, { 2, 2, 2, 5, 2, 0 }, { 2, 1, 1, 1, 1, 4, 0 }, { 1, 2, 2, 2, 2, 1, 1, 0 }, { 2, 2, 2, 1, 1, 1, 2, 0 }, { 2, 2, 2, 2, 2, 1, 0 }, { 1, 1, 1, 1, 1, 1, 0 }, { 3, 1, 2, 2, 1, 0 }, { 2, 1, 1, 1, 1, 1, 1, 0 }, { 3, 1, 2, 2, 2, 0 }, { 4, 3, 5, 4, 0 }, { 3, 1, 1, 1, 2, 0 }, { 2, 2, 1, 2, 0 }, { 1, 1, 9, 0 }, { 1, 1, 1, 1, 1, 0 }, { 2, 3, 1, 2, 0 }, { 2, 1, 1, 2, 1, 2, 0 }, { 2, 2, 1, 1, 1, 0 }, { 1, 2, 4, 1, 3, 0 }, }; char old_val[SIZE]; char new_val[SIZE]; bool full[SIZE]; bool empty[SIZE]; int modified; int found; void tryplace(int i, char *numbers) { if (numbers[0] == 0) { bool can_place = true; for (int j = i; j < SIZE; j++) { if (old_val[j] == VOL) { can_place = false; break; } new_val[j] = LEEG; } if (can_place) { for (int j = 0; j < SIZE; j++) if (new_val[j] == VOL) empty[j] = false; else full[j] = false; found++; } return; } int ib = i == 0 ? i : i+1; int ie = ib + numbers[0]-1; for (; ie < SIZE; ib++, ie++) { bool can_place = true; for (int j = i; j < ib; j++) if (old_val[j] == VOL) { can_place = false; break; } if (can_place) { for (int j = ib; j <= ie; j++) if (old_val[j] == LEEG) { can_place = false; break; } if (can_place) { for (int j = i; j < ib; j++) new_val[j] = LEEG; for (int j = ib; j <= ie; j++) new_val[j] = VOL; tryplace(ie+1, numbers + 1); } } } } void solve(char *rowcol, int nr, int pos, int off, char *numbers) { for (int i = 0; i < SIZE; i++) { old_val[i] = veld[pos + off * i]; full[i] = true; empty[i] = true; } found = 0; tryplace(0, numbers); if (found == 0) return; int mod = 0; for (int i = 0; i < SIZE; i++) { if ( full[i] && !empty[i] && veld[pos + off * i] == ONBEKEND) { veld[pos + off * i] = VOL; modified++; mod++; } if ( !full[i] && empty[i] && veld[pos + off * i] == ONBEKEND) { veld[pos + off * i] = LEEG; modified++; mod++; } } if (mod) { printf("%8s %2d: ", rowcol, nr); for (int j = 0; j < SIZE; j++) printf("%c", old_val[j]); printf(" -> "); for (int j = 0; j < SIZE; j++) printf("%c", veld[pos + off * j]); printf(" %d (%d)\n", mod, found); } } void main() { // testen invoer correct int som_row = 0; for (int i = 0; i < SIZE; i++) for (int j = 0; j < LEN; j++) if (row[i][j] == 0) break; else som_row += row[i][j]; int som_col = 0; for (int i = 0; i < SIZE; i++) for (int j = 0; j < LEN; j++) if (col[i][j] == 0) break; else som_col += col[i][j]; if (som_row != som_col) { printf("row (%d) and col (%d) differ\n", som_row, som_col); return 0; } for (int i = 0; i < SIZE*SIZE; i++) veld[i] = ONBEKEND; do { modified = 0; for (int r = 0; r < SIZE; r++) solve("rij", r+1, r*SIZE, 1, row[r]); for (int c = 0; c < SIZE; c++) solve("colom", c+1, c, SIZE, col[c]); printf("\nmodified = %d\n", modified); printf("--------------------\n"); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) printf("%c", veld[i*SIZE+j]); printf("\n"); } printf("--------------------\n"); } while (modified > 0); }