当前位置:网站首页>Implementation of n queen in C language
Implementation of n queen in C language
2022-07-02 15:04:00 【ALuoJi】
Backtracking algorithm implementation N queens problem
Complete code implementation
#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;
}
The test sample
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
边栏推荐
- C#代码审计实战+前置知识
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
- CTO如何帮助业务?
- Printf function and scanf function in C language
- taobao. trade. Get (get some information of a single transaction), Taobao store order interface, Taobao oauth2.0 interface, Taobao R2 interface code docking and sharing
- Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
- 【C语言】详解指针的初阶和进阶以及注意点(1)
- [QNX hypervisor 2.2 user manual]6.3 communication between guest and external
- Xilinx Vivado set *.svh as SystemVerilog Header
- Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
猜你喜欢
Obsidian installs third-party plug-ins - unable to load plug-ins
MFC timer usage
【无标题】LeetCode 2321. 拼接数组的最大分数
About text selection in web pages and counting the length of selected text
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
How does CTO help the business?
C#代码审计实战+前置知识
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
btrace-(字节码)动态跟踪工具
随机推荐
Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
2021-2022学年编译原理考试重点[华侨大学]
kityformula-editor 配置字号和间距
qml 弹窗框架,可定制
mathjax 入门(web显示数学公式,矢量的)
【C语言】详解指针的初阶和进阶以及注意点(1)
fatal: unsafe repository is owned by someone else 的解决方法
C code audit practice + pre knowledge
LeetCode 209. 长度最小的子数组
GeoServer offline map service construction and layer Publishing
btrace-(字节码)动态跟踪工具
buuctf-pwn write-ups (7)
How does CTO help the business?
2. Const pointer
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
【apipost】使用教程
MFC CString 转 char*
forEach的错误用法,你都学废了吗
MFC 控制台打印,弹出对话框
2021-2022學年編譯原理考試重點[華僑大學]