迷宫问题
来源:互联网 发布:常用推荐算法 编辑:程序博客网 时间:2024/06/05 14:29
STL讲义上搜索的一道例子,代码是书上的,有点问题改动了一点,二维数组表示的迷宫,0代表没路,1代表有路,采用深度搜索(应该是吧刚学不清楚。。。。
#include<iostream>#include<vector>using namespace std;int maze[4][6] = { {1, 1, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 0}, {1, 1, 0, 1, 0, 0}, {0, 1, 1, 1, 0, 0}};vector<pair<int, int> > path;int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};//下 右 上 左void printvector(vector<pair<int, int> > path){ vector<pair<int , int > >::iterator pit; for(pit=path.begin();pit != path.end();pit++) { cout<<pit->first<<","<<pit->second<<" -> "; } cout<<"3,3"<<endl;}void search(vector<pair<int, int> > tpath, int x, int y){ if(x < 0 || y < 0 || x > 5 || y > 3)//越界返回 return; if(x == 3 && y == 3) { path = tpath; //如果找到了出口,则记录下路径 printvector(path); return; } for(int ix = 0; ix < 4; ix++)//四个方向搜索 { if(maze[x+dir[ix][0]][ y+dir[ix][1]] == 1) { tpath.push_back(make_pair(x, y)); maze[x][y] = 2; search(tpath, x+dir[ix][0], y+dir[ix][1]); tpath.pop_back();//删除最后一个元素 } }}int main(){ vector<pair<int, int> > tpath; search(tpath, 0, 0);//从开始点找起 cout<<endl;}
0 0
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题...
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- 迷宫问题
- [李景山php]每天TP5-20161204|Loader.php-2
- Git上传代码到github
- C语言笔记(《C与指针》)
- Android使用post方式上传图片到服务器的方法
- Android基础之内存溢出
- 迷宫问题
- windows7下的PHP+apache+mysql+eclipse for php 开发环境搭建
- java单链表的基本操作
- 高中OJ3046. 【NOIP2012模拟10.23】游戏
- 例题:除法(UVa 725)
- NDK开发的javah命令无法生成h文件
- Revit开发设置模型线颜色
- [剑指offer学习心得]之:两个链表的第一个公共结点
- 欢迎使用CSDN-markdown编辑器