/* The rows are 1..5 the columns are 1..5 cells (1,1) (1,5) (5,1) (5,5) do not exist. */ #include #include static bool exists(int r, int c) { return (2 <= r && r <= 4 && 1 <= c && c <= 5) || (2 <= c && c <= 4 && 1 <= r && r <= 5); } static void set(int r, int c, int k) { int k1; /* Set row r, column c, to colour k */ printf("&x%d%d%d\n", r, c, k); /* The other colours are not possible for this cell */ for (k1 = 1; k1 <= 5; k1++) { if (k1 != k) { printf("&~x%d%d%d\n", r, c, k1); } } /* we _could_ note that this colour is not available for other cells in the same row, or column, or diagonal, but that should happen automatically. */ } int main(void) { int r, r1, c, c1, k, k1; char ch, oc; oc = '('; for (r = 1; r <= 5; r++) { for (c = 1; c <= 5; c++) { if (exists(r, c)) { /* This cell has one of five colours */ putchar(oc); ch = '('; for (k = 1; k <= 5; k++) { printf("%cx%d%d%d", ch, r, c, k); ch = '|'; } printf(")\n"); oc = '&'; /* It does not have two colours at the same time */ for (k = 1; k < 5; k++) { for (k1 = k+1; k1 <= 5; k1++) { printf("%c(~x%d%d%d|~x%d%d%d)\n", oc, r, c, k, r, c, k1); oc = '&'; } } } } } /* rows */ for (r = 1; r <= 5; r++) { if (exists(r, 1)) { /* This is a full row */ for (k = 1; k <= 5; k++) { /* This colour must appear in the row */ putchar('&'); ch = '('; for (c = 1; c <= 5; c++) { printf("%cx%d%d%d", ch, r, c, k); ch = '|'; } printf(")\n"); oc = '&'; } } for (k = 1; k <= 5; k++) { /* This colour must not appear twice in the row */ ch = '('; for (c = 1; c < 5; c++) { for (c1 = c+1; c1 <= 5; c1++) { if (exists(r, c) && exists(r, c1)) { printf("%c(~x%d%d%d|~x%d%d%d)\n", oc, r, c, k, r, c1, k); oc = '&'; } } } } } /* columns */ for (c = 1; c <= 5; c++) { if (exists(1, c)) { /* This is a full column */ for (k = 1; k <= 5; k++) { /* This colour must appear in the column */ putchar(oc); ch = '('; for (r = 1; r <= 5; r++) { printf("%cx%d%d%d", ch, r, c, k); ch = '|'; } printf(")\n"); oc = '&'; } } for (k = 1; k <= 5; k++) { /* This colour must not appear twice in the column */ for (r = 1; r < 5; r++) { for (r1 = r+1; r1 <= 5; r1++) { if (exists(r, c) && exists(r1, c)) { printf("%c(~x%d%d%d|~x%d%d%d)\n", oc, r, c, k, r1, c, k); oc = '&'; } } } } } /* downwards diagonals */ for (k = 1; k <= 5; k++) { for (r = 4; r >= 2; r--) { putchar(oc); ch = '('; for (r1 = r; r1 <= 5; r1++) { c1 = r1 - r + 1; printf("%c~x%d%d%d", ch, r1, c1, k); ch = '|'; } printf(")\n"); oc = '&'; } putchar(oc); ch = '('; for (r = 2; r <= 4; r++) { printf("%c~x%d%d%d", ch, r, r, k); ch = '|'; } printf(")\n"); oc = '&'; for (c = 2; c <= 4; c++) { putchar(oc); ch = '('; for (c1 = c; c1 <= 5; c1++) { r1 = c1 - c + 1; printf("%c~x%d%d%d", ch, r1, c1, k); ch = '|'; } printf(")\n"); oc = '&'; } } /* upwards diagonals */ for (k = 1; k <= 5; k++) { for (c = 2; c <= 4; c++) { putchar(oc); ch = '('; for (c1 = c; c1 >= 1; c1--) { r1 = c - c1 + 1; printf("%c~x%d%d%d", ch, r1, c1, k); ch = '|'; } printf(")\n"); oc = '&'; } putchar(oc); ch = '('; for (c = 4; c >= 2; c--) { printf("%c~x%d%d%d", ch, 6-c, c, k); ch = '|'; } printf(")\n"); oc = '&'; for (r = 2; r <= 4; r++) { putchar(oc); ch = '('; for (r1 = r; r1 <= 5; r1++) { c1 = r - r1 + 5; printf("%c~x%d%d%d", ch, r1, c1, k); ch = '|'; } printf(")\n"); oc = '&'; } } set(1, 4, 1); set(3, 4, 2); set(5, 2, 3); set(5, 4, 4); printf(");\n"); return 0; }