poj3239(深搜)
来源:互联网 发布:apache 官网下载64位 编辑:程序博客网 时间:2024/04/30 01:43
传送门:poj3239
其实这个题深搜(n>25基本就崩的差不多了)是过不了的。。想过只能套公式,构造公式法详见:http://blog.csdn.net/lyy289065406/article/details/6642789
之所以写深搜是因为推导不出来公式,套用网上现成的公式又没多大意思,不如练一练搜索。
深搜还是老套路,依然需要注意的就是标记的复位问题,还有就是这个题要注意皇后之间的斜率不能为±1。
#include<stdio.h>#include<math.h>#include<algorithm>#include<string.h>using namespace std;int n;struct node{int c,r;}m[303];int book[303];int dfs(int r,int x)//参数为当前行数和已放置的皇后的个数 {if(x==n)return 1;int flag=0;for(int i=1;i<=n;i++)//枚举每一列 {flag=0;for(int j=1;j<=x;j++)//枚举每个已放置的皇后 {if(abs(m[j].c-i)==abs(r-m[j].r))//判断和前面已放置的皇后的斜率是不是±1 {flag=1;break;}}if(!flag&&!book[i])//如果当前列可以放置皇后 {book[i]=1;m[x+1].c=i;m[x+1].r=r;int f=dfs(r+1,x+1);if(f)//如果采取上面的放置方法后面无法继续放完要把当前位置标记取消 return 1;elsebook[i]=0; }}return 0;}int main(){while(scanf("%d",&n)&&n){dfs(1,0);memset(book,0,sizeof(book));for(int i=1;i<=n;i++)printf("%d ",m[i].c);putchar('\n');} }
0 0
- poj3239(深搜)
- POJ3239
- POJ3239 Solution to the n Queens Puzzle
- poj3239 Solution to the n Queens Puzzle (n皇后问题)
- poj3239 n皇后问题快速得到一个解
- POJ3239《Solution to the n Queens Puzzle》方法:构造公式法
- hdu1010(深搜 +剪枝)
- hdu(2266)深搜
- HDU1078(深搜+DP)
- 滑雪(dp+深搜)
- 池塘问题(深搜)
- hdu 1401(深搜)
- ZOJ1008(深搜)
- CSU1660(深搜)
- poj1321(深搜)
- poj1426(深搜)
- POJ2676 - Sudoku (深搜)
- HDU1015 - Safecracker (深搜)
- java 文件写入oracle数据库blob字段
- cocos2dx中c++向lua传string的问题
- HDU 4059-容斥原理 +拉格朗日插值法
- Nginx平滑升级
- android的代码规范
- poj3239(深搜)
- Intent使用规范:不要在Intent的中携带待会自动被提交的数据。
- Mysql5.7究竟该如何安装
- centos下安装NS2
- 14LongestCommonPrefix
- 深度学习笔记5torch实现mnist手写数字识别
- POJ 3252 Round Numbers 组合数学
- Servlet详解
- ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题