深度寻路算法
深度寻路算法思想
- 规定试探方向顺序-顺时针(上右下左)和逆时针方向(上左下右)
- 实时记录每个点 当前试探方向 记录每个点是否走过
- 如果四个方向都不能走,则需要回退
- 回退每走一步,栈结构存储当前位置
注:需要回退时,删除当前栈顶元素,跳到当前栈顶元素处 - 遇到终点,循环结束栈结构存储起点到终点的路径,栈为空
1:已知当前坐标(currentpos),我们设置一个试探点(searchpos),通过设定的方向(顺时针或逆时针)
2:判断试探点是否能走,如果能走就标记该坐标能走,入栈。
3:如果不能走,改变方向,循环如此,直至找到终点。
4:没有找到终点,但栈为空,整个辅助地图遍历完毕。
寻路代码
int map[ROWS][CLOS] =
{
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1,
1 , 0 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 1,
1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1,
1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1,
1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1,
1 , 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1,
1 , 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1,
1 , 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1,
1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1
};
pathNode pathmap[ROWS][CLOS] = { 0 }; //辅助地图
//准备一个栈
MyTack<point> stack;
point begPos = { 1,1 }; //起点
point endPos = { 9, 8 }; //终点
//起点准备入栈
stack.push(begPos);
//标记起点走过
pathmap[begPos.row][begPos.clos].isFind = true;
//当前点
point currentpos = begPos;
//试探点
point searchPos;
//是否找到了终点
bool isFindEnd = false;
while (1)
{
//循环寻路
searchPos = currentpos;
switch (pathmap[currentpos.row][currentpos.clos].dir)
{
case p_up:
searchPos.row--;
pathmap[currentpos.row][currentpos.clos].dir++;
//判断能不能走
if (map[searchPos.row][searchPos.clos] == 0 && pathmap[searchPos.row][searchPos.clos].isFind == false)
{
pathmap[currentpos.row][currentpos.clos].dir++;
//走
currentpos = searchPos;
//标记当前点走过
pathmap[currentpos.row][currentpos.clos].isFind = true;
//入栈
stack.push(currentpos);
}
break;
case p_right:
searchPos.clos++;
pathmap[currentpos.row][currentpos.clos].dir++;
//判断能不能走
if (map[searchPos.row][searchPos.clos] == 0 && pathmap[searchPos.row][searchPos.clos].isFind == false)
{
pathmap[currentpos.row][currentpos.clos].dir++;
//走
currentpos = searchPos;
//标记当前点走过
pathmap[currentpos.row][currentpos.clos].isFind = true;
//入栈
stack.push(currentpos);
}
break;
case p_down:
searchPos.row++;
pathmap[currentpos.row][currentpos.clos].dir++;
//判断能不能走
if (map[searchPos.row][searchPos.clos] == 0 && pathmap[searchPos.row][searchPos.clos].isFind == false)
{
pathmap[currentpos.row][currentpos.clos].dir++;
//走
currentpos = searchPos;
//标记当前点走过
pathmap[currentpos.row][currentpos.clos].isFind = true;
//入栈
stack.push(currentpos);
}
break;
case p_left:
searchPos.clos--;
pathmap[currentpos.row][currentpos.clos].dir++;
//判断能不能走
if (map[searchPos.row][searchPos.clos] == 0 && pathmap[searchPos.row][searchPos.clos].isFind == false)
{
//走
currentpos = searchPos;
//标记当前点走过
pathmap[currentpos.row][currentpos.clos].isFind = true;
//入栈
stack.push(currentpos);
}
else
{//不能走 死胡同
stack.pop(); //删除当前栈顶元素
currentpos = stack.getTop(); //跳到栈顶元素
}
break;
}
Sleep(200);
printMap(map, currentpos);
//判断是否找到终点
if (endPos == currentpos)
{
isFindEnd = true;
break;
}
//判断是否为空
if (stack.isEmpty())
{
break;
}
}
//找到了终点了
if (isFindEnd)
{
printf("找到终点了\n");
while (stack.isEmpty())
{
printf("%d %d", stack.getTop().row, stack.getTop().clos);
stack.pop();
}
printf("\n");
}
创建一个栈
#pragma once
#include<stdlib.h>
#include<cstring>
template <class T>
class MyTack
{
T* buff;
int len;
int maxlen;
public:
MyTack() { buff = NULL; len = 0; maxlen = 0; };
~MyTack()
{
if (buff)
delete[] buff;
buff = NULL;
len = 0;
maxlen = 0;
};
//入栈
void push(const T& data);
//出栈
void pop()
{
if (len > 0)
{
len--;
}
}
//获取当前栈顶元素
T getTop()
{
return buff[len - 1];
}
//判断栈是否为空
bool isEmpty()
{
return (len == 0);
}
};
template <class T>
void MyTack<T>::push(const T& data)
{
//判断是否需要申请内存
if (maxlen <= len)
{
//计算需要申请的内存大小 >>1右移一位等同除以2
maxlen = maxlen + (((maxlen >> 1) > 1) ? (maxlen >> 1) : 1);
//开内存
T* pNew = new T[maxlen];
if (buff)
{
memcpy(pNew, buff, sizeof(T) * len);
//释放内存段
delete[] buff;
}
//指向新开内存
buff = pNew;
buff[len++] = data;
}
}
以上代码均不是完整版,本文章仅用此学习,如有问题请指正!