当前位置:网站首页>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
边栏推荐
- MFC console printing, pop-up dialog box
- Wechat applet uses towxml to display formula
- C#代码审计实战+前置知识
- C RichTextBox controls the maximum number of lines displayed
- 大顶堆、小顶堆与堆排序
- 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
- Check password
- tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
- btrace-(字节码)动态跟踪工具
- Fabric.js 动态设置字号大小
猜你喜欢
Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
JMeter script parameterization
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
Tmall product details interface (APP, H5 end)
kityformula-editor 配置字号和间距
The use of TestNG, the testing framework (II): the use of TestNG XML
CTO如何帮助业务?
[email protected]: The platform “win32“ is incompatible with this module."/>
info [email protected]: The platform “win32“ is incompatible with this module.
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
Large top heap, small top heap and heap sequencing
随机推荐
Reuse and distribution
What is erdma? Popular science cartoon illustration
LeetCode 2320. 统计放置房子的方式数
Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
Uniapp automated test learning
buuctf-pwn write-ups (7)
1、编辑利器vim
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
华为面试题: 没有回文串
[QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
MFC console printing, pop-up dialog box
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
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
[QNX hypervisor 2.2 user manual]6.3 communication between guest and external
LeetCode_字符串_简单_412.Fizz Buzz
电脑怎么设置扬声器播放麦克风的声音
Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control
LeetCode 2310. 个位数字为 K 的整数之和