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
| #include<cstdio> #define N 8
int ans[N] = {}, kind, must; bool mx[N]; bool s1[15], s2[15]; void solve(int row, int col); void backtracking(int col, int count); int main() { int Case; scanf("%d", &Case); while (Case--) { int x, y; scanf("%d%d", &x, &y); puts("SOLN COLUMN"); puts(" # 1 2 3 4 5 6 7 8\n");
for (int i = 0; i < N; i++) { mx[i] = false; ans[i] = -1; } for (int i = 0; i < 15; i++) s1[i] = s2[i] = false;
solve(x, y);
if (Case) putchar('\n'); } return 0; } void solve(int row, int col) { kind = 0; row--; col--; mx[row] = s1[row + col] = s2[col - row + 7] = true; ans[col] = row; must = col; backtracking(0, 0); } void backtracking(int col, int count) { if (count == N) { kind++; printf("%2d ", kind); for (int i = 0; i < N; i++) printf(" %d", ans[i] + 1); putchar('\n'); return; } else if (col == must) backtracking(col + 1, count + 1); else { for (int x = 0; x < N; x++) { int d1 = x + col, d2 = col - x + 7; if (!mx[x] && !s1[d1] && !s2[d2]) { mx[x] = s1[d1] = s2[d2] = true;
ans[col] = x; backtracking(col + 1, count + 1);
mx[x] = s1[d1] = s2[d2] = false; } } } }
|