当前位置:网站首页>N皇后问题的解决
N皇后问题的解决
2022-07-02 12:00: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("输入皇后个数:\r\n");
int n;
scanf("%d",&n);
nQueen(n);
return 0;
}

边栏推荐
- Why can't browsers read JSX?
- taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
- LeetCode 2320. Count the number of ways to place the house
- Internet Explorer officially retired
- Niuke Practice 101
- 关于网页中的文本选择以及统计选中文本长度
- Socket and socket address
- 2021-2022学年编译原理考试重点[华侨大学]
- 一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
- [untitled] leetcode 2321 Maximum score of concatenated array
猜你喜欢
[email protected]: The platform “win32“ is incompatible with this module."/>info [email protected]: The platform “win32“ is incompatible with this module.

The past and present lives of visual page building tools

C language exercises - (array)

C#代码审计实战+前置知识

Add vector formula in rich text editor (MathType for TinyMCE, visual addition)

Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.

Yolov6 training: various problems encountered in training your dataset

YoloV6训练:训练自己数据集遇到的各种问题

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

Implement a server with multi process concurrency
随机推荐
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
Advanced C language (realize simple address book)
【C语音】详解指针进阶和注意点(2)
【无标题】LeetCode 2321. 拼接数组的最大分数
Internet Explorer officially retired
Introduction to C language -- array
Makefile separates file names and suffixes
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
2021-2022学年编译原理考试重点[华侨大学]
表格响应式布局小技巧
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
btrace-(字节码)动态跟踪工具
如何对 TiDB 进行 TPC-C 测试
C # delay, start the timer in the thread, and obtain the system time
Simple verification code generator for 51 single chip microcomputer experiment
数据库内容输出有问题怎么解决
【apipost】使用教程
How does CTO help the business?
[c voice] explain the advanced pointer and points for attention (2)