9.9---n皇后问题(CC150)
思路:首先写一个检查能不能摆的函数。boolean checkValid(int[] columns,int row1, int column1);意思是row1行摆在column1列可不可以。
然后是place函数。第一个参数row表示现在摆第几行。第一行可以摆n次位置,然后往下也是8ci。也就是相当于8^8次检查。如果能摆了,往下一行,如果不能摆往后移一列。当row>8说明摆好了。那么计数器+1.
答案:
public class Solution{
public static void main(String[] args){
System.out.println(nQueens(8));
}
public static int nQueens(int n) {
// write code here
int[] columns = new int[n];
int[] a = new int[1];
placeQueens(n,0,columns,a);
return a[0];
}
public static void placeQueens(int n,int row, int[] columns,int[] a){
if(row == n){
a[0]++;
}else{
for(int col = 0; col < n; col++ ){
if(checkValid(columns,row,col)){
columns[row] = col;//放皇后
placeQueens(n,row + 1,columns,a);
}
}
}
}
public static boolean checkValid(int[] columns, int row1, int column1) {
for (int row2 = 0; row2 < row1; row2++) {
int column2 = columns[row2];
if (column1 == column2) {
return false;
}
int columnDistance = Math.abs(column2 - column1);
int rowDistance = row1 - row2; // row1 > row2, so no need to use absolute value
if (columnDistance == rowDistance) {
return false;
}
}
return true;
}
}
9.9---n皇后问题(CC150)的更多相关文章
- [CC150] 八皇后问题
Write an algorithm to print all ways of arranging eight queens on an 8*8 chess board so that none of ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 面试题目——《CC150》递归与动态规划
面试题9.1:有个小孩正在上楼梯,楼梯有n个台阶,小孩一次可以上1阶.2阶或者3阶.实现一个方法,计算小孩有多少种上楼梯的方式. 思路:第4个数是前三个数之和 注意:能不能使用递归,能不能建立一个很大 ...
- 递归实现n(经典的8皇后问题)皇后的问题
问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...
- 八皇后算法的另一种实现(c#版本)
八皇后: 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于 ...
- [LeetCode] N-Queens II N皇后问题之二
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [LeetCode] N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- N皇后问题—初级回溯
N皇后问题,最基础的回溯问题之一,题意简单N*N的正方形格子上放置N个皇后,任意两个皇后不能出现在同一条直线或者斜线上,求不同N对应的解. 提要:N>13时,数量庞大,初级回溯只能保证在N< ...
- 数据结构0103汉诺塔&八皇后
主要是从汉诺塔及八皇后问题体会递归算法. 汉诺塔: #include <stdio.h> void move(int n, char x,char y, char z){ if(1==n) ...
随机推荐
- Unity3D热更全书
http://www.cnblogs.com/crazylights/p/3897742.html
- nuget包管理器控制台下的powershell脚本介绍
http://personball.com/powershell/2016/07/15/powershell-tips 定制自己的powershell,减少重复工作 安装一系列自己的常用nuget包 ...
- Linq使用Group By 1
Linq使用Group By 1 1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述 ...
- 使用EntityFramework6.1的DbCommandInterceptor拦截生成的SQL语句
开始 EF6.1也出来不少日子了,6.1相比6.0有个很大的特点就是新增了System.Data.Entity.Infrastructure.Interception 命名空间,此命名空间下的对象可以 ...
- ElasticSearch之二——集群
ElasticSearch 集群 首先看下ElasticSearch(ES)的架构: 术语解释: cluster:代表一个集群,集群中有多个节点,其中有一个master节点,master通过选举自动产 ...
- Python之路【第二篇】:Python基础
参考链接:老师 BLOG : http://www.cnblogs.com/wupeiqi/articles/4906230.html 入门拾遗 一.作用域 只要变量在内存中就能被调用!但是(函数的栈 ...
- 使用 Elasticsearch ik分词实现同义词搜索(转)
1.首先需要安装好Elasticsearch 和elasticsearch-analysis-ik分词器 2.配置ik同义词 Elasticsearch 自带一个名为 synonym 的同义词 fil ...
- motto11
我们应该这样来提高自己表达能力:在和人交流的时候,以欣赏的态度接受对方的观点,如果不太同意对方的观点,不能说对方的观点不好,而应该说,你的想法(观点)很好,但我认为,xxxxxx这样做会更好些. 这样 ...
- java.lang.reflect.Constructor
java.lang.reflect.Constructor 一.Constructor类是什么 Constructor是一个类,位于java.lang.reflect包下. 在Java反射中 Cons ...
- HighCharts选项和参数详细配置查询表
概述:作为一款出色的交互图表制作工具,HighCharts有着全面的选项.参数等配置信息.为了帮助大家进一步掌握HighCharts,今天我们为大家整理了HighCharts的所有配置信息和说明,将其 ...