当前位置:网站首页>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;
}

边栏推荐
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- Implement a server with multi process concurrency
- 为什么只会编程的程序员无法成为优秀的开发者?
- LeetCode - 搜索二维矩阵
- MFC 定时器使用
- IE 浏览器正式退休
- Btrace- (bytecode) dynamic tracking tool
- Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
- Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
- 可视化搭建页面工具的前世今生
猜你喜欢

Base64 编码原来还可以这么理解

kityformula-editor 配置字号和间距

Tmall product details interface (APP, H5 end)

Internet Explorer officially retired

Factal: Unsafe repository is owned by someone else Solution
![[c voice] explain the advanced pointer and points for attention (2)](/img/fb/515e25899bd9a2905ee63cb041934a.png)
[c voice] explain the advanced pointer and points for attention (2)

btrace-(字节码)动态跟踪工具

forEach的错误用法,你都学废了吗

Large top heap, small top heap and heap sequencing

C code audit practice + pre knowledge
随机推荐
MFC console printing, pop-up dialog box
info [email protected] : The platform “win32“ is incompatible with this module.
Arithmetic operations and related exercises in C language
buuctf-pwn write-ups (7)
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
GeoServer offline map service construction and layer Publishing
kityformula-editor 配置字号和间距
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
C RichTextBox controls the maximum number of lines displayed
Implement a server with multi process concurrency
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
Xilinx Vivado set *. svh as SystemVerilog Header
[untitled] leetcode 2321 Maximum score of concatenated array
TiDB 软件和硬件环境建议配置
LeetCode 2310. The number of digits is the sum of integers of K
Why can't browsers read JSX?
蜻蜓低代码安全工具平台开发之路
Implementation of n queen in C language
【C语音】详解指针进阶和注意点(2)
Kityformula editor configure font size and spacing