回溯--迷宫问题
来源:互联网 发布:淘客数据采集 编辑:程序博客网 时间:2024/05/29 03:59
感觉做了两个回溯之后都差不多....
#include <iostream>
#include <stack>
using namespace std;
typedef enum{AVAILABEL, ROUTE, BACKTRACKED, WALL} Status;//前期准备 记录当前迷宫的状态,分别为可用(未探索),已用,已经探测过没有退路的,还有墙
typedef enum{ UNKNOWN, EAST, SOUTH, WEST, NORTH, NO_WAY} ESWN;
//enum方向,从unknown开始,到NORTH,遇到NO_WAY就要回溯并标记 BACKTRACKED了
inline ESWN nextESWN ( ESWN eswn)
{
return ESWN(eswn + 1);
}
//这个内联函数用来返回方向的
struct Cell{
int x, y;
Status status;
ESWN incoming, outgoing;
};
//迷宫宫格
#define LABY_MAX 24
Cell labe[LABY_MAX][LABY_MAX];
inline Cell* neighbor (Cell* cell)
{
switch (cell->outgoing){
case EAST : return cell + LABY_MAX;
case SOUTH : return cell + 1;
case WEST : return cell - LABY_MAX;
case NORTH : return cell - 1;
default: exit(-1);
}
}
//去探测当前cell的下一个邻居cell
inline Cell* advance (Cell* cell){
Cell *next;
switch (cell ->outgoing){
case EAST :
next = cell + LABY_MAX;
next ->incoming = WEST;
break;
case SOUTH :
next = cell + 1;
next ->incoming = NORTH;
break;
case WEST :
next = cell - LABY_MAX;
next ->incoming = EAST;
break;
case NORTH : next = cell - 1;
next ->incoming = SOUTH;
break;
default: exit(-1);
}
return next;
}
//跳转到下一个邻居,并给邻居做上标记
bool laberinth(Cell Laby[LABY_MAX][LABY_MAX], Cell *sourth, Cell *tail)
{
if ((AVAILABEL != sourth->status) || (AVAILABEL != tail ->status))
{
return false;
}
//这是用来处理初始条件不符合的情况
stack <Cell*> path;
sourth->incoming = UNKNOWN;
sourth -> status = ROUTE;
path.push(sourth);
//初始设定,我觉得也应该将outgoing 设置成UNKNOWN的
do //不断循环,直到遍历所有,或者有结果
{
Cell *c = path.top();//当前位置,若和出口相等,退出
if (c == tail)
{
return true;
}
while(NO_WAY > (c -> outgoing = nextESWN(c->outgoing)))//不断去探索路,直到找到没去过的
//如果没有,就回溯
if (AVAILABEL == neighbor(c)->status) break;
if (NO_WAY <= c->outgoing)
{
c->status = BACKTRACKED;
path.pop();
}
else //可以的话就push c的邻居进去,并且标记
{
path.push(c = advance(c));
c->outgoing = UNKNOWN;
c->status = ROUTE;
}
} while (!path.empty());
}
- 迷宫问题 回溯法
- 回溯算法---迷宫问题
- 回溯法-迷宫问题
- 迷宫问题回溯算法
- 回溯--迷宫问题
- 回溯求解-迷宫问题
- 回溯法求迷宫问题
- 回溯算法之迷宫问题
- 回溯法解迷宫问题
- 回溯法解迷宫问题
- 回溯法求迷宫问题
- 回溯法解决迷宫问题
- 回溯法求迷宫问题
- 回溯法解决迷宫问题
- 回溯法解决迷宫问题
- 迷宫问题(深搜+回溯)
- 回溯法求解迷宫问题
- 回溯算法解决迷宫问题
- iOS 8_UIAlertController
- Maven下的SpringMVC4.2+hibernate4.3整合
- Minimum Inversion Number
- 存储过程
- 适配器模式
- 回溯--迷宫问题
- Java内存分配和String类型的深度解析
- Android 生成正式签名的APK文件
- Android实战简易教程-第三十七枪(ListView中点击button跳转到拨号界面实例)
- hdu 1864 最大报销额 动态规划
- scala实现设计模式之装饰者模式
- uC/OS-II 函数之信号量相关函数
- NOIP2010 引水入城(BFS+贪心)
- 再次开始学习python-Python Web实战 第一课