题目例如以下: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循环或递归。非常easy出现死循环。

而假设用随机生成排列,直到打印出全部排列。又easy超时。

        因此通过对问题的分析,发现能够将原问题映射成为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;
    }
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

转载于:https://www.cnblogs.com/zfyouxi/p/4828498.html

打印到类阵列的给定序列的所有排列的n皇后问题相关推荐

  1. 判断一个序列是否可由给定序列通过栈操作获得

    Java代码: public class LegalSequence {public static void main(String[] args) {// System.out.println(le ...

  2. Python Qt GUI设计:QPrinter打印图片类(基础篇—21)

    打印图像是图像处理软件中的一个常用功能,打印图像实际上是在QPaintDevice中画图,与平常在QWidget.QPixmap和Qlmage中画图一样,都是创建一个QPainter对象进行画图的,只 ...

  3. 每天一道LeetCode-----找出给定序列的所有子序列

    Subsets 原题链接Subsets 给定一个数组序列,找出所有子序列 深度优先扫一遍:) class Solution { public:vector<vector<int>&g ...

  4. 每天一道LeetCode-----找到给定序列中所有和为某个值的集合或集合个数,序列中可以有/无重复项,集合元素顺序不同算不同集合等

    Combination Sum 原题链接Combination Sum 给定一个无重复项的序列,找到所有和是target的集合,每个元素可以使用多次. 可以用深度优先(dfs),对于某个元素都有两种选 ...

  5. Python---寻找给定序列中相差最小的两个数字

    编写函数,寻找给定序列中相差最小的两个数字 def getTwoClosestElements(arr):#先进行排序,使得相邻元素最接近#相差最小的元素必然相邻seq = sorted(arr)#先 ...

  6. Java判断一个序列是否可由给定序列通过栈操作获得(ABCDEF)

    Java栈操作中的代码题目你是否已经掌握了呢?接下来上题目叭~~~ 判断一个序列是否可由给定序列通过栈操作获得(ABCDEF) 这是基础题噢 学不会就打你pp呜呜呜~ 还是那句话,原创禁止转载侵权必究 ...

  7. C++ 基础复习系列2(打印图形类(循环)经典问题类)

    C++ 基础复习系列--孙不坚1208 C++ 基础复习系列1(输入输出类.调用数学函数类) C++ 基础复习系列2(打印图形类(循环).经典问题类) C++ 基础复习系列3(递归算法){Fibona ...

  8. 查找Python中给定字符串的所有排列

    Python itertools Module Python itertools模块 "itertools" are an inbuilt module in Python whi ...

  9. scala类的序列化_Scala序列理解,通用类和内部类示例

    scala类的序列化 A sequence comprehension statement consists of a generator part which generates a list of ...

最新文章

  1. iOS 时间选择器封装(含三种模式)
  2. Kubernetes的十大使用技巧
  3. dataframe 去除重复
  4. 根据双眼的坐标对齐人脸Python实现
  5. 是否会回到起点.回忆只能是回忆
  6. 理清 WebSocket 和 HTTP 的关系
  7. python如何绘制直线_python绘制直线的方法
  8. Fiori note automatic delete deletion scenario
  9. 三星Galaxy S20:如何开启黑暗模式
  10. 【系统架构设计师】软考高级职称,一次通过,倾尽所有,2013年下半年系统架构设计师考试论文真题(论软件可靠性设计技术的应用)
  11. MTK 驱动开发(43)---GPS问题分类--MTK ALPS GPS的特殊知识
  12. SMBv3 BSoD 0day
  13. 【演讲实录+PPT下载】一网打尽AI年度热点,2017中国人工智能大会资料曝光(持续更新)...
  14. Excel中文转拼音【真正的完整版】 拼音 驼峰命名专用
  15. 物联卡代理商究竟如何选择?51物联卡告诉你正确答案
  16. linux执行arm文件,Linux安装FFMPEG转换amr为mp3格式
  17. 安装 emoji 字体
  18. win7桌面背景变黑且不能更换壁纸
  19. Zotero配合坚果云Web DAV同步那些坑
  20. vue照片查看器插件v-viewer

热门文章

  1. rabbitmq——镜像队列
  2. 关于Java里如何跳出一个多重循环
  3. 计算几何相关资料+题目推荐(不定期补充)
  4. 狸猫换太子:动态替换WinCE的原生驱动!
  5. tf.placeholder()
  6. windows下如何查看磁盘IO性能
  7. WORD中如何自动生成目录?
  8. python修改列表中字典内的值_python修改字典内key对应值的方法
  9. 反向输出dna序列_蛋白质序列反向(逆向)翻译成DNA序列-在线工具
  10. linux 安装wdcp控制面板