原文链接:欢迎浏览我的博客:http://www.fezhu.top/2020/03/31/%E6%B7%B1%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2DFS%E8%AF%A6%E8%A7%A3/
今天学习了深度优先搜索,觉得这一章讲的特别好,连我这么笨的人都看懂了,相信您也一定能看懂,所以我就想把它分享出来,至于为什么不自己写,我觉得原文更加容易理解(绝对不是因为我懒哈),说到这里必须吐槽一下WPS,把图转化成Word还要充钱,还那么贵,哎只能怪我本人太穷,废话少说,贴图吧
ok,是不是很详细呢,大力推荐啊哈算法这本书,讲的真的很好,在这里我把第二道题的代码贴出来吧,我觉得这是一道非常经典的DFS模板题,便于您复制(尽管您不需要)
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int Min=99999,m,n,tx,ty;
int next[4][2]={{1,0} , {0,1} , {-1,0} , {0,-1}},a[100][100];
int vis[100][100];
void dfs(int x,int y,int step){
int temp_x=x,temp_y=y;
if(x==tx&&y==ty){
if(step<Min) Min=step;
return ;
}
for(int i=0;i<=3;i++){
x=temp_x+next[i][0];
y=temp_y+next[i][1];
if(x<1||y<1||x>m||y>n) continue;
if(!a[x][y]&&!vis[x][y]){
vis[x][y]=1;
dfs(x,y,step+1);
vis[x][y]=0;
}
}
return;
}
int main()
{
cin>>m>>n;
int sx,sy;
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
cin>>sx>>sy>>tx>>ty;
vis[sx][sy]=1;
dfs(sx,sy,0);
cout<<Min<<endl;
}