[LeetCode]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.."]
]

参考:LeetCode 题解

戴方勤 ([email protected])

https://github.com/soulmachine/leetcode

public class Solution {
	boolean[] column = null;
	boolean[] diag = null;
	boolean[] anti_diag = null;
	int[]C = null;
	String model = null;
    public List<String[]> solveNQueens(int n) {
        column = new boolean[n];
        diag = new boolean[2*n];
        anti_diag = new boolean[2*n];
        C = new int[n];//Q在第i行的第几列
        List<String []> res = new ArrayList<>();

        dfs(0,res,n);
        return res;

    }

    private void dfs(int row,List<String[]> res,int n){
    	if(row==n){
    		String [] str = new String[n];
    		for(int i=0;i<n;i++){
    			StringBuilder sb = new StringBuilder();
    			for(int j=0;j<n;j++){
    				if(C[i]==j){
    					sb.append("Q");
    				}else{
    					sb.append(".");
    				}
    			}
    			str[i] = sb.toString();
    		}
    		res.add(str);
    		return;
    	}

    	for(int j=0;j<n;j++){
    		if(!column[j]&&!anti_diag[row+j]&&!diag[n-row-1+j]){
    			C[row]=j;
    		}else{
    			continue;
    		}
    		column[j]=anti_diag[row+j]=diag[n-row-1+j]=true;
    		dfs(row+1,res,n);
    		column[j]=anti_diag[row+j]=diag[n-row-1+j]=false;
    	}
    }
}

时间: 2024-08-11 01:33:55

[LeetCode]N-Queens 八皇后问题扩展(经典深搜)的相关文章

HDU 2553 N皇后问题(递归深搜)

N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8525    Accepted Submission(s): 3802 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求

Python----递归------Eight Queens 八皇后问题

递归思想是算法编程中的重要思想. 作为初学者,对递归编程表示很蒙逼,每次遇到需要递归的问题,心里就有一万头草泥马飞过~~~~~~(此处略去一万头草泥马) 在B站看数据结构与算法的视频时,视频中给了两个非常典型的例子--<汉诺塔>和<八皇后问题>,就希望自己用Python实现一下这两个递归程序,其中汉诺塔问题比较简单,还是能够理解,这里就不讲了. <八皇后问题>:说要在一个棋盘上放置8个皇后,但是不能发生战争,皇后们都小心眼,都爱争风吃醋,如果有人和自己在一条线上(水平.

[Leetcode] n queens n皇后问题

The n-queens puzzle is the problem of placing n queens on an n×nchessboard 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 t

hdu 1455 sticks(经典深搜+剪枝技巧)

 题意:有一堆的木棒,长度不一,它们是有一些整齐的木棒截断而成的,求最小的木棒原始长度. 思路很简单深搜,但是直接深搜的话会tle,首先可以对木棒长度进行排序从大到小,优先使用长度长的木棒,加入当前长度不符合,考虑下一个木棒 其次如果长度为零的时候选择木棒失败,那么直接退出,实测加上这一剪枝就可以ac,这一剪枝可以帮助我们尽可能的在靠近树根处剪枝,所以优化效果很明显. 然后是如果这次选择的木棒长度和上次失败时的一样,那么剪枝. #include<cstdio> #include<cs

八皇后以及N皇后问题分析

八皇后是一个经典问题,在8*8的棋盘上放置8个皇后,每一行不能互相攻击. 因此 拓展出 N皇后问题. 下面慢慢了解解决这些问题的方法: 回溯法: 回溯算法也叫试探法,它是一种系统地搜索问题的解的方法. 回溯算法的基本思想是:从一条路往前走,能进则进,不能进则退回来,换一条路再试. 在现实中,有很多问题往往需要我们把其所有可能穷举出来,然后从中找出满足某种要求的可能或最优的情况,从而得到整个问题的解. 回溯算法就是解决这种问题的“通用算法”,有“万能算法”之称. N皇后问题在N增大时就是这样一个解

openjudge-NOI 2.5-1700 八皇后问题

题目链接:http://noi.openjudge.cn/ch0205/1700/ 题解: 经典深搜题目-- 1 #include<cstdio> 2 bool a[9][9]; 3 int num; 4 void print() 5 { 6 printf("No. %d\n",num); 7 for(int i=1;i<=8;i++) 8 { 9 for(int j=1;j<=8;j++) 10 { 11 printf("%d ",a[j]

LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚

本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会详细解读深度优先搜索和回溯算法,感兴趣的同学不容错过. 链接 Next Permutation 难度 Medium 描述 实现C++当中经典的库函数next permutation,即下一个排列.如果把数组当中的元素看成字典序的话,那下一个排列即是字典序比当前增加1的排列.如果已经是字典序最大的情况

【蓝桥杯】经典的八皇后问题

这个问题很经典,不清楚问题描述的可以百度一下,这里就不再赘述了,只列出我的具体做法. import java.util.ArrayList; import java.util.List; class Test8Queens { public static StringBuffer result = new StringBuffer(); public static List<Integer> list = new ArrayList<Integer>(); public stati

54. 八皇后问题[eight queens puzzle]

[本文链接] http://www.cnblogs.com/hellogiser/p/eight-queens-puzzle.html [题目] 在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后不得处在同一行.同一列或者同一对角斜线上.下图中的每个黑色格子表示一个皇后,这就是一种符合条件的摆放方法.请求出总共有多少种摆法. [分析] 之前博文28.字符串的排列[StringPermutation]介绍过字符串的全排列,八皇后问题也可以转换为全排列问题. 由于八个皇后的任意两个不