当前位置:网站首页>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
边栏推荐
- Li Chuang EDA learning notes 15: draw border or import border (DXF file)
- MFC console printing, pop-up dialog box
- Mavn 搭建 Nexus 私服
- 大顶堆、小顶堆与堆排序
- OpenCV调用USB摄像头的点滴
- 复用和分用
- C thread transfer parameters
- AtCoder Beginner Contest 254
- 一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
- 使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
猜你喜欢

Factal: Unsafe repository is owned by someone else Solution

Solve the problem that El radio group cannot be edited after echo

一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

Makefile separates file names and suffixes

可视化搭建页面工具的前世今生

Add vector formula in rich text editor (MathType for TinyMCE, visual addition)

蜻蜓低代码安全工具平台开发之路

Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting

mathjax 入门(web显示数学公式,矢量的)
随机推荐
Wechat applet uses towxml to display formula
YoloV6训练:训练自己数据集遇到的各种问题
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
Base64 编码原来还可以这么理解
报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
[noi simulation] Elis (greedy, simulation)
Kityformula editor configure font size and spacing
华为面试题: 没有回文串
解决el-radio-group 回显后不能编辑问题
Huawei interview question: no palindrome string
蜻蜓低代码安全工具平台开发之路
fatal: unsafe repository is owned by someone else 的解决方法
Btrace- (bytecode) dynamic tracking tool
[QNX hypervisor 2.2 user manual]6.3 communication between guest and external
C language exercises - (array)
jmeter脚本参数化
Obsidian installs third-party plug-ins - unable to load plug-ins
Large top heap, small top heap and heap sequencing
【题解】Educational Codeforces Round 82
Implement a server with multi process concurrency