经典DFS问题实践
八皇后问题:
//八皇后问题 经典的DFS问题实践
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
int mat[][];
int ans=;
bool check(int row,int col)//最终检查满足要求,则return 1
{
for(int i=;i<row;i++)
{
if(mat[i][col])
return false;
for(int j=;j<;j++)
{
//不能在同一条对角线上
if(mat[i][j])
{
if(fabs(i-row)-fabs(j-col)==)
return ;
else
continue; }
}
}
return ;
}
int dfs(int row)
{
if(row>=)
{
ans++;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
cout<<mat[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
for(int col=;col<;col++)
{
if(check(row,col))
{
mat[row][col]=;
dfs(row+);
mat[row][col]=;
}
}
return ;
}
int main()
{
memset(mat,,sizeof(mat));
dfs();
cout<<"一共有"<<ans<<"种解决方案"<<endl;
}
//最终结果显示一共有92种解决方案
部分和问题
给定整数a1、a2、.......an,判断是否可以从中选出若干个数,使他们的和恰好为k。

//部分和问题
#include <iostream>
using namespace std;
const int Maxn=;
int a[Maxn];
int n,k;
bool dfs(int i,int sum){
if(i==n) return sum==k;
if(dfs(i+,sum)) return true;//不加上a[i]的情况
if(dfs(i+,sum+a[i])) return true;
return false; }
void solve()
{
if(dfs(,)) printf("yes!");
else printf("no!");
}
int main() {
cin>>n;
for(int i=;i<n;i++){
cin>>a[i];
}
cin>>k;
solve();
return ;
}
经典DFS问题实践的更多相关文章
- 洛谷 P1019 单词接龙【经典DFS,温习搜索】
P1019 单词接龙 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在 ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ 1321 - 棋盘问题 - [经典DFS]
题目链接:http://poj.org/problem?id=1321 Time Limit: 1000MS Memory Limit: 10000K Description 在一个给定形状的棋盘(形 ...
- 蓝桥杯之剪格子(经典dfs)
如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+--+-- ...
- LeetCode51 N皇后——经典dfs+回溯(三段式解法)
代码如下: class Solution { public: // record[row] 该行对应的列 vector<vector<string> > ans; // 结果集 ...
- HDU 1241 Oil Deposits(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 很经典的一道dfs,但是注意每次查到一个@之后,都要把它变成“ * ”,然后继续dfs ...
- HDU 1312 Red and Black(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...
随机推荐
- Python编码简要说明
●python2默认编码:ASCII编码 达到正确显示,程序需要编码转换: UTF-8 -- >decode解码 --> Unicode Unicode -- > encode编码 ...
- Jmeter 接口测试知识梳理——持续集成篇
Jmeter 使用也有很长时间了,但是一直没有做一下知识梳理,近期会对公司同事做一下这方面的培训,借此机会,把使用过程中应用到的知识,或是遇到的问题,整理出来,方便大家学习! Jmeter + Ant ...
- 如何获取select选中的值
一:JavaScript原生的方法 1:拿到select对象: var myselect=document.getElementById(“test”); 2.拿到选中项的索引: var index= ...
- [maven] introduction to the standard directory layout
The next section documents the directory layout expected by Maven and the directory layout created b ...
- rabbimq
问题 启动RabbitMQ后,没法访问Web管理页面 解决 RabbitMQ安装后默认是不启动管理模块的,所以需要配置将管理模块启动 启动管理模块命令如下 rabbitmqctl start_ ...
- English Voice of <<Dream it passible>>
Dream It Possible(梦想成为可能) - DelaceyI will run I will climb I will soar.我奔跑,我攀爬 我要飞翔.I'm undefeated我所 ...
- java中的Sort函数,你值得看
基于C语言中的sort如此这么方便,自然而然,java中也有类似C的sort函数. 1.普通数组:Arrays.sort(数组名,开始位置,结束位置). 2.类中属性排序: 模板: class A { ...
- Confluence 6 查看所有空间
有下面 2 种方法在 Confluence 中查看空间: 空间目录(The space directory) – 在 Confluence 的头部选择 空间(Spaces )> 空间目录(Spa ...
- python记录_day07
一.基本数据类型补充 1.列表的拼接用join()方法 li = ["hello","world"] s = "_".join(li) pr ...
- 【PowerDesigner】【5】数据模型 CDM
前言:各种箭头的含义其实我还是有点混乱,所以这里只做记录 参考博客: 1,Powerdesigner数据库建模--概念模型--ER图[转] - holycrap - 博客园https://www.cn ...