LeetCode-51. N-Queens (JAVA)(打印N皇后解集)

51. N-Queens

The n-queens puzzle is the problem of placingn queens on ann×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to then-queens puzzle.

Each solution contains a distinct board configuration of then-queens' placement, where'Q' and '.' both indicate a queen and an empty space respectively.

For example,
There exist two distinct solutions to the 4-queens puzzle:

N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行、同一列、同一斜线上的皇后都会自动攻击)。

LeetCode-52. N-Queens II (JAVA)(N皇后解集个数)

[
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]

一:

public List<List<String>> solveNQueens(int n) {
		char[][] board = new char[n][n];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				board[i][j] = '.';
		List<List<String>> res = new ArrayList<List<String>>();
		dfs(board, 0, res);
		return res;
	}

	private void dfs(char[][] board, int colIndex, List<List<String>> res) {
		if (colIndex == board.length) {
			res.add(construct(board));
			return;
		}
		// 按列进行放置,若到了第colIndex列,
		//那么第0~colIndex-1列已经是放置好的
		for (int i = 0; i < board.length; i++) {
			if (validate(board, i, colIndex)) {
				board[i][colIndex] = 'Q';
				dfs(board, colIndex + 1, res);
				board[i][colIndex] = '.';
			}
		}
	}

	// x == i 同一行
	// x + j == y + i (y -x == j - i,斜率1,在同一条直线上) 同一主斜行
	// x + y == i + j(x-i=-(y-j),斜率-1,在同一条直线上) 同一副斜行
	private boolean validate(char[][] board, int x, int y) {
		for (int i = 0; i < board.length; i++) {
			// 判断放置第j列的时候,是否与前面的冲突,
			// 不需要判断y == j(循环j<y),只是与前面的进行比较
			for (int j = 0; j < y; j++) {
				// same as if(board[i][j] == 'Q' 
				//&& (Math.abs(x - i) ==
				// Math.abs(y - j) || x == i))
				if (board[i][j] == 'Q' && 
						(x - y == i - j 
						|| x + y == i + j 
						|| x == i))
					return false;
			}
		}
		return true;
	}

	private List<String> construct(char[][] board) {
		List<String> res = new LinkedList<String>();
		for (int i = 0; i < board.length; i++) {
			String s = new String(board[i]);
			res.add(s);
		}
		return res;
	}

二:

一个N长的数组就可以解决 int[n],例如int[0]=1表示在Q放在第1行的第2列,int[2]=3表示在Q放在第3行的第4列。

public List<List<String>> solveNQueens(int n) {
		List<List<String>> res = new ArrayList<List<String>>();
		int[] queenList = new int[n]; // 第i个位置存放的数表示row行时,Q的列
		placeQueen(queenList, 0, n, res);// 在第0行放Q
		return res;
	}

	private void placeQueen(int[] queenList, int row, int n, 
			List<List<String>> res) {
		// 如果已经填满,就生成结果
		if (row == n) {
			ArrayList<String> list = new ArrayList<String>();
			for (int i = 0; i < n; i++) {
				String str = "";
				for (int col = 0; col < n; col++) {
					if (queenList[i] == col) {
						str += "Q";
					} else {
						str += ".";
					}
				}
				list.add(str);
			}
			res.add(list);
		}
		// 按照行进行放置
		// 循环每一列
		for (int col = 0; col < n; col++) {
			// 如果在该列放入Q不冲突的话
			if (isValid(queenList, row, col)) { 
				// 没有回溯,因为没有修改原结果集
				// 只是临时记录结果
				queenList[row] = col;
				placeQueen(queenList, row + 1, n, res);
			}
		}
	}

	private boolean isValid(int[] queenList, int row, int col) {
		for (int i = 0; i < row; i++) {
			// pos为列
			int pos = queenList[i];
			// 和新加入的Q处于同一列
			if (pos == col) {
				return false;
			}
			// 在新加入的Q的右对角线上
			if (pos + row - i == col) {
				return false;
			}
			// 在新加入的Q的左对角线上
			if (pos - row + i == col) {
				return false;
			}
		}
		return true;
	}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值