当前位置:网站首页>N皇后问题的解决
N皇后问题的解决
2022-07-02 12:00:00 【MASJLE】
#include <stdio.h>
#include <malloc.h>
#include <math.h>
int place(int* paraSolution, int paraT){
int j;
for (j = 1; j < paraT; j ++){
if ((abs(paraT - j) == abs(paraSolution[j] - paraSolution[paraT])) || (paraSolution[j] == paraSolution[paraT]))
return 0;
}
return 1;
}
void backtracking(int* paraSolution, int paraN, int paraT){
int i;
if (paraT > paraN){
for (i = 1; i <= paraN; i ++)
printf("%d ", paraSolution[i]);
printf("\r\n");
}else{
for (i = 1; i <= paraN; i ++){
paraSolution[paraT] = i;
if (place(paraSolution, paraT))
backtracking(paraSolution, paraN, paraT + 1);
}
}
}
void nQueen(int paraN){
int i;
int* solution = (int*)malloc((paraN + 1) * sizeof(int));
for (i = 0; i <= paraN; i ++)
solution[i] = 0;
backtracking(solution, paraN, 1);
}
int main(){
printf("输入皇后个数:\r\n");
int n;
scanf("%d",&n);
nQueen(n);
return 0;
}
边栏推荐
- taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
- C语言中的printf函数和scanf函数
- CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
- LeetCode 209. Minimum length subarray
- php获取数组中键值最大数组项的索引值的方法
- Wechat applet uses towxml to display formula
- Base64 coding can be understood this way
- 871. 最低加油次数 : 简单优先队列(堆)贪心题
- LeetCode 2320. Count the number of ways to place the house
- 传感器数据怎么写入电脑数据库
猜你喜欢
Simple verification code generator for 51 single chip microcomputer experiment
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
Makefile 分隔文件名与后缀
IE 浏览器正式退休
解决el-radio-group 回显后不能编辑问题
Base64 编码原来还可以这么理解
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
C code audit practice + pre knowledge
表格响应式布局小技巧
Internet Explorer officially retired
随机推荐
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
实用调试技巧
LeetCode 209. Minimum length subarray
C语言中的printf函数和scanf函数
Advanced C language (realize simple address book)
STM32 standard firmware library function name (I)
Introduction to C language -- array
LeetCode 2310. 个位数字为 K 的整数之和
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
871. 最低加油次数 : 简单优先队列(堆)贪心题
taobao. logistics. dummy. Send (no logistics delivery processing) interface, Taobao store delivery API interface, Taobao order delivery interface, Taobao R2 interface, Taobao oau2.0 interface
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
可视化搭建页面工具的前世今生
Mavn 搭建 Nexus 私服
[QNX hypervisor 2.2 user manual]6.3 communication between guest and external
buuctf-pwn write-ups (7)
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
C语言中的算术运算及相关练习题