(Java)LeetCode-51. N-Queens

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

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

Each solution contains a distinct board configuration of the n-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:

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

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


著名的八皇后问题,早有耳闻,今日终得一见,难得难得。

最典型的回溯算法,逐行来找能放皇后的位置,若到最后没有皇后可以放,就回溯去掉前面的状态。

自己写完后,去看了别人的代码,都差不多,就是有一个地方和别人不太一样,就是如何判断一个位置是否能放一个皇后,别人的做法的是用一个数组存储前面已经放下的皇后的位置,然后再来判断,这样写起来比较简单,省地方,我是直接的找到这个点的上下左右几个方向上看有没有皇后。这个地方不太成熟,要向别人学习。不过还是贴上我自己写的代码,毕竟写了蛮久的。


代码如下:




	public List<List<String>> solveNQueens(int n) {
		List<List<String>> result = new ArrayList<List<String>>();
		List<String> line = new ArrayList<String>();
		char[][] cube = new char[n][n];
		for(int i = 0; i < cube.length; i++){
			for(int j = 0; j < cube[i].length; j++){
				cube[i][j] = '.';
			}
		}
		boolean[] flags = new boolean[n];
		solveNQueens(result, cube, flags, n, 0);
        return result;
    }

	private void solveNQueens(List<List<String>> result, char[][] cube,boolean[] flags,int n, int cnt) {
		// TODO Auto-generated method stub
		if(cnt == n){
			List<String> list = new ArrayList<String>();
			for(char[] ch : cube){
				list.add(new String(ch));
			}
			result.add(list);
			return ;
		}
		for(int i = 0; i < flags.length; i++){
			if(flags[i] == false){
				List<Integer> temp = aPosition(cube, i);
				if(temp.size() == 0){
					break;
				}
				for(int t : temp){
					cube[i][t] = 'Q';
					flags[i] = true;
					solveNQueens(result, cube, flags, n , cnt+1);
					cube[i][t] = '.';
					flags[i] = false;
				}
				break;
			}
		}
	}

	private List<Integer> aPosition(char[][] cube, int num) {
		// TODO Auto-generated method stub
		List<Integer> result = new ArrayList<Integer>();
		int n = cube.length;
		boolean flag = false;
		for(int i = 0; i < n; i++){			//the direction is "--"
			if(cube[num][i] == 'Q'){
				return result;
			}
		}
		
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){		//the direction is "|"
				if(cube[j][i] == 'Q'){
					flag = true;
					break;
				}
			}
			if(flag == false){
				if(num >= i){					// the direction is "\"
					int dif = num - i;
					for(int j = 0; j + dif <= n-1; j++){ 
						if(cube[j+dif][j] == 'Q'){
							flag = true;
							break;
						}
					}
				}else{
					int dif = i - num;
					for(int j = 0; j + dif <= n-1; j++){
						if(cube[j][j+dif] == 'Q'){
							flag = true;
							break;
						}
					}
				}
				if(flag == false){
					if(num + i <= n-1){				//the direction is "/"
						int sum = i + num;
						for(int j = 0; sum >= j; j++ ){
							if(cube[j][sum-j] == 'Q'){
								flag = true;
								break;
							}
						}
					}else{
						int sum = i + num;
						for(int j = n-1; sum-j <= n-1; j--){
							if(cube[sum-j][j] == 'Q'){
								flag = true;
								break;
							}
						}
					}
				}
			}
			if(flag == false){
				result.add(i);
			}else{
				flag = false;
			}
		}
		return result;
	}

	public static void main(String[] args){
    	Solution sol = new Solution();
    	System.out.println("start");
    	List<List<String>> re = sol.solveNQueens(4);
    	for(List<String> l : re){
    		System.out.println(l);
    	}
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值