DFS之搜索顺序例题

例题

马走日

题面

传送门
在这里插入图片描述
在这里插入图片描述

思路

这题问的是方案数,因此我们需要回溯;

Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long ll;

const int N = 1e1 + 10;

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

int n,m,sx,sy,ans;

bool vis[N][N];

void dfs(int x,int y,int cnt){
	if(cnt == n*m){
		++ans;
		return;
	}
	for(int i=0,xx,yy;i<8;++i){
		xx = x + dx[i];
		yy = y + dy[i];
		if(xx < 1 || xx > n || yy < 1 || yy > m) continue;
		if(vis[xx][yy]) continue;
		vis[xx][yy] = 1;
		dfs(xx,yy,cnt+1);
		vis[xx][yy] = 0;
	}
}

void solve(){
	memset(vis,0,sizeof vis);
	cin >> n >> m >> sx >> sy;
	ans = 0;
	++sx,++sy;
	vis[sx][sy] = 1;
	dfs(sx,sy,1);
	cout << ans << '\n';
}

int main(){
	std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int t;
	cin >> t;
	while(t--)
		solve();
	return 0;
}

单词接龙

题面

传送门
在这里插入图片描述
在这里插入图片描述

思路

先预处理出任意两个字符串之间的最小覆盖是多少(因为我们想要龙尽可能的长)

随后暴力dfs即可;

Code

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

const int N = 2e1 + 10;

string words[N];

int G[N][N];//G(i,j) 表示单词i后面接j的最小覆盖

int used[N],ans,n;

//"龙"是什么,最后接的谁
void dfs(string tot,int back){
	ans = max(ans,(int)tot.size());
	for(int i=1;i<=n;++i){
		if(G[back][i] && used[i] < 2){
			++used[i];
			dfs(tot + words[i].substr(G[back][i]),i);
			--used[i];
		}
	}
}

void solve(){
	cin >> n;
	for(int i=1;i<=n;++i) cin >> words[i];
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j){
			string &u = words[i],&v = words[j];
							//不能是子集
			for(int len=1;len<min(u.size(),v.size());++len){
				//后缀匹配前缀
				if(u.substr(u.size() - len,len) == v.substr(0,len)){
					G[i][j] = len;
					break; // 取最小重合
				}
			}
		}
	
	char start;
	cin >> start;
	for(int i=1;i<=n;++i)
		if(words[i][0] == start){
			//一开始都是0次
			++used[i];
			dfs(words[i],i);
			--used[i];
		}
	cout << ans << '\n';		
}

int main(){
	std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	solve();
	return 0;
}

分成互质组

题面

分成互质组

在这里插入图片描述

思路

对于每个数来说,有两种选择;

  • 放入某一个已存在的组中
  • 新开一个组

我们只需要将这两种情况枚举完,最后取一个min即可;

Code

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

const int N = 1e1 + 10;

int n,group; // 组数

vector<int> G[N];

int a[N],ans = 1e9;

int gcd(int x,int y){
	return y ? gcd(y,x%y) : x;
}

bool check(int x,vector<int>& _group){ //x能否插入group中
	for(int i=0;i<_group.size();++i){
		if(gcd(x,_group[i]) > 1){
			return false;
		}
	}
	return true;
}	

void dfs(int now){ //now代表是第几个数
	if(group >= ans) return; //剪枝
	if(now == n + 1){
		ans = group;
	}
	for(int i=1;i<=group;++i){
		if(check(a[now],G[i])){
			G[i].push_back(a[now]);
			dfs(now + 1);
			G[i].pop_back();
		}
	}

	G[++group].push_back(a[now]);
	dfs(now + 1);
	G[group--].pop_back();
}

void solve(){
	cin >> n;
	for(int i=1;i<=n;++i) cin >> a[i];
	dfs(1);
	cout << ans << '\n';
}

int main(){
	std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DFS搜索是一种使用深度优先遍历算法实现的搜索方法,用于在树或图中查找特定节点或解决问题。在Python中,可以使用递归或栈数据结构来实现DFS搜索算法。DFS搜索算法的基本思想是从起始节点开始,沿着树的深度优先遍历节点,并尽可能深入搜索树的分支。当搜索到达一个节点时,它会沿着未探索过的边继续向下搜索,直到无法继续深入为止。然后,搜索会回溯到上一个未探索的节点,并开始搜索其他分支。这个过程会一直进行下去,直到所有节点都被访问为止。 在Python中实现DFS搜索算法,可以使用递归方法或者显式地使用栈数据结构来模拟递归过程。递归方法通过递归函数来实现深度优先遍历,每次递归调用都会遍历当前节点的子节点。而显式地使用栈数据结构,则会将每个节点及其相关信息压入栈中,以便在回溯时能够继续搜索其他分支。 例如,可以使用以下Python代码实现DFS搜索算法: ```python def dfs(graph, start): visited = set() # 用于存储已访问过的节点 stack = [start # 使用栈来模拟递归过程 while stack: node = stack.pop() # 弹出栈顶节点 if node not in visited: visited.add(node) # 将节点标记为已访问 neighbors = graph[node # 获取当前节点的邻居节点 for neighbor in neighbors: stack.append(neighbor) # 将邻居节点压入栈中 return visited ``` 这段代码实现了一个简单的DFS搜索算法。其中,graph表示图的邻接关系,start表示起始节点。算法使用一个集合visited来记录已访问过的节点,使用一个栈stack来模拟递归过程。在每次循环中,算法从栈中弹出一个节点,检查它是否已访问过。如果没有访问过,则将其标记为已访问,并将其邻居节点压入栈中。算法重复这个过程,直到栈为空。 可以根据具体的应用场景和需求,对DFS搜索算法进行相应的改进和扩展。比如,可以添加目标节点的判断条件,以找到特定的节点或解决问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值