当前位置:网站首页>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
边栏推荐
- Actual combat sharing of shutter screen acquisition
- Btrace- (bytecode) dynamic tracking tool
- kityformula-editor 配置字号和间距
- 为什么只会编程的程序员无法成为优秀的开发者?
- 3. Function pointers and pointer functions
- mathML转latex
- About text selection in web pages and counting the length of selected text
- 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
- Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
- Makefile 分隔文件名与后缀
猜你喜欢
可视化搭建页面工具的前世今生
报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
天猫商品详情接口(APP,H5端)
[noi Simulation Competition] scraping (dynamic planning)
C language exercises - (array)
forEach的错误用法,你都学废了吗
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
[email protected] : The platform “win32“ is incompatible with this module."/>
info [email protected] : The platform “win32“ is incompatible with this module.
Factal: Unsafe repository is owned by someone else Solution
kityformula-editor 配置字号和间距
随机推荐
C语言习题---(数组)
LeetCode 209. 长度最小的子数组
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
Implement a server with multi process concurrency
【NOI模拟赛】刮痧(动态规划)
[noi Simulation Competition] scraping (dynamic planning)
Introduction to C language -- array
LeetCode - 搜索二维矩阵
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
Fundamentals of software testing
taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
MFC A对话框调用B对话框函数并传参
[noi simulation] Elis (greedy, simulation)
Simple verification code generator for 51 single chip microcomputer experiment
数据库连接池和数据源
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
forEach的错误用法,你都学废了吗
Reuse and distribution