题目例如以下: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. 黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception 依赖注入容器Uni ...

  2. Android涉及到的设计模式(转)

    1.适配器模式:ListView或GridView的Adapter 简介:不同的数据提供者使用一个适配器来向一个相同的客户提供服务. 2.建造者模式:AlertDialog.Builder 简介:可以 ...

  3. C++第11周(春)项目1 - 存储班长信息的学生类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目1 - 存储班长信息的学生类] clas ...

  4. libyuv编

     下载代码(墙): git clone http://git.chromium.org/external/libyuv.git 用 make 编译(linux.mingw.cygwin): mak ...

  5. easyui LinkButton

    http://www.zi-han.net/case/easyui/menu&button.html

  6. MVC @Html控件

    传统的Html元素不能和服务端数据进行绑定 HtmlHelper类提供了一系列的方法来生成Html元素 并可以实现与数据绑定在一起 然后生成Html Html.BeginForm(actionName ...

  7. 玩转html5(三)---智能表单(form),使排版更加方便

    <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...

  8. Ognl底层使用

    今天,在得知ognl采用.在这里和大家分享一下.我希望能帮助. package com.hcj.test; import java.util.ArrayList; import java.util.L ...

  9. php xss过滤

    XSS已知CSS (Cross Site Script) ,跨站点脚本攻击.它指的是恶意攻击者Web插入恶意网页html代码,当用户浏览网页.其中嵌入Web里面html代码运行,从而实现了一些人的攻击 ...

  10. linux下一个Oracle11g RAC建立(五岁以下儿童)

    linux下一个Oracle11g RAC建立(五岁以下儿童) 四.建立主机之间的信任关系(node1.node2) 建立节点之间oracle .grid 用户之间的信任(通过ssh 建立公钥和私钥) ...