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

边栏推荐
- 用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
- 学习使用php将时间戳转换为大写日期的方法代码示例
- 【无标题】LeetCode 2321. 拼接数组的最大分数
- TiDB 环境与系统配置检查
- qml 弹窗框架,可定制
- Tmall product details interface (APP, H5 end)
- Find the maximum inscribed circle of the contour
- The past and present lives of visual page building tools
- Xilinx Vivado set *.svh as SystemVerilog Header
- LeetCode_字符串_简单_412.Fizz Buzz
猜你喜欢

Large top heap, small top heap and heap sequencing
![[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment](/img/e1/c8e81570ab78de1e488a611c25ebb9.png)
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment

JMeter script parameterization

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

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

Leetcode - Search 2D matrix

Dragonfly low code security tool platform development path

About text selection in web pages and counting the length of selected text

Simple verification code generator for 51 single chip microcomputer experiment

Table responsive layout tips
随机推荐
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
How does CTO help the business?
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
数据库内容输出有问题怎么解决
C#代码审计实战+前置知识
info [email protected] : The platform “win32“ is incompatible with this module.
Fundamentals of software testing
Large top heap, small top heap and heap sequencing
TiDB 环境与系统配置检查
Advanced C language (realize simple address book)
C RichTextBox controls the maximum number of lines displayed
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
Yolov6 training: various problems encountered in training your dataset
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
The past and present lives of visual page building tools
可视化搭建页面工具的前世今生
Xilinx Vivado set *.svh as SystemVerilog Header
Base64 coding can be understood this way
fatal: unsafe repository is owned by someone else 的解决方法
TiDB 软件和硬件环境建议配置