回溯算法n皇后问题_使用回溯算法的N Queen问题和解决方案

回溯算法n皇后问题

N-皇后问题 (N - Queen's problem)

The n – queen problem is the generalized problem of 8-queens or 4 – queen’s problem. Here, the n – queens are placed on a n * n chess board, which means that the chessboard has n rows and n columns and the n queens are placed on thus n * n chessboard such that no two queens are placed in the same row or in the same column or in same diagonal. So that, no two queens attack each other.

n皇后问题是8皇后或4 皇后问题的广义问题 。 在这里,将n –皇后放置在* n棋盘上,这意味着该棋board具有n行和n列,并且将n个皇后放置在n * n棋盘上,这样就不会在同一行中放置两个皇后。在同一列或同一对角线中。 因此,没有两个女王互相攻击。

Here, we suppose that the queen i is to be placed in row i. We can say that 1 queen is placed in the first row only but can have columns from 1, 2... n so that they satisfy all the explicit and implicit constraints.

在这里,我们假设将女王i放在第i行。 我们可以说1个皇后只放在第一行,但是可以有1、2 ... n个列,这样它们就可以满足所有显式和隐式约束。

All solutions to the n – queen’s problem can be therefore represented as n – tuples (x1, x2... xn) where xi is the column on which queen i is to be placed.

因此,n- 皇后问题的所有解决方案都可以表示为n-元组(x1,x2 ... xn),其中xi是放置女王i的列。

The explicit constraints using this formulation are Si = {1, 2, 3... n-1, n}, where 1 <= I <= n. Therefore, the solution space consists of nˆn n- tuples. Now, while considering the implicit constraints, that no two xi’s can be same i.e., two queens cannot be in the same row, same column, or in same diagonal. So, each xi should be different. Now by the above constraint, the solution is the permutations of the n – tuples (1, 2, 3, 4... n-1, n).

使用此公式的显式约束为Si = {1,2,3 ... n-1,n},其中1 <= I <= n。 因此,解空间由nˆn个元组组成。 现在,在考虑隐式约束时,没有两个xi可以相同,即两个皇后不能在同一行,同一列或同一对角线中。 因此,每个xi应该不同。 现在,根据上述约束,解决方案是n个元组的排列(1、2、3、4 ... n-1,n)。

算法 (Algorithm )

For all the solutions of the n - queen’s problem...

对于n-皇后问题的所有解...

1.	Algorithm N Queen (k, n)
2.	// Using backtracking, this procedure prints all possible placements of
3.	// n- queens on the n*n chess board so that they are non-attacking.
4.	{
5.	For I = 1 to n do
6.	{
7.	If Place (k, i) then
8.	{
9.	X[k] = I;
10.	If (k = n) then write (x[1: n ]) ;
11.	 Else N Queens (k+1, n);
12.	}
13.	}
14.	}

A NxN chessboard

翻译自: https://www.includehelp.com/algorithms/n-queens-problem-and-solution-using-backtracking-algorithm.aspx

回溯算法n皇后问题

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

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

相关文章

天气预报HTML代码

天气预报HTML代码<iframe height"200" src"http://weather.news.qq.com/inc/ss218.htm" frameborder"0" width"200" scrolling"no"></iframe><iframe width"420" scrolling"no" height&…

PHP源码安装及配置——以fastCGI的方式与httpd整合

在前一篇博文中介绍了LAMP的安装及配置。这里专门介绍PHP以fastCGI的方式与httpd整合的情况下&#xff0c;具体如何编译及配置。而且假定httpd、mysql均如前一篇博文介绍的安装配置完毕。php也采取一样版本的源码包php-5.6.1.tar.bz2。1. 编译安装php# tar xf php-5.6.1.tar.b…

超简单的分布式ID生成方案!美团开源框架介绍

目录阐述背景Leaf snowflake 模式介绍Leaf segment 模式介绍Leaf 改造支持 RPC阐述背景不吹嘘&#xff0c;不夸张&#xff0c;项目中用到 ID 生成的场景确实挺多。比如业务要做幂等的时候&#xff0c;如果没有合适的业务字段去做唯一标识&#xff0c;那就需要单独生成一个唯一的…

高质量的c源代码

现在自由软件及开源软件越来越流行&#xff0c;有大量的附带源程序的软件可以使用&#xff0c;这给我们学习软件开发提供了更多方便。但另一方面&#xff0c;也使得找到高质量的软件越来越不易。Landon Curt Noll 推荐 阅读David Ingalls Bell 的源代码。fefe 网站很推崇D.J.Be…

Java LinkedList公共布尔提供(对象o)方法(带示例)

LinkedList公共布尔提供(对象o)方法 (LinkedList public boolean offer(Object o) method) This method is available in package java.util.LinkedList.offer(Object o). 软件包java.util.LinkedList.offer(Object o)中提供了此方法。 This method is used to adds a specifie…

教你写Bug,常见的 OOM 异常分析

在《Java虚拟机规范》的规定里&#xff0c;除了程序计数器外&#xff0c;虚拟机内存的其他几个运行时区域都有发生 OutOfMemoryError 异常的可能。本篇主要包括如下 OOM 的介绍和示例&#xff1a;java.lang.StackOverflowErrorjava.lang.OutOfMemoryError: Java heap spacejava…

elf文件格式实例解析

试验环境&#xff1a;archlinux 速龙3000&#xff08;即x86兼容32位处理器&#xff09; 必须软件&#xff1a;gcc binutils 参考资料&#xff1a; System V application binary interface ELF Format &#xff08;mirror txt format ) Hello,world in less than 20 bytes…

离散数学关系的性质_关系和关系的性质| 离散数学

离散数学关系的性质笛卡尔积(A * B不等于B * A) (Cartesian product (A*B not equal to B*A)) Cartesian product denoted by * is a binary operator which is usually applied between sets. It is a set of ordered pairs where the first member of the pair belongs to th…

HTML,,,音乐,html embed用法

第一种&#xff1a;在页面代码中的<head></head>之间加入<bgsound src"音乐url" loop"-1"> 这段代码。 在这里要说的是&#xff0c;“loop”中的数值是音乐循环的次数&#xff0c;可设置为任意正整数&#xff0c;若设为“-1”的话&#…

少年开始学习c#编程,过路的大神请担待!

这应该真正算开始学习编程&#xff0c; 安装好了 linux&#xff0c; 开通了博客员的博客&#xff0c; 同时今天也需要把sublime安好&#xff0c; 安装MonoDevelop Codeblocks mysql-workbench 配置好相应的c#的环境&#xff0c; 在linux下安装了vmware&#xff0c; 也安装了一个…

池化技术到达有多牛?看了线程和线程池的对比吓我一跳!

这是我的第 82 篇原创文章作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;情商高的人是能洞察并照顾到身边所有人的情绪&#xff0c;而好的文章应该让所有人都能看懂。尼采曾…

小型elf Hello,World程序

参考链接&#xff1a;http://timelessname.com/elfbin/ 环境要求&#xff1a;linux gcc nasm hexcurse&#xff08;用来修改elf文件内容&#xff09; 先尝试用C语言写"Hello,World"程序(名为chello.c)&#xff1a; #include <stdio.h> int main(void) {printf…

spearman相关性_Spearman的相关性及其在机器学习中的意义

spearman相关性This article is about correlation and its implication in the machine learning. In my previous article, I have discussed Pearson’s correlation coefficient and later we have written a code to show the usefulness of finding Pearson’s correlati…

[java] 找出字符串中出现最多的字符和出现的次数

逛园子看到一童鞋做的华为上机题目&#xff0c;写来好长的代码&#xff0c;懒得看&#xff0c;感觉不可能这么难&#xff0c;于是动手敲了下。 import java.util.Scanner;public class StringTest {/*** param args*/public static void main(String[] args) {// TODO Auto-gen…

WIN7开启WIFI

开启windows 7的隐藏功能&#xff1a;虚拟WiFi和SoftAP&#xff08;即虚拟无线AP&#xff09;&#xff0c;就可以让电脑变成无线路由器&#xff0c;实现共享上网&#xff0c;节省网费和路由器购买费。主机设置如下&#xff1a; 【第一步】开始->在搜索栏中输入‘CMD’->右…

被问哭了,一位小姐姐的阿里面经!(附部分答案)

这篇文章是一位 女读者 &#xff08;加粗&#xff01;太难得&#xff09;的面试阿里的经历分享&#xff0c;虽然第二面就失败了&#xff0c;但是这样的经历对自己帮助应该还是很大的。下面的一些问题非常具有代表性&#xff0c;部分问题我简单做了修改&#xff08;有些问题表述…

Python程序不使用函数将字符大写

In this article, we will go for capitalizing the characters i.e. conversion from lowercase to uppercase without using any function. This article is based on the concept that how inbuilt function perform this task for us? 在本文中&#xff0c;我们将大写字符…

C语言,你真的弄懂了么?

程序&#xff08;来源 &#xff09;&#xff1a; #include <stdio.h>int main(void) {int x[4];printf("%p\n", (void*) (x));printf("%p\n", (void*) (x 1));printf("%p\n", (void*) (&x));printf("%p\n", (void*) (&…

Oracle自增序列

SQL Server中数据库可以自增字段&#xff0c;但是Oracle中没有这个选项&#xff0c;实际应用中我们可以使用序列(Sequence)实现想要的功能。创建Sequence语法如下&#xff1a;CREATE SEQUENCE SEQUENCE_NAME? START WITH 1 --从 1 开始INCREMENT BY 1 --每次增加 1NOMA…

阿里《Java开发手册》最新嵩山版发布!

《Java 开发手册》是阿里巴巴集团技术团队的集体智慧结晶和经验总结&#xff0c;经历了多次大规模一线实战的检验及不断完善&#xff0c;公开到业界后&#xff0c;众多社区开发者踊跃参与&#xff0c;共同打磨完善&#xff0c;系统化地整理成册&#xff0c;当前的版本是嵩山版。…