当前位置:网站首页>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;
}
边栏推荐
- N皇后问题的解决
- 关于网页中的文本选择以及统计选中文本长度
- How does CTO help the business?
- PHP method to get the index value of the array item with the largest key value in the array
- 数据库内容输出有问题怎么解决
- 學習使用php實現公曆農曆轉換的方法代碼
- LeetCode 2310. The number of digits is the sum of integers of K
- 数据分析思维分析方法和业务知识——业务指标
- C# richTextBox控制显示最大行数
- LeetCode 2320. 统计放置房子的方式数
猜你喜欢
Set set you don't know
02_线性表_顺序表
Dragonfly low code security tool platform development path
How does CTO help the business?
06_ Stack and queue conversion
Application and practice of Jenkins pipeline
04_ Stack
About text selection in web pages and counting the length of selected text
Base64 coding can be understood this way
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
随机推荐
Application of CDN in game field
CTO如何帮助业务?
07_哈希
TiDB 集群最小部署的拓扑架构
php获取数组中键值最大数组项的索引值的方法
06_栈和队列转换
Mavn builds nexus private server
表格响应式布局小技巧
15_Redis_Redis.conf详解
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
Huawei interview question: no palindrome string
[noi Simulation Competition] scraping (dynamic planning)
info [email protected] : The platform “win32“ is incompatible with this module.
C# richTextBox控制显示最大行数
C# 线程传参
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
C语言中的算术运算及相关练习题
Map介绍
MFC 定时器使用
Record an interview