当前位置:网站首页>(dfs+ pruning + checkerboard problem +dood) acwing 843 N-queen problem
(dfs+ pruning + checkerboard problem +dood) acwing 843 N-queen problem
2022-06-13 09:24:00 【Age worry】
843. n- queens problem
Topic link https://www.acwing.com/problem/content/845/
subject :
Complexity :0(n!)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
char a[10][10];
bool c[10],dj[30],udj[30];
void dfs(int u){
if(u==n){
for(int i=0;i<n;i++)
puts(a[i]);
puts("");
return ;
}
for(int i=0;i<n;i++){
if(!c[i]&&!dj[u+i]&&!udj[n-u+i]){
c[i]=dj[u+i]=udj[n-u+i]=true;
a[u][i]='Q';
dfs(u+1);
a[u][i]='.';
c[i]=dj[u+i]=udj[n-u+i]=false;
}
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]='.';
dfs(0);
return 0;
}
边栏推荐
猜你喜欢
Exporting MySQL data table documents using Navicat
20220524 how to install coppeliasim to disk D
Online debugging tool Arthas advanced
线上调试工具Arthas基础
Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
final 原理
BGP Federation +community
线上调试工具Arthas高级
C language: Simulated Implementation of library function strcpy
C language: five custom types
随机推荐
[implementation of depth first search]
C language: recursive function to realize Hanoi Tower
20211020 academician all drive system
Class loading overview
HAProxy + Keepalived实现MySQL的高可用负载均衡
LeetCode 6095. Strong password checker II
20211028 adjustment and tracking
C language: Simulated Implementation of library function strcpy
CAS NO lock
BGP 联邦+Community
JUC原子数组
C language: summary of question brushing (1)
Redis fuzzy query batch deletion
Jenkins integrates LDAP. The problem of login failure of Jenkins users caused by LDAP configuration error is solved
Timestamp to localdate
共享模型之不可变
LeetCode 6095. 强密码检验器 II
Longadder of the source code of JUC atomic accumulator
Solov2 source code analysis
C language: deep understanding of character functions and string functions (2)