深度优先搜索(DFS) --- 入门篇

在 搜索算法 中,DFS常常指利用递归函数方便地实现暴力枚举的算法。可以说是高级枚举;递归算法中,往往我们需要借助一个函数工作栈,这个栈是有大小的,因此如果我们的递归层数过大,很可能会爆栈,因此DFS算法通常处理的数据范围都很小。

全排列问题 - 洛谷

这个是一个典型的DFS问题,我们传入一个值,来代表我们执行的次数,每次按顺序枚举    1 ~ n,如果当前值没有出现过,我们就使用它,并进行标记,然后递归调用,让num值++;当num值 == n 时,说明我们找到了一个全排列,我们输出它,并进行返回,返回的过程中,我们还要对之前的标记进行回溯,这很关键。

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<unordered_map>
#define x first
#define y second 
#define _for(i,s,t) for(int i = (s);i <=t ; i++)
#define FAST ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int,int> PII;

const int N = 10;

int st[N];
int p[N];

int n;

void dfs(int num)
{
	if(num == n)
	{
		_for(i,0,n-1)printf("%5d",p[i]);
		cout << '\n'; 
		return ;
	}
	for(int i =1 ;i <= n ;i++)
	{
		if(!st[i])
		{
			st[i] = true;
			p[num] = i;
			
			dfs(num + 1);
			st[i] = false;
		}
	}
}


int main()
{
	scanf("%d",&n);
	
	dfs(0); 
		
	return 0;
}

 求细胞数量 - 洛谷

 这个题,我们借助一个位移偏量,从第一个位置开始枚举,没错从该位置枚举上下左右四个方向如果该位置合法的话,我们进行递归,我们每走过一个合法位置没将其标记为非法就是‘0’,就巧妙的避免了重复搜索,节省了n个空间(尽管没多少);没遍历一次说明我们找到了一个细胞,记录答案就好了。

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<unordered_map>
#define x first
#define y second 
#define _for(i,s,t) for(int i = (s);i <=t ; i++)
#define FAST ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int,int> PII;

const int N = 110;

int n,m,res;
char p[N][N];

int dx[4] = {-1,0,1,0} , dy[4] = {0,1,0,-1};


void dfs(int x,int y)
{
	
	p[x][y] = '0';
	for(int i =0 ;i < 4 ;i++)
	{
		int a = dx[i] + x , b = dy[i] + y;
		
		if(a > 0 && a <= n && b > 0 && b <= m && p[a][b] != '0')
		{
			dfs(a,b);
		}
	}
}

int main()
{
	FAST;
	cin >> n >> m;
		
	for(int i = 1 ;i <= n ;i++)
		for(int j =1; j <=  m ; j++)
			cin >> p[i][j];
	

	for(int i =1 ;i<= n ;i++)
	{
		for(int j =1 ;j<= m ;j++)
		{
			if(p[i][j] != '0')
			{
				dfs(i,j);
				res++;
			}
		}
	}
	cout << res << endl;
	
	return 0;
}

 [USACO10OCT]Lake Counting S - 洛谷

 咱们再来看一个8偏移量的题,思路和上面的一样我就不重复写了,我们也是借助原数组避免重复搜索,也可以开一个bool数组,看自己喜好就行。

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<unordered_map>
#define x first
#define y second 
#define _for(i,s,t) for(int i = (s);i <=t ; i++)
#define FAST ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int,int> PII;

const int N = 110;

int dx[8] = {0,1,-1,0,1,1,-1,-1};
int dy[8] = {1,0,0,-1,1,-1,-1,1};

int n,m,res;
char p[N][N];

void dfs(int a,int b)
{
	
	p[a][b] = '.';
	for(int i = 0 ; i < 8 ;i++)
	{
		int x = a + dx[i] , y = b + dy[i];
		 
		if(x > 0 && x <= n && y > 0 && y <= m  && p[x][y] == 'W')
		{
			dfs(x,y);
		}
	}
	
} 

int main()
{
	cin >> n >> m;
	for(int i =1 ; i<= n ;i++)
		for(int j = 1 ;j<= m ;j++)
			cin >> p[i][j];
	
			
	for(int i =1 ;i <= n ; i++)
	{
		for(int j =1 ;j <= m ;j++)
			if(p[i][j] == 'W')
			{
				dfs(i,j);
				res++;
			}
	}
				
	cout<< res << endl;
	
	return 0;
}

  [USACO1.5]八皇后 Checker Challenge - 洛谷

我们再来看一个经典的问题,几乎是学习DFS必学的一个问题,八皇后问题;

 我们需要使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。

行列都好说,对角线我们怎么表示;

 我们可以知道主对角线的函数和负对角线的函数;

那我们可以巧妙的用截距来表示我们的主对角线和负对角线;

那由于我们主对角线上的截距是 y - x;那我们为了不出现负数,我们给他加上一偏移量n,即可;

接下来,就是我们的DFS了;

我们知道每行每列只能有一个棋子,那我们利用这个条件,按行枚举,然后判断该行对应的列,对角线即可;

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<unordered_map>
#define x first
#define y second 
#define _for(i,s,t) for(int i = (s);i <=t ; i++)
#define FAST ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int,int> PII;

const int N = 20;

int n,res,idx;
bool p[N][N];
bool col[N], dg[N], udg[N];

void dfs(int u)
{
	if(u == n + 1)
	{
		res++;
		if(res > 3) return ;
		
		for(int i =1 ;i <= n ;i++) 
		{
			for(int j = 1 ;j <= n ;j++)
			{
				if(p[i][j]) printf("%d ",j);
			}
		}
		cout<<'\n';
		return; 
	}
	
	for(int i =1;i <= n ;i++)
	{
		if(!col[i] && !dg[u+i] && !udg[n-u+i])
		{
			p[u][i] = true; 
			col[i] = dg[u+i] = udg[n-u+i] = true;
			dfs(u + 1);
			col[i] = dg[u+i] = udg[n-u+i] = false;//回溯
			p[u][i] = false; 
		}
	}
}


int main()
{
	scanf("%d",&n); 
	memset(p,false,sizeof p);
	dfs(1);
	
	printf("%d",res); 
	return 0;
} 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是饿梦啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值