当前位置:网站首页>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语言习题---(数组)
- taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
- MFC CString to char*
- 传感器数据怎么写入电脑数据库
- 2021-2022学年编译原理考试重点[华侨大学]
- mathjax 入门(web显示数学公式,矢量的)
- ##51单片机实验之简易验证码发生器
- 数据库连接池和数据源
- Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
- 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
猜你喜欢

C#代码审计实战+前置知识

实用调试技巧

Leetcode - Search 2D matrix

Simple verification code generator for 51 single chip microcomputer experiment

Makefile separates file names and suffixes

报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

天猫商品详情接口(APP,H5端)

C code audit practice + pre knowledge

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

Socket and socket address
随机推荐
C language exercises - (array)
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
c语言入门--数组
复用和分用
LeetCode - 搜索二维矩阵
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
kityformula-editor 配置字号和间距
Tmall product details interface (APP, H5 end)
MFC CString to char*
LeetCode_字符串_简单_412.Fizz Buzz
mathML转latex
forEach的错误用法,你都学废了吗
Niuke Practice 101
socket(套接字)与socket地址
AtCoder Beginner Contest 254
CDN 在游戏领域的应用
求轮廓最大内接圆
Points clés de l'examen de principe de compilation pour l'année scolaire 2021 - 2022 [Université chinoise d'outre - mer]
kibana 基础操作
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器