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

边栏推荐
猜你喜欢

21_Redis_浅析Redis缓存穿透和雪崩

Jenkins Pipeline 应用与实践

Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting

电脑怎么设置扬声器播放麦克风的声音

C语言习题---(数组)

07_哈希

17_Redis_Redis发布订阅

LeetCode 2310. The number of digits is the sum of integers of K

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

C language exercises - (array)
随机推荐
Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
How to test tidb with sysbench
Topology architecture of the minimum deployment of tidb cluster
14_Redis_乐观锁
List集合&UML图
HUSTPC2022
LeetCode 2310. 个位数字为 K 的整数之和
05_ queue
2021-2022學年編譯原理考試重點[華僑大學]
为什么只会编程的程序员无法成为优秀的开发者?
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
【NOI模拟赛】刮痧(动态规划)
表格响应式布局小技巧
Introduction to mathjax (web display of mathematical formulas, vector)
About text selection in web pages and counting the length of selected text
04_ Stack
Arithmetic operations and related exercises in C language
15_Redis_Redis.conf详解
如何对 TiDB 进行 TPC-C 测试
Map介绍