当前位置:网站首页>蓝桥杯_N 皇后问题
蓝桥杯_N 皇后问题
2022-06-24 06:55:00 【踏过山河,踏过海】
蓝桥杯每日一题
解题思路
又到了咬文嚼字的时间了,蓝桥杯就喜欢出这种问题,有点点烦,但是还是一起来看看吧!
解题思路:看成N*N矩阵棋盘,每两个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成 45 角的斜线上
当n等于4时,有两种情况

代码
#include <iostream>
#include <stdio.h>
using namespace std;
int x[15] = {
0};
int sum,n;
int PD(int k)
{
for(int i=1; i<k; i++)
{
if(abs(k-i)==abs(x[k]-x[i]))
return 0;
else if (x[k]==x[i])
return 0;
//即判断是否符合条件来放,i表示皇后所在的行数,x[i]表示所在的列数,
//所以前面那个条件用来判断两个皇后是否在对角线上,后面用来判断是否在同一列上。
//行数不需要判断,因为他们本身的i就代表的是行数
}
return 1;
}
bool check(int a)
{
if(a>n)
sum++;
else
return 0;
return 1;
}
void DFS(int a)
{
if(check(a))
return ;
else
for(int i=1; i<=n; i++)
{
x[a]=i;
//第a个皇后放的列数
if(PD(a))
//判断是否能放这步
DFS(a+1);
//能的话进行下一个皇后的放置
else continue ;
//不能就下一列
}
}
int main()
{
cin>>n;
//表示几个皇后
DFS(1);
//每次都从第一个皇后开始
cout<<sum<<endl;
return 0;
}
边栏推荐
- Standing at the center of the storm: how to change the engine of Tencent
- 3-list introduction
- JDBC 在性能测试中的应用
- Serialization of unity
- Model effect optimization, try a variety of cross validation methods (system operation)
- L1-019 谁先倒 (15 分)
- 不止于观测|阿里云可观测套件正式发布
- Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization
- Bit operation
- AWTK 最新动态:Grid 控件新用法
猜你喜欢

Shader common functions

快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning

Hilbert Huang Transform
![[run the script framework in Django and store the data in the database]](/img/6b/052679e5468e5a90be5c4339183f43.png)
[run the script framework in Django and store the data in the database]

The first exposure of Alibaba cloud's native security panorama behind the only highest level in the whole domain

Signature analysis of app x-zse-96 in a Q & a community

第 2 篇:绘制一个窗口

From jsonpath and XPath to spl

Live wire, neutral wire and ground wire. Do you know the function of these three wires?

Chapter 3: drawing triangles
随机推荐
OC Extension 检测手机是否安装某个App(源码)
第 3 篇:绘制三角形
Thread support
Ad-gcl:advantageous graph augmentation to improve graph contractual learning
Echart's experience (I): about y axis yaxis attribute
L1-019 谁先倒 (15 分)
[nilm] non intrusive load decomposition module nilmtk installation tutorial
Specify IP when calling feign interface
Vulnhub target: boredhackerblog: social network
Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV
基于Distiller的模型压缩工具简介
Signature analysis of app x-zse-96 in a Q & a community
Random number remarks
Decltype usage introduction
Do you still have the opportunity to become a machine learning engineer without professional background?
Introduction to software engineering - Chapter 3 - Requirements Analysis
热赛道上的冷思考:乘数效应才是东数西算的根本要求
Chapter 2 line graph of canvas
快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning
慕思股份在深交所上市:毛利率持续下滑,2022年一季度营销失利