N-Queens(N皇后问题) --Java版

问题: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..”]
]

思路:
使用回溯法,即从第一行开始按照规则放置,放置好后放置第二行、第三行。。。。若某一行没有一个位置满足要求,则返回到上一行,将位置后移。如此反复,当最后一行都有位置可以放置时则找出一个解,程序继续运行,寻找剩余的解;当第一行的位置已经超出范围,则说明没有更多的解了。

约束条件:
程序结束条件:第一行的位置超过n
回溯的条件:该行数据超过n
回溯需要注意的是:将该行位置置0

代码实现(JAVA):

import java.util.*;
public class Solution {
    public List<String[]> solveNQueens(int n) {

        List<String[]> solutionList = new ArrayList<String[]>();//存放结果

        if(n==1){         //如果只有一行,则单独处理
            String[] Solution = new String[n];
            Solution[0] = "Q";
            solutionList.add(Solution);
            return solutionList;
        }

        if(n < 4){   //当n等于2或者3时可知不存在解
            return solutionList;
        }

        int[] pos = new int[n];  //用来存放每一行皇后所在的位置
        int i = 0;

        while(pos[0]<n){  //当一行的位置没有超过范围时都可以继续回溯,求更多的解

            while(i<n && pos[0]<n){   //设置每行皇后所在位置
                while(isConflict(i,pos)&&pos[i]<n){ //isConflict()判断位置是否冲突
                    pos[i]++;//若位置冲突,则位置后移(即加1)
                }            
                if(pos[i]>=n && i>0){  //如果某一行数据超出范围,则返回上一行
                    pos[i]=0;  //需要注意的是将此行位置置0
                    i--;
                    pos[i]++;  //回溯后上一行位置需要加1
                }else{
                    i++;
                }            
            }
            i--;  //注意从上一个循环结束后i值为n,所以此处需要减1
            if(i == n-1 && pos[i] < n){  //找到一个解决方案后,需要存入List
                String[] Solution = new String[n];
                for(int k=0; k<n; k++){
                    StringBuilder newSolution = new StringBuilder();
                    for(int j=0; j< pos[k];j++){
                        newSolution.append(".");
                    }
                    newSolution.append("Q");
                    for(int j=pos[k]+1;j<n;j++){
                        newSolution.append(".");
                    }
                    Solution[k] = newSolution.toString();
                }
                solutionList.add(Solution);

                //找到一个解决方案后需要继续回溯,继续寻找更多的方案
                if(pos[i]<n){
                    pos[i]++;
                }
            }

        }
        return solutionList;
    }

    public static boolean isConflict(int k, int[] pos){  //判断位置是否符合要求
        for(int i = 0; i<k ;i++){
            if(pos[k] == pos[i] || Math.abs(pos[k] - pos[i]) == Math.abs(k - i)){
                return true;
            }
        }
        return false;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值