【leetcode】N-Queens II N皇后II

这道题跟我在博客另外一篇的代码差不多 就多一个方法 可以看看我另外一篇文章  https://blog.csdn.net/kevin_nan/article/details/87778923 另外一篇有详细的介绍 这一篇是另外一篇的改造 改成统计数量

class Solution {
 
	public int sum = 0;

	public int totalNQueens(int n) {
		int[] arr = new int[n];
		Nqueen(0, n, arr);
		return sum;
	}

	public void Nqueen(int i, int n, int arr[]) {
		if (i == n) {
			sum++;
		} 
                else {
			for (int temp = 0; temp < n; temp++) {
				arr[i] = temp;
				boolean flag = true;
				for (int x = 0; x < i; x++) {
					if (arr[i] == arr[x] || (Math.abs(arr[i] - arr[x]) == Math.abs(x - i))) {
						flag = false;
						arr[i] = 0;
						break;
					}
				}
				if (flag == true) {
					Nqueen(i + 1, n, arr);
				}
			}
		}
	}
}

别人的解法:差不多 

class Solution {

    /**
     * 记录某列是否已有皇后摆放
     */
    private boolean col[];

    /**
     * 记录某条正对角线(左上右下)是否已有皇后摆放(某条对角线对应的摆放位置为 x - y + n - 1)
     */
    private boolean dia1[];

    /**
     * 记录某条斜对角线(左下右上)是否已有皇后摆放(某条对角线对应的摆放位置为 x + y)
     */
    private boolean dia2[];

    public int totalNQueens(int n) {
        // 依然可以使用 51 号问题的解决思路,但问题是有没有更好的方法
        col = new boolean[n];
        dia1 = new boolean[2 * n - 1];
        dia2 = new boolean[2 * n - 1];
        return putQueen(n, 0);
    }

    /**
     * 递归回溯方式摆放皇后
     *
     * @param n     待摆放皇后个数
     * @param index 已摆放皇后个数
     */
    private int putQueen(int n, int index) {
        int res = 0;
        if (index == n) {
            return 1;
        }
        // 表示在 index 行的第 i 列尝试摆放皇后
        for (int i = 0; i < n; i++) {
            if (!col[i] && !dia1[i - index + n - 1] && !dia2[i + index]) {
                // 递归
                col[i] = true;
                dia1[i - index + n - 1] = true;
                dia2[i + index] = true;
                res += putQueen(n, index + 1);
                // 回溯
                col[i] = false;
                dia1[i - index + n - 1] = false;
                dia2[i + index] = false;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int n = new Solution().totalNQueens(8);
        System.out.println(n);
    }
}
private int bt(boolean[] c, boolean[] f, boolean[] b, int row, int n) {
    if (row == n) return 1;
    int ans = 0;
    for (int col = 0; col < n; ++col) {
        int i = col + row, j = col - row + n;
        if (c[col] || f[i] || b[j]) continue;
        c[col] = f[i] = b[j] = true;
        ans += bt(c, f, b, row + 1, n);
        c[col] = f[i] = b[j] = false;
    }
    return ans;
}

public int totalNQueens(int n) {
    return bt(new boolean[n], new boolean[2 * n], new boolean[2 * n], 0, n);
}
public int totalNQueens(int n) {
    int ans = 0;
    int[] queens = new int[n];
    boolean[] c = new boolean[n + 1];
    boolean[] f = new boolean[2 * n];
    boolean[] b = new boolean[2 * n];
    c[n] = true; //dummy boundary
    int col = 0, row = 0;
    while (true) {
        if (c[col] || f[col + row] || b[col - row + n]) {
            if (row == n || col == n) {
                if (row == 0) return ans;
                if (row == n) ans++;
                col = queens[--row];
                c[col] = f[col + row] = b[col - row + n] = false;
            }
            col++;
        } else {
            c[col] = f[col + row] = b[col - row + n] = true;
            queens[row++] = col;
            col = 0;
        }
    }
}
// Runtime: 4ms

猜你喜欢

转载自blog.csdn.net/kevin_nan/article/details/87787717