COJ 0024 N皇后问题
2015-06-21 09:32
344 查看
N皇后问题 |
难度级别:B; 运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B |
试题描述 |
在N*N的方格棋盘放置N个皇,使得它们不相互攻击(即任意2个皇后不允许处在同一行,或同一列,也不允许处在与棋盘边框成45角的斜线上。你的任务是,对于给定的N,求出有多少种符合要求放置方法。 |
输入 |
输入中有一个正整数N≤20,表示棋盘和皇后的数量 |
输出 |
为一个正整数,表示N个皇后的不同放置方法数。 |
输入示例 |
5 |
输出示例 |
10 |
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<queue> #include<cstring> #define PAU putchar(' ') #define ENT putchar('\n') using namespace std; const int maxn=25; int ans=0,a[maxn],n; bool check(int t){ for(int i=0;i<t;i++) if(a[t]==a[i]||t+a[t]==i+a[i]||t-a[t]==i-a[i]) return false; return true; } void dfs(int d){ if(d==n){ans++;return;} for(int i=0;i<n;i++){ a[d]=i;if(check(d)) dfs(d+1); } return; } inline int read(){ int x=0,sig=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();} while(isdigit(ch))x=10*x+ch-'0',ch=getchar(); return x*=sig; } inline void write(int x){ if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; int len=0,buf[15];while(x)buf[len++]=x%10,x/=10; for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; } void init(){ n=read(); dfs(0);write(ans); return; } void work(){ return; } void print(){ return; } int main(){init();work();print();return 0;}
相关文章推荐
- 归并排序求数组中的逆序对
- getResources()方法
- Windows10怎么自定义设置3D文字屏幕保护?
- 测试相关理解(三)—边界值分析方法
- Makefile中的=, :=, ?=, +=
- inputStream 与 String 的互相转换
- Java知多少(99)Graphics2D类的绘图方法
- 图 像处理相关研究
- solrj实现增量索引
- MySQL InnoDB存储引擎undo redo解析
- jdk环境配置
- [caffe]caffe资料收集
- 与成功有约
- 一般线性模型
- Commit message中Partial-Bug, Related-Bug等含义
- Android启动画面
- C++语言基础 例程 C++中的输入和输出
- JavaScript中圆括号()和方括号[]的一个特殊用法
- JavaScript中圆括号()和方括号[]的一个特殊用法
- 黑马程序员——JAVA笔记——线程