走迷宫-栈的使用

本文介绍了一种基于栈的迷宫求解算法,通过深度优先搜索策略在二维迷宫中寻找从起点到终点的路径。
#include <stdio.h>

#define MAX_STACK_SIZE 64
#define MAZE_SIZE 7
#define EXIT_ROW 5
#define EXIT_COL 5

struct element
{
   int row;
   int col;
   int dir;
};

struct offset
{
   int vert;
   int hori;
};

char move_str[] = { 'N', 'E', 'S', 'W' };

int maze[MAZE_SIZE][MAZE_SIZE] = {
   1,1,1,1,1,1,1,
   1,0,1,1,1,1,1,
   1,0,0,0,0,1,1,
   1,0,1,1,1,1,1,
   1,0,1,0,0,0,1,
   1,0,0,0,1,0,1,
   1,1,1,1,1,1,1,
};

offset move[] =
{
   {-1,0}, {0,1}, {1,0}, {0,-1} 
};

int mark[MAZE_SIZE][MAZE_SIZE] = {0};
element stack[MAX_STACK_SIZE];
int top = 0;

void Path()
{
   bool found = false;
   int row = 0, col = 0, dir = 0;
   int next_row = 0, next_col = 0, next_dir = 0;

   while( top > -1 && !found )
   {
      row = stack[top].row;
      col = stack[top].col;
      dir = stack[top].dir;

      if( dir > 3 )
      {
         mark[row][col] = 0;

         --top;
         ++stack[top].dir;
         continue;
      }

      next_row = row + move[dir].vert;
      next_col = col + move[dir].hori;

      if( next_row == EXIT_ROW && next_col == EXIT_COL )
      {
         found = true;
         ++top;
         stack[top].row = next_row;
         stack[top].col = next_col;
      }
      else if( !maze[next_row][next_col] && !mark[next_row][next_col] )
      {
         ++top;
         stack[top].row = next_row;
         stack[top].col = next_col;
         stack[top].dir = 0;

         mark[next_row][next_col] = 1;
      }
      else
      {
         ++stack[top].dir;
      }
   }

   if( found )
   {
      for( int i = 0; i < MAZE_SIZE; ++i )
      {
         for( int j = 0; j < MAZE_SIZE; ++j )
         {
            printf("%d ", maze[i][j]);
         }
         printf("\n");
      }
      for( int i = 0; i <= top; ++i )
      {
         printf("(%d, %d, %c)\n", stack[i].row, stack[i].col, move_str[stack[i].dir]);
      }
   }
}

int main(int argc, char **argv)
{
   mark[1][1] = 1;

   stack[0].row = 1;
   stack[0].col = 1;
   stack[0].dir = 0;

   Path();

   return 0;
}


### 使用实现老鼠迷宫问题 #### 的概念及其应用 是一种遵循后进先出(LIFO, Last In First Out)原则的数据结构。在迷宫求解过程中,被用来记录路径上的节点信息以及回溯的过程。当遇到死路时,可以通过弹出顶元素返回到上一状态继续探索其他可能的方向。 #### 迷宫表示方法 通常情况下,迷宫可以用二维数组来表示。例如,在引用材料中提到的方法[^3],通过 `vector<vector<int>>` 初始化一个二维数组用于存储迷宫的状态。其中: - `-1` 表示障碍物; - `0` 表示未访问的可通行区域; - `1` 或其他正整数值可以用来标记已访问过的点或者特定路径。 #### 算法基本流程 以下是基于迷宫求解算法的主要逻辑描述: 1. **起点入**:将迷宫入口作为初始点压入,并将其标记为已访问。 2. **方向探测**:按照预设顺序(通常是 上->下->左->右),尝试向四周扩展寻找下一个可行点。 3. **新点处理**:如果发现新的可用点,则将其加入并将该点设置为已访问;否则执行下一步。 4. **退操作**:如果没有找到任何有效的新点,则从中移除当前顶部元素(即回退一步),回到前一点重新检测剩余方向。 5. **终止条件**:一旦到达终点或将整个迷宫遍历完毕仍无法抵达目标则停止运行程序。 #### 完整代码实例 (Python 版本) 下面给出了一种简单的 Python 实现方式供参考: ```python class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def maze_path(maze, start_x, start_y, end_x, end_y): stack = [] directions = [(0,-1),(-1,0),(0,1),(1,0)] # 左 上 右 下 的移动增量元组列表 current_point = Point(start_x,start_y) while True: if current_point.x == end_x and current_point.y == end_y: break found_next_step = False for direction in directions: next_x,next_y=(current_point.x+direction[0],current_point.y+direction[1]) if maze[next_x][next_y]==0: # 如果这个格子没过而且不是墙 new_p=Point(next_x,next_y) stack.append(current_point) current_point=new_p maze[current_point.x][current_point.y]=2 # 将当前位置标记为已经过 found_next_step=True break if not found_next_step: if len(stack)==0: return None # 找不到出路的情况 else: current_point=stack.pop() result=[(p.x,p.y)for p in reversed([start]+stack+[end])] return result if __name__=="__main__": maze=[ [-1,-1,-1,-1], [ 0, 0,-1, 0], [-1, 0,-1,-1], [ 0, 0, 0, 0]] path=maze_path(maze,1,0,3,3) print(path) ``` 上述代码实现了利用解决简单迷宫问题的功能[^4]。 #### 总结 使用能够有效地模拟深度优先搜索策略来进行迷宫求解。这种方法不仅直观易懂,而且易于编程实现。然而需要注意的是,实际开发过程中还需要考虑边界情况以及其他特殊情况下的鲁棒性改进等问题。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值