打印到类阵列的给定序列的所有排列的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 ...
随机推荐
- javascript面向对象之闭包
javascript面向对象之闭包 学习javascript一段时间了,自己对闭包作出如下总结,如有某点不妥,请君指出,不胜感激! 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量 ...
- HDU1176_免费馅饼【号码塔】
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- iOS随机颜色
#import <UIKit/UIKit.h> @interface UIColor (RandomColor) +(UIColor *) randomColor; @end #impor ...
- gc overhead limit exceeded eclipse错误解决方式
在Eclipse打包的时候报错:gc overhead limit exceeded eclipse 原因是Eclipse默认配置内存太小须要更改安装Eclipse目录下的eclipse.ini文件. ...
- 1、Cocos2dx 3.0游戏开发三找一小块前言
尊重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27094663 前言 Cocos2d-x 是一个通用 ...
- Java 序列化Serializable详解(附详细例子)
Java 序列化Serializable详解(附详细例子) 1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization ...
- Socket规划(1)
socket定义 于Linux网络编程是通过socket进行的. 人们常说的socket是一种特殊的I/O接口,他也是一个文件描写叙述符.socket是一种经常使用的进程之间通信机制,通过它不仅能实现 ...
- Sublime Text 3 最性感的编辑历史
↑ ↑ ↑ ↑ ↑ 请参阅文件夹 ↑ ↑ ↑ ↑ ↑ 下载 / 装 windows / MAC OS 官网下载.双击安装,这个都会吧- linux linux下安装.一种办法是从官网下载 tar.bz ...
- iSwifting如何发送照片社区
登录iSwifting社区 1,首先点击"帖子": 2,点击"照片": 3.点击"选择文件上传" 4,上传后的照片: 5,点击上传的照片: ...
- Java反射探索研究(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankakay 摘要:本文详细深入讲解是Java中反射的机制,并介绍了如何通过反射来生成对象.调用函数.取得 ...