题目例如以下: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)

    {

        };

        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>();

        )

            return result;

        )

        {

            elem.add(num[]);

            result.add(elem);

            return result;

        }

        int row;

        QueuePos.push();

        row;

        )

        {

            put_queue(QueuePos,row,QueueNum);

            //皇后放置完成

            ;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;

        QueuePos.pop();

        column=QueuePos.pop();

        column;

        )

        {

            

            if(column<QueueNum&&!QueuePos.contains(column))

            {

                QueuePos.push(column);

                return row;

            }

            else if(column==QueueNum)

            {

                row--;

                )

                    return row;

                column=QueuePos.pop();

                column;

            }else

            {

                column++;

            }

        }

        return row;

    }

    

    public static boolean put_queue(Stack<Integer> QueuePos,int row,int QueueNum)

    {

        int i,j,column; //i代表行。j代表列

        i;

        while(i<QueueNum)

        {

            ;j<QueueNum;j++)

                if(!QueuePos.contains(j))

                    break;

            if(j!=QueueNum)

            {

                QueuePos.push(j);

                i++;

                j;

            }

            else

            {

                i--;

                )

                {

                    column=QueuePos.pop();

                    QueuePos.push(column);

                }else

                    return false;

            }

        }

        return true;

    }

}

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

打印到类阵列的给定序列的所有排列的n皇后问题的更多相关文章

  1. [leetcode]47. Permutations全排列(给定序列有重复元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. 使用DataContractJsonSerializer类将类型实例序列化为JSON字符串和反序列化为实例对象 分类: JSON 前端 2014-11-10 10:20 97人阅读 评论(1) 收藏

    一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...

  3. PHP打印日志类

    PHP简单封装个打印日志类,方便查看日志: <?php /** * Created by PhpStorm. * User: zenkilan * Date: 2019/9/26 * Time: ...

  4. google test 打印派生类对象

    在Unison中使用google test时,发现EXPECT_EQ在fail时,不能打印Unison Test Language中定义的派生类的对象.于是写了个纯C++的示例,发现在只定义基类的op ...

  5. 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

    题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: 输 ...

  6. /.nav-tabs :是普通标签页 .nav-pills:胶囊式标签页 action ;默认的激活项,给<li>加默认显示的是哪个标签页内容 .nav是标签页的一个基类,给ul加 .nav-stacked: 垂直排列BootStrap

    <meta name="viewport" content="with=device-width, initial-scale=1, user-scalabe=no ...

  7. IO流(五)__文件的递归、Properties、打印流PrintStream与PrintWriter、序列流SequenceInputStream

    一.文件的遍历 1.需求:对指定目录进行所有的内容的列出(包含子目录的内容)-文件的深度遍历 思想:递归的思想,在递归的时候要记住递归的层次. public class FileTest { publ ...

  8. C# richTextBox封装的一个打印的类

    附件 http://files.cnblogs.com/xe2011/CSharpWinForm_richTextBoxPrintClass.rar 在窗体上一个Richtextbox 控件 和3个按 ...

  9. java利用反射打印出类的结构

    1 输入一个类名:java.lang.String将打印出String类定义的结构,例如: public final class java.lang.String { public java.lang ...

随机推荐

  1. Screwturn搭建企业内部wiki

    企业内部WIKI搭建 本文所使用的是Screwturn 基于asp.net webform和Sql server的. 仅仅要把本文资源下载下来,直接用IIS部署,然后更改web.config的conn ...

  2. HTTP/1.1

    HTTP真的很简单   原文:HTTP Made Really Easy因为我本身网络基础就很差,所以看到这篇文章一方面是学习网络知识,另一方面为了锻炼我蹩脚的英语水平,文中如有错误,欢迎浏览指正! ...

  3. Custom Media Player in WPF (Part 1)

    First of all I would like to welcome everyone to my new blog and wish you all a happy new year… Thro ...

  4. Array of Objects

    You should be comfortable with the content in the modules up to and including the module "Array ...

  5. 游戏碰撞OBB算法(java代码)

    业务需求      游戏2D型号有圆形和矩形,推断说白了就是碰撞检测 :      1.圆形跟圆形是否有相交      2.圆形跟矩形是否相交       3.矩形和矩形是否相交           ...

  6. c++ 计算程序运行时间

    转载 http://blog.csdn.net/trustbo/article/details/10582287 以前经常听人提起如何计算程序运行时间,给出一系列函数,当时没有注意,随便选了clock ...

  7. Code-Based Configuration (EF6 onwards)

    https://msdn.microsoft.com/en-us/data/jj680699#Using

  8. 采用SharePoint Designer将JavaScript而他们的网站页面集成的定义

    采用SharePoint Designer将JavaScript而他们的网站页面集成的定义 像JavaScript这种动态脚本语言可以给你的页面震撼效果.为了加盟JavaScript要定义自己的网站页 ...

  9. Appium0.18.x迁移到Appium1.x须知事项(灰常实用,解答了本人几个疑问)

    英文原版:https://github.com/appium/appium/blob/master/docs/en/advanced-concepts/migrating-to-1-0.md Migr ...

  10. decimal ? 含义

    例如:   decimal ? je = zfje;   意思是  将 JE赋值为  ZFJE ,  并且允许  JE 为 NULL 值 这时JE为引用类型