打印到类阵列的给定序列的所有排列的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循环或递归。非常easy出现死循环。
而假设用随机生成排列,直到打印出全部排列。又easy超时。
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皇后问题的更多相关文章
- [leetcode]47. Permutations全排列(给定序列有重复元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 使用DataContractJsonSerializer类将类型实例序列化为JSON字符串和反序列化为实例对象 分类: JSON 前端 2014-11-10 10:20 97人阅读 评论(1) 收藏
一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...
- PHP打印日志类
PHP简单封装个打印日志类,方便查看日志: <?php /** * Created by PhpStorm. * User: zenkilan * Date: 2019/9/26 * Time: ...
- google test 打印派生类对象
在Unison中使用google test时,发现EXPECT_EQ在fail时,不能打印Unison Test Language中定义的派生类的对象.于是写了个纯C++的示例,发现在只定义基类的op ...
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: 输 ...
- /.nav-tabs :是普通标签页 .nav-pills:胶囊式标签页 action ;默认的激活项,给<li>加默认显示的是哪个标签页内容 .nav是标签页的一个基类,给ul加 .nav-stacked: 垂直排列BootStrap
<meta name="viewport" content="with=device-width, initial-scale=1, user-scalabe=no ...
- IO流(五)__文件的递归、Properties、打印流PrintStream与PrintWriter、序列流SequenceInputStream
一.文件的遍历 1.需求:对指定目录进行所有的内容的列出(包含子目录的内容)-文件的深度遍历 思想:递归的思想,在递归的时候要记住递归的层次. public class FileTest { publ ...
- C# richTextBox封装的一个打印的类
附件 http://files.cnblogs.com/xe2011/CSharpWinForm_richTextBoxPrintClass.rar 在窗体上一个Richtextbox 控件 和3个按 ...
- java利用反射打印出类的结构
1 输入一个类名:java.lang.String将打印出String类定义的结构,例如: public final class java.lang.String { public java.lang ...
随机推荐
- php获取前一天,前一个月,前一年的时间
获取前一天的时间: $mytime= date("Y-m-d H:i:s", strtotime("-1 day")); 获取三天前的时间: $mytime= ...
- 努比亚 Z5 mini刷机包 omni4.4.2改动V4.0 自用版 精简 MIUI特效
ROM介绍: 第一版: 1.基于lwang适配的omni4.4.2第二版改动,少量精简改动 2.设置加入"自启项管理",体验更快.更顺滑 3.替换特效为XUI特效 4.改动host ...
- Tomcat中更改网站根目录和默认页的配置方法
1.tomcat原来的默认根目录是http://localhost:8080,如果想修改访问的根目录,可以这样: 找到tomcat的server.xml(在conf目录下),找到: <Host ...
- windows phone xaml文件中元素及属性(10)
原文:windows phone xaml文件中元素及属性(10) Textblock xaml文件和隐藏文件 在设计界面的时候我们可以通过xaml中进行设计,这种设计是所见即所得的,很是方便,由于x ...
- python学习笔记之四:条件,循环和其他语句
前面已经介绍过几种基本语句(print,import,赋值语句),下面我们来介绍条件语句,循环语句. 一. print和import的更多信息 1.1 使用逗号输出 A.打印多个表达式,用逗号隔开,会 ...
- 软件測试系统文章(文件夹&链接在此)
前言 我会在此账号上写一系列关于软件測试的文章,故在此置顶软件測试系列文章的文件夹和链接,以方便大家阅读! 文件夹 软件測试系列之入门篇(一) 软件測试系列之了解篇(二) 软件測试系列之黑白盒(三) ...
- hdu 4524 郑厂长系列故事——逃离迷宫 小水题
郑厂长系列故事——逃离迷宫 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- Android源代码同步脚本(增加设置线程参数)
#!/bin/sh #Filename: repo_sync.sh count= ret= ] do #输入参数1,用作同步的线程数 #如果什么参数都不输入,默认线程为4 #usage: ./repo ...
- 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS Memory Limit: 6 ...
- mySQL中删除unique key的语法 (删除某个字段的唯一性)
mySQL中删除unique key的语法 CREATE TABLE `good_booked` ( `auto_id` int(10) NOT NULL auto_increment, `goo ...