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

边栏推荐
猜你喜欢
![[C language] explain the initial and advanced levels of the pointer and points for attention (1)](/img/61/1619bd2e959bae1b769963f66bab4e.png)
[C language] explain the initial and advanced levels of the pointer and points for attention (1)

蜻蜓低代码安全工具平台开发之路

c语言入门--数组

Jenkins Pipeline 应用与实践

06_栈和队列转换

Application and practice of Jenkins pipeline

Solve the problem that El radio group cannot be edited after echo

LeetCode - 搜索二维矩阵

How does CTO help the business?

19_Redis_宕机后手动配置主机
随机推荐
LeetCode 2310. 个位数字为 K 的整数之和
Introduction to mathjax (web display of mathematical formulas, vector)
Tidb data migration scenario overview
TiDB 软件和硬件环境建议配置
记一次面试
LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
forEach的错误用法,你都学废了吗
14_Redis_乐观锁
TiDB混合部署拓扑
Practical debugging skills
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
MFC console printing, pop-up dialog box
Leetcode - Search 2D matrix
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
【C语音】详解指针进阶和注意点(2)
How to solve the problem of database content output
Topology architecture of the minimum deployment of tidb cluster
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
TiDB数据迁移场景综述
Internet Explorer officially retired