用了二维数组的蠢货在此,个人极其不推荐。

P1219 [USACO1.5] 八皇后 Checker Challenge

flashfear @ 2024-12-04 20:13:12

int n, cnt, step, book[14][14], ans[7];

void dispose(int i, int j, int flag) {
    for (int y = 1; y <= n; y++)
        book[i][y] += flag;
    for (int x = 1; x <= n; x++)
        book[x][j] += flag;
    int x = i, y = j;
    while (--x > 0 && ++y <= n)
        book[x][y] += flag;
    x = i, y = j;
    while (--x > 0 && --y > 0)
        book[x][y] += flag;
    x = i, y = j;
    while (++x <= n && ++y <= n)
        book[x][y] += flag;
    x = i, y = j;
    while (++x <= n && --y > 0)
        book[x][y] += flag;
    book[i][j] -= flag;
}

void dfs(int step) {
    if (step == n + 1) {
        if (cnt < 3) {
            for (int i = 1; i <= n; i++) {
                cout << ans[i] << ' ';
            }
            cout << '\n';
        }
        cnt++;
        return;
    }

    int i = step, j;
    for (j = 1; j <= n; j++) {
        if (book[i][j] == 0) {
            ans[step] = j;
            dispose(i, j, 1);
            dfs(step + 1);
            dispose(i, j, -1);
        }
    }
    return;
}

by ny_Dacong @ 2024-12-04 20:37:57

已阅


|