数据结构 深度优先 迷宫问题代码
来源:互联网 发布:梁朝伟 帅 知乎 编辑:程序博客网 时间:2024/04/28 20:46
- #include <iostream>
- #include <cstring>
- #include <list>
- using namespace std;
- struct coor {
- int x, y;
- bool operator == (coor a)
- {
- if(a.x == x && a.y == y) return true;
- else return false;
- }
- coor operator += (coor a)
- {
- x += a.x, y += a.y;
- return *this;
- }
- coor operator -= (coor a)
- {
- x -= a.x, y -= a.y;
- return *this;
- }
- bool pend(int n, int m)
- {
- if(x >= 0 && y >= 0 && x < m && y < n) return true;
- else return false;
- }
- };
- int n, m;
- int mat[20][20];
- bool vis[20][20], ok = false;
- coor op, ed;
- list<coor> l;
- const coor dir[4] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };
- int all;
- void init()
- {
- cout << "Input Row and Col: ";
- cin >> n >> m;
- cout << "Input the matrix:" << endl;
- for(int i = 0; i < n; i++)
- for(int j = 0; j < m; j++) cin >> mat[i][j];
- cout << "Input the entrance coor: ";
- cin >> op.x >> op.y;
- cout << "Input the exit coor: ";
- cin >> ed.x >> ed.y;
- cout << "Input if you wanna see all the solutions(0 / 1): ";
- cin >> all;
- l.push_back(op);
- memset(vis, 0, sizeof(vis));
- }
- void success()
- {
- cout << endl;
- cout << "There'are total " << l.size() << " steps: " << endl;
- for(list<coor>::iterator i = l.begin(); i != l.end(); i++)
- cout << " --> (" << i->x << ", " << i->y << ") <--" << endl;
- }
- void dfs(coor now, coor ed)
- {
- if(now == ed)
- {
- if(!all) ok = true;
- success();
- }
- vis[now.y][now.x] = true;
- for(int i = 0; i < 4; i++)
- {
- now += dir[i];
- if(now.pend(n, m))
- {
- if(!mat[now.y][now.x] && !vis[now.y][now.x])
- {
- l.push_back(now);
- dfs(now, ed);
- l.pop_back();
- }
- if(ok) return;
- }
- now -= dir[i];
- }
- if(all) vis[now.y][now.x] = false;
- }
- int main()
- {
- init();
- dfs(op, ed);
- return 0;
- }
好吧,问题就是:读入一个迷宫,0是空地1是墙,然后DFS出出口。
- 数据结构 深度优先 迷宫问题代码
- 迷宫代码:深度优先算法
- Matlab 深度优先搜索求解迷宫问题
- 用深度优先搜索解迷宫问题
- 经典迷宫问题DFS 深度优先
- 深度优先搜索DFS(迷宫问题)
- 深度优先搜索求解迷宫问题
- 迷宫问题(深度优先搜索)
- 数据结构实践——迷宫问题之图深度优先遍历解法
- 数据结构实践——迷宫问题之图深度优先遍历解法
- 数据结构实践——迷宫问题之图深度优先遍历解法
- 数据结构实践——迷宫问题之图深度优先遍历解法
- 2015年大二上-数据结构-图-1-(5)-迷宫问题之图深度优先遍历解法
- 数据结构第十一周项目5--迷宫问题之图深度优先遍历解法
- 迷宫问题|深度优先搜索&广度优先搜索
- 走迷宫问题(深度优先遍历 + 广度优先遍历)
- 第十二周--数据结构-迷宫之图的深度优先遍历
- 数据结构上机-迷宫-非递归版深度优先搜索
- 小麦的学习笔记
- 盒饭故意
- 64位windows7系统下安装phpnow出现“安装服务 [ Apache_pn ] 失败”的解决办法
- C++中关于冒泡排序的算法代码
- create table xx.sql
- 数据结构 深度优先 迷宫问题代码
- jsp问题如何解决?!
- fedora下新版本jdk安装问题
- 大四了,感慨下。
- 在路上
- 越来越觉的压力大。
- 加油
- 天气变冷了。。
- 今天开始新旅程