当前位置:网站首页>Solution of Queen n problem
Solution of Queen n problem
2022-07-02 15:14: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(" Enter the number of queens :\r\n");
int n;
scanf("%d",&n);
nQueen(n);
return 0;
}
边栏推荐
猜你喜欢
Learn the method code example of converting timestamp to uppercase date using PHP
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
C语言习题---(数组)
forEach的错误用法,你都学废了吗
LeetCode 2320. Count the number of ways to place the house
【C语音】详解指针进阶和注意点(2)
How does CTO help the business?
Have you learned the wrong usage of foreach
Dragonfly low code security tool platform development path
LeetCode 209. Minimum length subarray
随机推荐
AtCoder Beginner Contest 254
Tidb cross data center deployment topology
【无标题】LeetCode 2321. 拼接数组的最大分数
info [email protected] : The platform “win32“ is incompatible with this module.
【题解】Educational Codeforces Round 82
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
Tidb hybrid deployment topology
【NOI模拟赛】伊莉斯elis(贪心,模拟)
Mfc a dialog calls B dialog function and passes parameters
C code audit practice + pre knowledge
Niuke Practice 101
实用调试技巧
蜻蜓低代码安全工具平台开发之路
Dragonfly low code security tool platform development path
18_Redis_Redis主从复制&&集群搭建
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
Kibana basic operation
How to conduct TPC-C test on tidb
C#延时、在线程中开启定时器、获取系统时间
关于网页中的文本选择以及统计选中文本长度