当前位置:网站首页>C语言实现N皇后问题
C语言实现N皇后问题
2022-07-02 11:53:00 【ALuoJi】
回溯算法实现N皇后问题
完整代码实现
#include<stdio.h>
#include<malloc.h>
#include<math.h>
#define true 1
#define false 0
int num=0;
int place(int* paraSolution,int paraT)
{
for(int j=1;j<paraT;j++)
{
if((abs(paraT-j)==abs(paraSolution[j]-paraSolution[paraT]))||(paraSolution[j]==paraSolution[paraT]))
{
return false;
}
}
return true;
}
void backtracking(int* paraSolution, int paraN, int paraT){
int i;
if (paraT > paraN){
for (i = 1; i <= paraN; i ++)
printf("%d ", paraSolution[i]);
num++;
printf("\r\n");
}else{
for (i = 1; i <= paraN; i ++){
paraSolution[paraT] = i;
if (place(paraSolution, paraT))
backtracking(paraSolution, paraN, paraT + 1);
}
}
}
void nQueue(int paraN)
{
int i;
int* solution=(int*)malloc(sizeof(int)*(paraN+1));
for(i=0;i<=paraN;i++)
solution[i]=0;
backtracking(solution,paraN,1);
}
int main()
{
nQueue(5);
printf("The num is %d",num);
return 0;
}
测试样例
1 3 5 2 4
1 4 2 5 3
2 4 1 3 5
2 5 3 1 4
3 1 4 2 5
3 5 2 4 1
4 1 3 5 2
4 2 5 3 1
5 2 4 1 3
5 3 1 4 2
The num is 10
边栏推荐
- What is erdma? Popular science cartoon illustration
- [QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
- info [email protected] : The platform “win32“ is incompatible with this module.
- Fabric. JS zoom canvas
- STM32 library function for GPIO initialization
- JMeter script parameterization
- MFC 定时器使用
- C#代码审计实战+前置知识
- LeetCode - 搜索二维矩阵
- 实现一个多进程并发的服务器
猜你喜欢
![[apipost] tutorial](/img/f9/717908a72720f152ad49034be64b35.png)
[apipost] tutorial

Advanced C language (realize simple address book)

There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
![[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)](/img/7b/2c471c070a3faa981f70136603495a.jpg)
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)

Fatal: unsafe repository is owned by someone else

Fabric. Usage of JS eraser (including recovery function)

jmeter脚本参数化

实现一个多进程并发的服务器

Reuse and distribution

Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
随机推荐
【NOI模拟赛】刮痧(动态规划)
Chapter 9: xshell free version installation
buuctf-pwn write-ups (7)
LeetCode_字符串_简单_412.Fizz Buzz
Database connection pool and data source
1. Editing weapon VIM
C# 线程传参
STM32标准固件库函数名(一)
Fabric.js 橡皮擦的用法(包含恢复功能)
Huawei interview question: no palindrome string
MQ tutorial | exchange (switch)
NLA natural language analysis makes data analysis more intelligent
obsidian安装第三方插件——无法加载插件
华为面试题: 没有回文串
taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
jmeter脚本参数化
info [email protected] : The platform “win32“ is incompatible with this module.
871. 最低加油次数 : 简单优先队列(堆)贪心题
threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
Li Chuang EDA learning notes 15: draw border or import border (DXF file)