N 皇后问题 : Leetcode 51. N-Queens

参考博客:  

回溯法 之 八皇后问题




N 皇后问题  : Leetcode 51. N-Queens


迭代写法:

import java.util.ArrayList;
import java.util.List;

public class Solution{
	public static void main(String[] args){
		int num = 0;
		Solution s = new Solution();
		s.solveNQueens(4);
	}
	
	public List> solveNQueens(int n) {
		int[] queen = new int[n];
    	for(int i=0;i> res = new ArrayList>();
    	int i = 0;
    	while(i>=0){
    		while(queen[i] elem = new ArrayList();
    				for(int j=0;j<=n-1;j++){
    					int val = queen[j];
    					String s = "";
    					for(int h=0;h<=n-1;h++){
    						if(h!=val){
    							s += ".";
    						}else{
    							s += "Q";
    						}
    					}
    					elem.add(s);
    				}
    				res.add(elem);
    			}else if(isValid(queen,i,queen[i])){
    				i++;
    			}
    		}
    		//fail back
    		queen[i] = -1;
    		i--;
    	}
    	return res;
	}

	private boolean isValid(int[] queen, int k, int i) {
		// TODO Auto-generated method stub
		for(int j=0;j



递归解法:

import java.util.ArrayList;
import java.util.List;

public class Solution{
	public static void main(String[] args){
		int num = 0;
		Solution s = new Solution();
	}
	public int num;
	
	
	public List> solveNQueens(int n) {
		int[] queen = new int[n];
    	for(int i=0;i> res = new ArrayList>();
    	getQueenPuzzle(n,queen,0,res);
    	return res;
	}
	

	private void getQueenPuzzle(int n, int[] queen, int k,List> res) {
		// TODO Auto-generated method stub
		for(int i=0;i<=n-1;i++){
			queen[k]=i; // queen at (k,i)
			if(k==n-1&&isValid(queen,k,i)){
				List elem = new ArrayList();
				for(int j=0;j<=n-1;j++){
					int val = queen[j];
					String s = "";
					for(int h=0;h<=n-1;h++){
						if(h!=val){
							s += ".";
						}else{
							s += "Q";
						}
					}
					elem.add(s);
				}
				res.add(elem);
			}else if(isValid(queen,k,i)){
				getQueenPuzzle(n,queen,k+1,res);
			}
			// not valid for i, then i++;
		}
	}

	private boolean isValid(int[] queen, int k, int i) {
		// TODO Auto-generated method stub
		for(int j=0;j

52. N-Queens II


迭代写法:

import java.util.ArrayList;
import java.util.List;

public class Solution{
	public static void main(String[] args){
		int num = 0;
		Solution s = new Solution();
		s.solveNQueens(9);
	}
	
	public int num;

	public int totalNQueens(int n) { 
		num = 0;
		int[] queen = new int[n];
    	for(int i=0;i> res = new ArrayList>();
    	int i = 0;
    	while(i>=0){
    		while(queen[i]> solveNQueens(int n) {
		int[] queen = new int[n];
    	for(int i=0;i> res = new ArrayList>();
    	int i = 0;
    	while(i>=0){
    		while(queen[i] elem = new ArrayList();
    				for(int j=0;j<=n-1;j++){
    					int val = queen[j];
    					String s = "";
    					for(int h=0;h<=n-1;h++){
    						if(h!=val){
    							s += ".";
    						}else{
    							s += "Q";
    						}
    					}
    					elem.add(s);
    				}
    				res.add(elem);
    			}else if(isValid(queen,i,queen[i])){
    				i++;
    			}
    		}
    		//fail back
    		queen[i] = -1;
    		i--;
    	}
    	return res;
	}

	private boolean isValid(int[] queen, int k, int i) {
		// TODO Auto-generated method stub
		for(int j=0;j



递归解法:

import java.util.List;

public class Solution{
	public static void main(String[] args){
		int num = 0;
		Solution s = new Solution();
	}
	public int num;
	
	public int totalNQueens(int n) {
		num = 0;
    	int[] queen = new int[n];
    	for(int i=0;i




你可能感兴趣的:(算法)