洛谷普及场(广搜)

本文通过P1605迷宫、P1141 01迷宫和P1443 马的遍历等题目,介绍了深度优先搜索(DFS)在解决迷宫问题中的基本思路和技巧。在DFS搜索过程中,需要注意状态的还原以避免死循环。同时,对于特殊走法如象棋马的移动,需要理解其行走规则来正确实现搜索算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • P1605迷宫

dfs搜索基础题,dfs搜索时每次搜索完要记得将转态还原

#include <cstdio>
const int N = 8 ; 
int n , m , t , ans ;
int maze[N][N] ; 
int dir[4][2] = {{-1,0},{0,-1},{1,0},{0,1}} ; 
int a , b , c , d ;
void dfs(int x1 , int y1 , int x2 , int y2){
	int x , y ;
	if (x1 == x2 && y1 == y2){
		ans ++ ; 
		return ; 
	} 
	for (int i = 0 ; i < 4 ; ++i){
		x = x1 + dir[i][0] ; 
		y = y1 + dir[i][1] ; 
		if (x>=1&&x<=n&&y>=1&&y<=m && !maze[x][y]){
			maze[x][y] = 1 ; 
			dfs(x,y,x2,y2) ; 
			maze[x][y] = 0 ;	//还原没走过的转态否则后面的走不了 
		}   
	}
}
int main(){
	scanf ("%d%d%d",&n,&m,&t) ;
	scanf ("%d%d",&a,&b) ;
	scanf ("%d%d",&c,&d) ;  
	int x , y ;
	for (int i = 1 ; i <= t ; ++i){
		scanf ("%d%d",&x,&y) ; 
		maze[x][y] = 1 ; 
	}
	maze[a][b] = 1 ; 
	dfs(a,b,c,d) ; 
	printf ("%d\n",ans) ; 
	return 0 ;  
}
  • P1141 01迷宫

  • 单独开了一篇博客:https://blog.youkuaiyun.com/coder370/article/details/101625931

  • P1443 马的遍历
    普通的搜索题,但是是象棋马的走法,一开始以为就是普通的上下左右(尴尬.jpg)

#include <cstdio>
#include <queue>
using namespace std ; 
const int N = 410 ; 
int dir[8][2] = {{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2},{-2,1},{-1,2}} ; 
int chess[N][N] ;
struct node{
	int x , y ; 
}; 
int n , m ; 
bool vis[N][N] ; 
int step ; 
void bfs(int x , int y){
	vis[x][y] = true ; 
	chess[x][y] = 0 ;
	queue<node> q ;
	node head , next ;
	head.x = x , head.y = y ;  
	q.push(head) ; 
	while(!q.empty()){
		head = q.front() ; 
		q.pop() ;
		for (int i = 0 ; i < 8 ; i ++){
			next.x = head.x + dir[i][0] , next.y = head.y + dir[i][1] ;
			if (next.x>=0&&next.x<n && next.y>=0&&next.y<m && !vis[next.x ][next.y]){
				chess[next.x ][next.y] = chess[head.x][head.y] + 1 ; 
				vis[next.x ][next.y] = true ;
				q.push(next) ; 
			}
		}
	}
	
}
int main(){
	int x , y ;   
	scanf ("%d%d%d%d",&n,&m,&x,&y) ;
	bfs(x-1,y-1) ; 
	for (int i = 0 ; i < n ; ++i){
		for (int j = 0 ; j < m ; ++j){
			if (chess[i][j] == 0){
				if (i == x-1 && j == y-1)
					printf ("%-5d",0) ; 
				else	printf ("%-5d",-1) ; 
			}
			else	printf ("%-5d",chess[i][j]) ; 
		}
		printf ("\n") ; 
	}
	return 0 ; 
}
### 关于洛谷平台上的广度优先索(BFS)题目与教程 #### 广度优先索(BFS)简介 广度优先索是一种用于图结构的遍历方法,其特点是按照层次顺序访问节点。它通常利用队列数据结构实现,适用于寻找最短路径等问题[^1]。 #### 洛谷平台上与BFS相关的题目推荐 以下是几道经典的基于BFS的题目: 1. **P1135 奇怪的电梯** - 这是一道典型的BFS应用题,涉及在一个特殊规则下的楼层移动问题。通过构建状态转移关系并使用BFS进行探索,可以高效解决该问题[^3]。 2. **P1141 宽度优先索 bfs** - 此题提供了一个由0和1构成的二维网格迷宫环境,要求计算从特定位置出发能够到达的最大格子数量。此题充分体现了BFS在处理连通性和可达性方面的优势[^4]。 3. **P1984 跳房子** - 本题是一个简单的跳跃游戏模拟,目标是从起点跳至终点,过程中需满足一定的条件限制。采用BFS策略可有效枚举所有可能的状态组合,从而得出最优解方案。 4. **P1768 小鱼吃大鱼** - 在这个游戏中,玩家控制一条小鱼不断吞噬较小的食物以增长体型直至达到最终目标大小。运用BFS可以帮助快速定位最近的有效食物源,进而优化整体决策过程。 #### 学习资源链接 除了实际练习之外,还可以参考一些高质量的学习资料加深理解: - 【普及组】广度优先索BFS——到达型索问题_C++算法竞赛[^2] 这些材料不仅讲解了基本原理还提供了丰富的实例分析,非常适合初学者入门以及进阶学习者巩固提升技能水平。 ```cpp // 示例代码展示如何初始化一个标准BFS框架 #include <bits/stdc++.h> using namespace std; int main(){ int n, m; cin >> n >> m; // 输入矩阵尺寸 vector<string> grid(n); for(auto &s : grid){ cin >> s; } while(m--){ int i, j; cin >> i >> j; queue<pair<int,int>> q; bool visited[n][n]; memset(visited,false,sizeof(visited)); q.push({i-1,j-1}); visited[i-1][j-1]=true; int count=1; const int dx[]={-1,0,1,0}; const int dy[]={0,-1,0,1}; while(!q.empty()){ auto [x,y]=q.front();q.pop(); for(int k=0;k<4;++k){ int nx=x+dx[k],ny=y+dy[k]; if(nx>=0 && ny>=0 && nx<n && ny<n && !visited[nx][ny]){ if(grid[x][y]!=grid[nx][ny]){ visited[nx][ny]=true; ++count; q.push({nx,ny}); } } } } cout<<count<<"\n"; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值