打印到类阵列的给定序列的所有排列的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 ...
随机推荐
- Locked ownable synchronizers(转)
public class DeadLock { public static void main(final String[] args) throws Exception { final Object ...
- 王立平--Unity综上所述控制
GUILayout Label 创建一个自己主动布局的标签. Box 创建一个自己主动布局的box. Button 创建一个单次button.当用户点击button会马上发生一些事件. Repea ...
- CentOS6.5 Nginx优化编译配置[续]
继续上文CentOS6.5 Nginx优化编译配置本文记录有关Nginx系统环境的一些细节设置,有关Nginx性能调整除了配置文件吻合服务器硬件之前就是关闭不必要的服务.磁盘操作.文件描述符.内核调整 ...
- 配置jndi服务,javax.naming.NamingException的四种情况
1.当jndi服务没有启动,或者jndi服务的属性没有设置正确,抛出如下异常: javax.naming.CommunicationException: Can't find SerialContex ...
- Google Protocol Buffers和java字符串处理控制
大多数的操作码被从夜晚复制.懒得敲. 直接在源代码和测试结果如下. serabuffer.proto档.使用下面的命令来生成java代码. protoc -I=./ --java_out=./ ser ...
- Eclipse正确导入第三方project
前言 昨晚,在不同的Android做出最终的在线测试时间,在其他平台上正常升级的提示突然报告出来"java.lang.NoClassDefFoundError"误.拉什adb lo ...
- Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
之前一直用mybatis+mybatis-spring-1.1.1,系统升级mybatis使用后 mybatis-spring-1.2.2, 再其他配置均为改动的情况下执行出错: Property ' ...
- HDU 4288 Coder (线段树)
Coder 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4288 题意:有三种类型的操作,(1)."add x",表示往集合里加入�数 ...
- 同TextView在不同的显示内容
首先,请原谅我不能命名文章.. . 我们不能准确地表达你说说什么什么,真正急着赶智商. 直接在地图上 如图所看到的显示的是两个textview 第一个实现的是,在同一个textview中给不同内容赋予 ...
- 使用 WPF 创建预加载控件
Introduction At the time when WPF applications do a very long process like getting response from a w ...