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

边栏推荐
猜你喜欢

Mavn builds nexus private server

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

CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E

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

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

为什么只会编程的程序员无法成为优秀的开发者?

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language

【无标题】LeetCode 2321. 拼接数组的最大分数

LeetCode 209. 长度最小的子数组

04_ Stack
随机推荐
How to conduct TPC-C test on tidb
XML配置文件
你不知道的Set集合
LeetCode - 搜索二维矩阵
C# richTextBox控制显示最大行数
How does CTO help the business?
N皇后问题的解决
03_線性錶_鏈錶
Why can't programmers who can only program become excellent developers?
SQL 后计算的利器 SPL
可视化搭建页面工具的前世今生
原则、语言、编译、解释
CDN 在游戏领域的应用
Mavn builds nexus private server
【题解】Educational Codeforces Round 82
HUSTPC2022
Tidb data migration tool overview
07_哈希
记一次面试
Practical debugging skills