当前位置:网站首页>7-1 n皇后问题
7-1 n皇后问题
2022-06-26 12:32:00 【白—】
7-1 n皇后问题
要求在n*n格的棋盘上放置彼此不会相互攻击的n个皇后。按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。
输入格式:
测试数据有多组,处理到文件尾。对于每组测试,输入棋盘的大小n(1<n<12)。
输出格式:
对于每组测试,输出满足要求的方案个数。
输入样例:
4
输出样例:
2
代码:
#include <iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int n,a[200],plan;
void dfs(int x){
if(x>n){
plan++;
return;
}
for(int i=1;i<=n;i++){
a[x]=i;
int flag=0;
for(int j=1;j<=x-1;j++){
if(a[x] == a[j] || fabs(x-j)==fabs(a[x]-a[j])){
flag=1;
break;
}
}
if(flag==0)
dfs(x+1);
}
}
int main()
{
while(scanf("%d",&n)!=EOF){
plan=0;
for(int i=1;i<20;i++)
a[i]=0;
dfs(1);
cout<<plan<<endl;
}
return 0;
}
202206260908日
边栏推荐
- I'd like to know what preferential activities are available for stock account opening? Is it safe to open an account online?
- Jsonarray and jsonobject of fastjson [easy to understand]
- How can we reach members more effectively?
- Scala-day05-set
- Omnichannel membership - tmall membership 1: opening tutorial
- VMware virtual machine bridging mode can not access the campus network "suggestions collection"
- 房租是由什么决定的
- VMware虚拟机 桥接模式 无法上网 校园网「建议收藏」
- Report on in-depth analysis and investment strategy recommendations for China's petroleum coke industry (2022 Edition)
- Scala-day01- companion objects and HelloWorld
猜你喜欢

Omnichannel membership - tmall membership 1: opening tutorial

【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构

Jmeter响应时间和tps监听器使用教程
![[graduation season · advanced technology Er] I remember the year after graduation](/img/e7/8e1dafa561217b77a3e3992977a8ec.png)
[graduation season · advanced technology Er] I remember the year after graduation

HUST network attack and defense practice | 6_ IOT device firmware security experiment | Experiment 3 freertos-mpu protection bypass

PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)

JMeter response time and TPS listener tutorial

Spark-day02-core programming-rdd

This executeQuery (SQL) cannot compile classes for JSP. What is the reason?

International beauty industry giants bet on China
随机推荐
CG骨骼动画
How long ago did PHP get
11、 Box styles and user interface
国际美妆业巨头押注中国
Fengshentai old shooting range Kali series
Introduction to the strongest swarm cluster one click deployment + hydrogen bomb level container management tool
7-2 大盗阿福
Omnichannel membership - tmall membership 2: frequently asked questions
我想知道,股票开户有哪些优惠活动?网上开户是否安全么?
One click deployment CEPH script
TP5 thinkphp5 extension package think Mongo operation mongodb time interval range query
JMeter response time and TPS listener tutorial
NFS共享存储服务安装
Basic principle of MOS tube and important knowledge points of single chip microcomputer
New routing file in laravel framework
Leetcode 78. 子集 and 90. 子集 II
CG bone animation
开通证券账户需要注意事项 开户安全吗
Scala-day02- variables and data types
File decryption in webgame development