八皇后问题---回溯

package com.atguigu.recursion;/*** @创建人 wdl* @创建时间 2021/3/21* @描述*/
public class Queue8 {//先定义一个max表示共有多少个皇后int max=8;//定义数组array,保存皇后防止位置的结果,比如 arr[8] = {0 , 4, 7, 5, 2, 6, 1, 3}int[] array=new int[max];static int count=0;public static void main(String[] args) {//测试一把,8皇后是否正确Queue8 queue8 = new Queue8();queue8.check(0);System.out.println("一共有"+count+"解法");}//编写一个方法,放置第n个皇后//特别注意:check是每一次递归时,进入到check中都有for (int i = 0; i < max; i++),因此会有回溯private void check(int n){if(n==max){//n=8,其实8个皇后就已经放好了print();return;}//依次放入皇后,并判断是否冲突for (int i = 0; i < max; i++) {//先把当前这个皇后n,放到该行的第1列array[n]=i;//判断当前放置的第n个皇后到i列时,是否冲突if(judge(n)){//不冲突//接着放n+1个皇后,即开始递归check(n+1);}//如果冲突,就继续执行array[n]=i;即将第n个皇后,放置在本行的后移一个位置}}//查看当我们放置第n个皇后,就去检测该皇后是否和前面已经摆放的皇后冲突/**** @param n 表示第n个皇后* @return*/private boolean judge(int n){for (int i = 0; i < n; i++) {//说明//1.array[i]==array[n]表示判断 第n个皇后是否和前面的n-1个皇后在同一列//2.Math.abs(n-i)==Math.abs(array[n]-array[i])表示判断第n个皇后是否和第i皇后是否在同一斜线//3.判断是否在同一行,没有必要,n每次都在递增if (array[i]==array[n]||Math.abs(n-i)==Math.abs(array[n]-array[i])){return false;}}return true;}//写一个方法,可以将皇后摆放的位置输出private void print(){count++;for (int i = 0; i < array.length; i++) {System.out.print(array[i]+" ");}System.out.println();}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/326921.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

程序员的情人节礼物:当天微软开始Build 2017登记

2016 年 12 月&#xff0c;微软曾经宣布 Build 2017 大会将于 2017 年 5 月 10 日至 12 日在美国西雅图举行&#xff1b;时隔两个月&#xff0c;关于 Build 2017 大会的消息&#xff0c;终于有了新的进展。 2 月 10 日&#xff0c;微软在官方博客中宣布 Build 2017 大会将开启注…

jQuery的三种bind/One/Live/On事件绑定使用方法

转载自 jQuery的三种bind/One/Live/On事件绑定使用方法 jQuery是 一款优秀的JavaScript框架,在旧版里主要用bind()方法&#xff0c;在新版里又多了两种One(),Live()&#xff0c;下面介绍这几种方法的使用 本篇文章介绍了&#xff0c;关于jQuery新的事件绑定机制on()的使用技…

C#实现人脸识别【Users】

这个类主要就是model类&#xff0c;对应数据库中的表users: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Camtest {public class Users{//编号public long id { get; set; }//姓名public…

查找前端依赖 jquery css js 时间控件 不要用远程依赖 会变化的 card

BootCDN - Bootstrap 中文网开源项目免费 CDN 加速服务 找到你要的版本

使用VS Code从零开始开发调试.NET Core 1.1

使用VS Code 从零开始开发调试.NET Core 1.1。无需安装VS 2017 RC 即可开发调试.NET Core 1.1应用。 .NET Core 1.1 发布也有一段时间了&#xff0c;最大的改动是从 project.json 还原回了csproj 。 今天微软发布 .NET Core SDK 1.0 RC4 版本&#xff0c;离RTM版本也很近了。 对…

Linux 退出保存/不保存

先按ESC键 然后输入 :wq 保存并退出 :q! 不保存退出 你可以强制退出&#xff0c;或者先保存在退出 保存&#xff1a;w 强制退出 &#xff1a;q&#xff01; 无论是否退出 vi&#xff0c;均可保存所做的工作。按 ESC 键&#xff0c;确定 vi 是否处于命令模式。 操作 键入 保…

冒泡排序+推导过程

推导过程 代码实现 package com.atguigu.sort;import java.util.Arrays;/*** 创建人 wdl* 创建时间 2021/3/21* 描述*/ public class BubbleSort {public static void main(String[] args) {int arr[]{3,9,-1,10,-2};//为了容易理解&#xff0c;我们吧冒泡排序的演变过程给大家…

jQuery 基础教程 (五)之使用jQuery创建动画效果

一、jQuery 中的动画: 隐藏和显示 &#xff08;1&#xff09;hide() 在 HTML 文档中, 为一个元素调用 hide() 方法会将该元素的 display 样式改为 none. 代码功 能同 css(“display”, “none”); &#xff08;2&#xff09;show() 将元素的 display 样式改为先前的显示状 态…

人脸检测解析json的工具类face_test

这个类主要是解析json数据 using face; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Camtest {public class fac…

.Net Core 之 MSBuild 介绍

前言 关于 .NET Core 旧版本的 sdk 介绍可以参看我以前的 这篇 文章。 8 个小时前&#xff0c;.NET Core 项目组释放了 .NET Core 新一轮的 sdk 工具更新&#xff0c;即 RC4 版本 &#xff0c;这个版本也就是意味着基本功能已经确定了&#xff0c;下个版本应该就是RTM版了&…

mysql加索引快很多

3秒 变成 0.1秒 左连接的字段加索引 order by字段加索引 频繁使用的检索字段加索引

冒泡排序优化后

package com.atguigu.sort;import java.util.Arrays;/*** 创建人 wdl* 创建时间 2021/3/21* 描述*/ public class BubbleSort {public static void main(String[] args) {int arr[]{3,9,-1,10,20};//为了容易理解&#xff0c;我们吧冒泡排序的演变过程给大家展示//第一趟排序&a…

jQuery 基础教程 (四)之jQuery中的DOM操作

一、jQuery 中的 DOM 操作 &#xff08;1&#xff09;DOM(Document Object Model—文档对象模型)&#xff1a;一 种与浏览器, 平台, 语言无关的接口, 使用该接口可以 轻松地访问页面中所有的标准组件 &#xff08;2&#xff09;DOM 操作的分类: (A)DOM Core: DOM Core 并不专…

人脸检测的model类facemodel

这个类主要就是人脸检测用到的实体模型 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace face {public class facemodel{//年龄public string age { get; set; }//美丑public string beauty …

.Net大户的选择:Windows Container在携程的应用

数人云上海&深圳两地“ 容器之Mesos/K8S/Swarm三国演义”的嘉宾精彩实录第四弹&#xff01;小数已经被接连不断的干货搞晕了&#xff0c;沉浸技术的海洋好幸福~Windows container在国内的实践还比较少&#xff0c;携程作为.Net大户&#xff0c;率先进行了调研和实践应用&am…

BigDecimal保留两位小数,不足两位补0

// 四舍五入 BigDecimal value new BigDecimal(object.toString()).setScale(2,BigDecimal.ROUND_HALF_UP); // 不足两位小数补0 DecimalFormat decimalFormat new DecimalFormat("0.00#"); String strVal decimalFormat.format(value);

人脸检测源码facedetection

人脸检测源码&#xff1a; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; using System.W…

73. 矩阵置零---LeetCode---JAVA

代码实现 class Solution {public void setZeroes(int[][] matrix) {boolean[] xnew boolean[matrix.length];boolean[] ynew boolean[matrix[0].length];for(int i0;i<matrix.length;i){for(int j0;j<matrix[0].length;j){if(matrix[i][j]0){x[i]true;y[j]true; …

真正理解线程上下文类加载器(多案例分析)

转载自 真正理解线程上下文类加载器&#xff08;多案例分析&#xff09; 前置知识&#xff1a; java类加载器不完整分析 前言 此前我对线程上下文类加载器&#xff08;ThreadContextClassLoader&#xff0c;下文使用TCCL表示&#xff09;的理解仅仅局限于下面这段话&#x…