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

边栏推荐
- Xilinx Vivado set *. svh as SystemVerilog Header
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- SQL 后计算的利器 SPL
- C language exercises - (array)
- 蜻蜓低代码安全工具平台开发之路
- C thread transfer parameters
- Ad20 cannot select the solution of component packaging in PCB editor
- 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
- taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
- LeetCode 2310. 个位数字为 K 的整数之和
猜你喜欢
![[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)](/img/7b/2c471c070a3faa981f70136603495a.jpg)
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)

Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment

Introduction to C language -- array

Socket and socket address

How does CTO help the business?

Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.

Dragonfly low code security tool platform development path

Have you learned the wrong usage of foreach

C code audit practice + pre knowledge

JMeter script parameterization
随机推荐
GeoServer offline map service construction and layer Publishing
牛客练习赛101
Huawei interview question: no palindrome string
About text selection in web pages and counting the length of selected text
qml 弹窗框架,可定制
C语言习题---(数组)
Wechat applet uses towxml to display formula
【题解】Educational Codeforces Round 82
TiDB 集群最小部署的拓扑架构
微信小程序使用towxml显示公式
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
YoloV6训练:训练自己数据集遇到的各种问题
Simple verification code generator for 51 single chip microcomputer experiment
c语言入门--数组
Niuke Practice 101
LeetCode 2310. The number of digits is the sum of integers of K
【C语音】详解指针进阶和注意点(2)
【NOI模拟赛】伊莉斯elis(贪心,模拟)
Why can't browsers read JSX?
Ad20 cannot select the solution of component packaging in PCB editor