八皇后问题

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int cnt,n=8,a[15],b[105],c[105],d[105];
void dfs(int x)
{
	if(x==9)
	{
		cnt++;
		for(int i=1;i<=n;i++)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
		return ;
	}
	for(int i=1;i<=n;i++)
	{
		if(b[i]==0&&c[i+x]==0&&d[i-x+7]==0)
		{
			b[i]=1;
			c[i+x]=1;
			d[i-x+7]=1;
			a[x]=i;
			dfs(x+1);
			b[i]=0;
			c[i+x]=0;
			d[i-x+7]=0;
		}
	}
}
int main()
{
	dfs(1);
	cout<<cnt;
	return 0; 
} 
相关推荐
lsylalalala1 小时前
常见的排序算法1
数据结构·算法·排序算法
想吃火锅10052 小时前
【leetcode】54. 螺旋矩阵
算法·leetcode·矩阵
wuminyu2 小时前
JVM利用io_uring优化堆外内存性能原理剖析
java·linux·c语言·jvm·c++
gumichef2 小时前
第二章:C/C++内存管理
c++·内存管理
想吃火锅10052 小时前
【leetcode】300. 最长递增子序列
算法·leetcode·职场和发展
imaol12 小时前
数据结构---队列
java·数据结构·算法
数模竞赛Paid answer2 小时前
2026年电工杯数学建模A题绿电直连型电氢氨园区优化运行求解全过程论文及程序
算法·数学建模·电工杯
疯狂打码的少年2 小时前
【数据结构】串的模式匹配:KMP算法(重点)
数据结构·笔记·算法
一米阳光86612 小时前
软考(中级)软件设计师核心笔记(8)数据结构——线性结构、数组、矩阵
数据结构·笔记·职场发展·软考·软件设计师·中级职称
晚风醉蝶3 小时前
第零篇-算法全景图
算法