打印給定一個數組序列的所有的排列的類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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章