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) ...
随机推荐
- GLEW OpenGL Access violation when using glGenVertexArrays
http://stackoverflow.com/questions/20766864/glew-opengl-access-violation-when-using-glgenvertexarray ...
- vertical-align0 垂直对齐- 图片 兼容个浏览器
效果: 代码: <html> <head> <style type="text/css"> img.top {vertical-align:t ...
- .NET软件开发与常用工具清单(转)
http://www.cnblogs.com/smileberry/p/4047835.html
- oss cmd
osscmd是基于python 2.5.4(其他版本没有试过),用来操作OSS的,可使用命令行来上传和下载文件. 下载地址:http://storage.aliyun.com/leo/osscmd.t ...
- js的元素对象
元素对象(element对象) ** 要操作element对象,首先必须要获取到element, - 使用document里面相应的方法获取 ...
- 使用cachemanager做缓存(Session的缓存)
1.我在这里直接用 cachemanager.redis 往redis里面存储缓存数据2.步骤 1)下载CacheManager.Redis(包含了CacheManager.Core) 下载Stack ...
- C#多线程学习(一) 多线程的相关概念(转)
什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. 什么是线程?线程是程序中的一个执行流,每个线程都有自己的专有寄 ...
- dreamwaver的动态相关文件 工具栏搜索
dreamwaver是很好的编辑工具, 用过很多ide, 对php,js, css代码来说, dw确实是很好很方便的一个工具 php本身设置了很多的 预定义常量, 函数, 可以用来获取当前运行php的 ...
- What is the most common software of data mining? (整理中)
What is the most common software of data mining? 1 Orange? 2 Weka? 3 Apache mahout? 4 Rapidminer? 5 ...
- R获取股票数据
R中好几个Pkg都提供了股票数据的在线下载方法,如果非得在其中找出一个最好的,那么quantmod当之无愧!举一个例子,譬如下载沪市大盘数据,代码可以是: library(quantmod)SSE & ...