9.9递归和动态规划(九)——N皇后
/**
* 功能:打印八皇后在8*8棋盘上的各种摆法。当中每一个皇后都不同行、不同列,也不在对角线上。
* 这里的“对角线”指的是全部的对角线,不仅仅是平分整个棋盘的那两条对角线。
*/
static int GRID_SIZE=8; /**
* 思路:每一行仅仅能摆放一个皇后,因此不须要将棋盘存储为完整的8*8矩阵。仅仅需一维数组,当中columns[r]=c表示有个皇后位于r行c列。
* @param row
* @param columns
* @param results
*/
public static void placeQueen(int row,Integer[] columns,ArrayList<Integer[]> results){
if(row==GRID_SIZE){
/*Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object.
* The general intent is that, for any object x, the expression:
x.clone() != x will be true.
* and that the expression:
x.clone().getClass() == x.getClass() will be true.
* but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x) will be true, this is not an absolute requirement. */
results.add(columns.clone());
}else{
for(int col=0;col<GRID_SIZE;col++){
if(checkValid(columns,row,col)){
columns[row]=col;//摆放皇后
placeQueen(row+1, columns, results);
}
}
}
} /**
* 检查(row,column)能否够摆放皇后。方法:
* 检查有无其它皇后位于同一列或对角线。不必检查是否在同一行上,由于调用placeQueen时,一次仅仅会摆放一个皇后。 由此可知,这一行是空的。
* @param columns
* @param row
* @param column
* @return
*/
public static boolean checkValid(Integer[] columns,int row,int column){
for(int r=0;r<row;r++){
int c=columns[r];
/* 检查同一列是否有皇后 */
if(c==column)
return false; /* 检查对角线:
* 若两行的距离等于两列的距离。则表示两个皇后在同一对角线上。 */
int columnDistance=Math.abs(c-column);
int rowDistance=row-r;//row>r,不用取绝对值
if(columnDistance==rowDistance)
return false;
} return true;
}
9.9递归和动态规划(九)——N皇后的更多相关文章
- C语言数据结构----递归的应用(八皇后问题的具体流程)
本节主要讲八皇后问题的基本规则和递归回溯算法的实现以及具体的代码实现和代码分析. 转载请注明出处.http://write.blog.csdn.net/postedit/10813257 一.八皇后问 ...
- 70. Climbing Stairs【leetcode】递归,动态规划,java,算法
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 算法 递归 迭代 动态规划 斐波那契数列 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 面试题目——《CC150》递归与动态规划
面试题9.1:有个小孩正在上楼梯,楼梯有n个台阶,小孩一次可以上1阶.2阶或者3阶.实现一个方法,计算小孩有多少种上楼梯的方式. 思路:第4个数是前三个数之和 注意:能不能使用递归,能不能建立一个很大 ...
- C#递归、动态规划计算斐波那契数列
//递归 public static long recurFib(int num) { if (num < 2) ...
- python---通过递归和动态规划策略解决找零钱问题
也是常见套路. # coding = utf-8 def rec_mc(coin_value_list, change, know_results): min_coins = change if ch ...
- Idea 02.暴力递归与动态规划(1)
1,关键词解释 1.1 暴力递归: 1, 把问题转化为规模缩小了的同类问题的子问题 2, 有明确的不需要继续进行递归的条件(base case) 3, 有当得到了子问题的结果之后的决策过程 4, 不记 ...
- scramble-string——两个字符串经过树化并旋转后是否一致、递归、动态规划
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- OptimalSolution(1)--递归和动态规划(1)斐波那契系列问题的递归和动态规划
一.斐波那契数列 斐波那契数列就是:当n=0时,F(n)=0:当n=1时,F(n)=1:当n>1时,F(n) = F(n-1)+F(n-2). 根据斐波那契数列的定义,斐波那契数列为(从n=1开 ...
随机推荐
- android 反编译和代码解读
二 错误代码还原规则 if…else 语句: 反编译代码 if (paramBoolean) paramTextView.setTextColor(-16727809); while (true) { ...
- What are the differences between WebAPI and WebAPI 2
http://stackoverflow.com/questions/21298961/what-are-the-differences-between-webapi-and-webapi-2 Maj ...
- Struts2中Struts.xml的作用
struts.xml 为Struts 2的核心配置文件.struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result定义等.struts.xml中主要配置Stru ...
- rest_framework 认证功能
from django.views import View from rest_framework.views import APIView from rest_framework.authentic ...
- MVC获取当前Controller/Action名称
1.视图中获取: var actionName=ViewContext.RouteData.Values["action"].ToString().ToLower(); var c ...
- Bayes++ Library入门学习之熟悉namespace
Bayes++是一个开源的C++类库.这些类表示并实现了用于离散系统的贝叶斯滤波的各种数值算法.该库中的类提供测试和一致的数值方法,并且用层次明确的结构表明了各种滤波算法和系统模型类型. 接下来,我们 ...
- 51Nod 1010 只包含因子2 3 5的数(打表+二分)
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- day01-Python介绍,安装,idea
一. python 简介 Python,读作['paɪθɑn],翻译成汉语是蟒蛇的意思,Python 的 logo 也是两条缠绕在一起的蟒蛇的样子,然而 Python 语言和蟒蛇实际上并没有一毛钱关系 ...
- Git 内部原理 - (3) Git 引用 (4)包文件
Git 引用 我们可以借助类似于 git log 1a410e 这样的命令来浏览完整的提交历史,但为了能遍历那段历史从而找到所有相关对象,你仍须记住 1a410e 是最后一个提交. 我们需要一个文件来 ...
- 洛谷1034 NOIP2002 矩形覆盖
问题描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7). 这些点可以 ...