打印给定一个数组序列的所有的排列的类n皇后问题

题目如下: Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].


分析:如果只是求排列数很好算,但是要打印所有排列且不重复比较困难。如果单纯用for循环或递归,很容易出现死循环。而如果用随机生成排列,直到打印出所有排列,又容易超时。
         因此通过对问题的分析,发现可以将原问题映射成为n皇后问题。每一次排列映射为n皇后的一次成功摆放,打印所有的排列即打印所有摆放皇后的方法。但与n皇后问题不同的是,此问题皇后不能放在同行同列,却可以放在对角线。因此核心还是回溯法,代码如下:

import java.util.ArrayList;
import java.util.Stack;
public class Main {
    public static void main(String[] args)
    {
        int[] num={1};
        ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
        result=permute(num);
        System.out.print(result);
    }
    public static ArrayList<ArrayList<Integer>> permute(int[] num) {
        ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> elem=new ArrayList<Integer>();
        int QueueNum=num.length;
        Stack<Integer> QueuePos=new Stack<Integer>();
        if(num.length==0)
            return result;
        if(num.length==1)
        {
            elem.add(num[0]);
            result.add(elem);
            return result;
        }
        int row;
        QueuePos.push(0);
        row=0;
        while(row>=0)
        {
            put_queue(QueuePos,row,QueueNum);
            //皇后放置完毕
            for(int i=0;i<QueuePos.size();i++)
            {
                elem.add(num[QueuePos.get(i)]);
            }
            result.add(elem);
            elem=new ArrayList<Integer>();
            //寻找下一个方法
            row=find_next(QueuePos,QueueNum);
            //status=put_queue(QueuePos,row,QueueNum);
        }
        return result;
    }
    
    public static int find_next(Stack<Integer> QueuePos,int QueueNum)
    {
        int row,column;
        row=QueueNum-2;
        QueuePos.pop();
        column=QueuePos.pop();
        column=column+1;
        while(row>=0)
        {
            
            if(column<QueueNum&&!QueuePos.contains(column))
            {
                QueuePos.push(column);
                return row;
            }
            else if(column==QueueNum)
            {
                row--;
                if(row<0)
                    return row;
                column=QueuePos.pop();
                column=column+1;
            }else
            {
                column++;
            }
        }
        return row;
    }
    
    public static boolean put_queue(Stack<Integer> QueuePos,int row,int QueueNum)
    {
        int i,j,column; //i代表行,j代表列
        i=row+1;
        while(i<QueueNum)
        {
            for(j=0;j<QueueNum;j++)
                if(!QueuePos.contains(j))
                    break;
            if(j!=QueueNum)
            {
                QueuePos.push(j);
                i++;
                j=0;
            }
            else
            {
                i--;
                if(i>=0)
                {
                    column=QueuePos.pop();
                    QueuePos.push(column+1);
                }else
                    return false;
            }
        }
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值