迷宫和树等的BFS及DFS
来源:互联网 发布:快速转换视频格式软件 编辑:程序博客网 时间:2024/06/13 07:31
1 DFS
#include <iostream>
#include<cstring>#include<sstream>
using namespace std;
struct node
{
int x, y;
};
char map[4][4] = { { '.', '.', '.', '.' }, { '.', '#', '#', '.' }, { '.', '#', '.', '.' }, { '.', '.', '.', '.' } };
int visit[4][4] = { 0 };
node star{ 0, 0 }, end{ 2, 2 };
int minstep = 600;
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
int judge(int x, int y,int step)
{
if ((2- x + 2 - y + step) > minstep)
return 0;
else
return 1;
}
vector<node> minpath;
vector<node> path;
int step = -1;
void dfs(int x, int y,int step, vector<node> path)
{
path.push_back(node{ x, y });
visit[x][y] = 1;
step++;
if (x == 2 && y == 2)
{
if (step <= minstep)
{
minstep = step;
minpath = path;
for (int i = 0; i < minpath.size(); i++)
{
cout << minpath[i].x << " " << minpath[i].y << endl;
}
cout << "--------------------------"<< endl;
}
path.pop_back();
visit[x][y] = 0;
step--;
}
else
{
for (int i = 0; i < 4; i++)
{
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (judge(nx, ny, step) && nx >= 0 && nx < 4 && ny >= 0 && ny < 4 && map[nx][ny] == '.'&&visit[nx][ny] == 0)
{
dfs(nx, ny, step, path);
}
}
path.pop_back();
visit[x][y] = 0;
step--;
}
}
int main()
{
dfs(0, 0,step,path);
cout << minstep << endl;
system("pause");
return 0;
}
跟剑指offer25题一模一样,区别是树每条路径是向下所有孩子搜索,而迷宫是上下左右搜索,为了防止上下左右四个孩子回溯到本节点导致循环递归,设置visit变量。但是要记得本条路径搜索完毕之后visiti要复位,防止影响其他搜索路径的搜索!!
方法2 错误
#include<string>
using namespace std;
#include<iostream>
#include <vector>
#include<deque>
#include <list>
#include<queue>
#include<stack>
#include<algorithm>
#include <iomanip>
#include <functional>
#include <iostream>
#include<cstring>
#include<sstream>
using namespace std;
struct node
{
int x, y;
};
char map[4][4] = { { '.', '.', '.', '.' }, { '.', '#', '#', '.' }, { '.', '#', '.', '.' }, { '.', '.', '.', '.' } };
int visit[4][4] = { 0 };
node star{ 0, 0 }, end{ 2, 2 };
int minstep = 600;
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
int judge(int x, int y,int step)
{
if ((2- x + 2 - y + step) > minstep)
return 0;
else
return 1;
}
vector<node> minpath;
vector<node> path;
int step = 0;
void dfs(int x, int y,int step, vector<node> path)
{
path.push_back(node{ x, y });
visit[x][y] = 1;
//step++;
if (x == 2 && y == 2)
{
if (step <= minstep)
{
minstep = step;
minpath = path;
for (int i = 0; i < minpath.size(); i++)
{
cout << minpath[i].x << " " << minpath[i].y << endl;
}
cout << "--------------------------"<< endl;
}
path.pop_back();
visit[x][y] = 0;
//step--;
}
else
{
for (int i = 0; i < 4; i++)
{
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (judge(nx, ny, step) && nx >= 0 && nx < 4 && ny >= 0 && ny < 4 && map[nx][ny] == '.'&&visit[nx][ny] == 0)
{
int nstep = ++step;//ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!应该修改为int nstep=(step+1),并不能改变step的值,否则上下左右四个孩子的step起始值不一样了,影响其他三个孩子的最短路径判断!!!!
dfs(nx, ny, nstep, path);
}
}
path.pop_back();
visit[x][y] = 0;
//step--;
}
}
int main()
{
dfs(0, 0,step,path);
cout << minstep << endl;
system("pause");
return 0;
}
2 BFS
#include <iostream>
#include<cstring>
#include<sstream>
using namespace std;
struct node
{
int x, y, level;
};
int dir[4][2] = { { -1, 0 }, { 1, 0 }, {0,-1}, {0,1} };
int visit[10][10] = {0};
int sx = 0, sy = 0, ex = 2, ey = 2;
char map[4][4] = { { '.', '.', '.', '.' }, { '.', '#', '#', '.' }, { '.', '#', '.', '.' }, { '.', '.', '.', '.' } };
int bfs(char map[4][4])
{
queue<node > q;
node start{ 0, 0, 0 }, end{ 2, 2, 0 };
q.push(start);
visit[0][0] = 1;
int m = 4, n = 4;
while (!q.empty())
{
node now = q.front(),next;
q.pop();
for (int i = 0; i < 4; i++)
{
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.level = now.level + 1;
if (next.x == ex&&next.y == ey)
return next.level;
if (next.x >= 0 && next.x < m && next.y >= 0 && next.y < n && visit[next.x][next.y] == 0 && map[next.x][next.y] == '.')
{
q.push(next);
visit[next.x][next.y] = 1;
}
}
}
return 0;
}
int main()
{
cout << bfs(map) << endl;
system("pause");
return 0;
}
- 迷宫和树等的BFS及DFS
- 迷宫---DFS和BFS解法
- 迷宫路径(bfs和dfs)
- BFS和DFS的差别,BFS实现迷宫最短路径
- 迷宫问题 dfs bfs
- BFS,DFS,迷宫
- 树和图的BFS 和 DFS
- 二叉树的DFS和BFS
- 迷宫求解非递归 DFS BFS(应用栈和队列)
- 迷宫最短路径dfs和bfs代码分析
- 3752:走迷宫 bfs dfs
- poj3083 dfs+bfs搜索迷宫
- DFS/BFS 关于迷宫问题
- 数据结构作业 迷宫问题 模拟 之基础的BFS DFS
- 超级迷宫 pku 3083 dfs+bfs 非常有意思的一道
- 【搜索】BFS、DFS的综合练习(小鼠迷宫)
- 图的dfs和bfs
- DFS 和 BFS 的原理
- 百度地图多边形消失的问题解决
- ARM裸机点LED
- Linux Tomcat JVM 参数调优
- Qt4.7开发吐槽(一)
- 使用 Load Balancer,Corosync,Pacemaker 搭建 Linux 高可用集群
- 迷宫和树等的BFS及DFS
- 推箱子第一关(存在bug)
- 开发ASP.NET MVC 在线录音录像(音视频录制并上传)
- 双大括号初始化的理解和使用
- 2015-2016 Northwestern European Regional Contest (NWERC 2015)
- plsql安装过程所遇问题及解决方法记录
- 【Oracle】wmsys.wm_concat函数字段值为空
- 客户信息管理系统4—客户信息的查询
- jar包和war包的介绍与区别