C/C++实现dfs(迷宫问题 最短路径)超级详细

输入一个二维数组表示迷宫,然后输入起始位置和要找到的位置,求其最短路径

地图样例
5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 1 4 3

在这里插入图片描述
一个点可以向上 下 左 右 四个点继续走,所以得挨个走一下试试,走不动了就回退到上一个点,看看还能不能换个方向走,不行了就继续回退上一个点,以此类推,就是用bfs。为了方便,规定一个顺时针方向,即右下左上的顺序来走。

做这个题,可以直接写出四个方向的dfs,不过看上去很难看。。。。
以向右走为例

if(a[x][y+1]==0&&b[x][y+1]==0&&x>=1&&x<=n&&y+1>=1&&y+1<=m)
	//if语句分别判断了右边的点是否能走,是否未走过和是否越界
	{
		b[x][y+1]=1;
		dfs(x,y+1,step+1);
		b[x][y+1]=0;//别忘了dfs后要变成0,要不然没法回退(回溯)了
	}

也可以用一个方向数组直接表示下一步要走的位置,很简洁而且判断边界也很方便

int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};//定义,也是按照右下左上的顺序
//比如向右走,x不变,y+1,所以是dx[0]=0,d[y]=1,tx=x+dx[0],ty=y+dy[0]
for(int k=0;k<=3;k++)
	{
		tx=x+dx[k];
		ty=y+dy[k];
		if(tx<1||tx>n||ty<1||ty>m) continue;//判断边界
		if(a[tx][ty]==0&&b[tx][ty]==0)
		{
			b[tx][ty]=1;
			dfs(tx,ty,step+1);
			b[tx][ty]=0;//别忘了dfs后要变成0,要不然没法回退(回溯)了
		}
	}

啊哈算法上是这样给的方向数组。个人觉得不好理解,所以还是用两个数组吧

int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
for(k=0;k<=3;k++)
	{
		tx=x+next[k][0];
		ty=y+next[k][1];
	}

完整代码如下

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int a[100][100],b[100][100];//c++不让用map做变量名字......
int startx,starty,endx,endy;
int n,m;
int v=999999;//c++不让用min做变量名字......
void dfs(int x,int y,int step)
{
	int tx,ty;
	int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
	if(x==endx&&y==endy)
	{
		if(step<v)//判断是不是最小的路径
		{
			v=step;
			return;
		}
	}
	for(int k=0;k<=3;k++)
	{
		tx=x+dx[k];
		ty=y+dy[k];
		if(tx<1||tx>n||ty<1||ty>m) continue;
		if(a[tx][ty]==0&&b[tx][ty]==0)
		{
			b[tx][ty]=1;
			dfs(tx,ty,step+1);
			b[tx][ty]=0;//别忘了dfs后要变成0,要不然没法回退(回溯)了
		}
	}
	return;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}
	cin>>startx>>starty;
	cin>>endx>>endy;
	b[startx][starty]=1;//起始位置先标记上,表示已经走过了
	dfs(startx,starty,0);
	cout<<"从"<<startx<<","<<starty<<"到"<<endx<<","<<endy<<endl;
	cout<<"最短路径为"<<v;
	return 0;
}

  • 11
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的C代码实现bfs迷宫问题最短路径: ``` #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_ROW 10 #define MAX_COL 10 typedef struct { int row, col, parent; } Node; Node queue[MAX_ROW * MAX_COL]; int head = 0, tail = 0; char maze[MAX_ROW][MAX_COL+1] = { "#S######.#", "......#..#", ".#.##.##.#", ".#........", "##.##.####", "....#....#", ".######.#.", "........#.", ".#######.#", "....#...G#" }; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; void enqueue(Node n) { queue[tail++] = n; } Node dequeue() { return queue[head++]; } bool is_empty() { return head == tail; } void print_path(Node n) { if (n.parent == -1) { printf("(%d, %d)\n", n.row, n.col); } else { print_path(queue[n.parent]); printf("(%d, %d)\n", n.row, n.col); } } void bfs() { Node start = {0, 1, -1}; enqueue(start); maze[start.row][start.col] = '#'; while (!is_empty()) { Node current = dequeue(); if (maze[current.row][current.col] == 'G') { print_path(current); return; } for (int i = 0; i < 4; i++) { int nr = current.row + dr[i]; int nc = current.col + dc[i]; if (nr < 0 || nc < 0 || nr >= MAX_ROW || nc >= MAX_COL) { continue; } if (maze[nr][nc] == '#' || maze[nr][nc] == 'S') { continue; } Node next = {nr, nc, head-1}; enqueue(next); maze[nr][nc] = '#'; } } } int main() { bfs(); return 0; } ``` 这个代码使用了一个队列来实现bfs。在每次循环中,它从队列中取出一个节点,检查它是否是终点(G),如果是,就打印路径并结束程序。否则,它会检查节点周围的四个邻居,如果邻居是可通行的,就将它们加入队列中。如果队列为空,就说明没有从起点到达终点的路径。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值