迷宫求解_栈的应用

来源:互联网 发布:常见面试几种算法 编辑:程序博客网 时间:2024/05/03 10:04
#ifndef STACK_FOR_MAZE_H#define STACK_FOR_MAZE_H  #include <stdbool.h>  struct Node;typedef struct Node * Stack; //用于声明指向表头的指针   typedef struct Node * PtrToNode; //用于声明指向结构体(节点)的指针   typedef int ElementType;struct Node{ElementType row;//行号,从0开始ElementType column;//列号,从0开始PtrToNode next;//前一个节点(元素)地址};/*********函数声明********///创建一个空栈,返回表头地址  Stack InitStack(void);//判断是否为空 空返回true bool IsEmpty(Stack S);  // 出栈 返回新栈顶地址  void Pop(Stack S);//清空栈 变为空栈   void ClearStack(Stack S);//返回栈中元素数目 int StackLength(Stack S);//返回行号的值 ElementType GetRow(Stack S);//返回列号的值ElementType GetColumn(Stack S);// 入栈 返回新栈顶地址void Push(ElementType _row, ElementType _column, Stack S);#endif


#include <stdio.h>#include <stdlib.h>#include "stackformaze.h"Stack InitStack(void){Stack tempNode;tempNode = (PtrToNode)malloc(sizeof(struct Node));if (tempNode == NULL)printf("Out of space!\n");tempNode->next = NULL;return tempNode;}bool IsEmpty(Stack S){return S->next == NULL;}void Pop(Stack S){// 出栈 返回新栈顶地址  PtrToNode tempNode;if (IsEmpty(S)){printf("Stack is empty\n");}else{tempNode = S->next;S->next = S->next->next; //S为表头地址,表头的next存的是栈顶的地址   free(tempNode); //栈底元素的next为NULL 所以在删除最后一个元素后   }}void ClearStack(Stack S){while (IsEmpty(S) != true)Pop(S);}int StackLength(Stack S){int sum = 0;while (IsEmpty(S) != true){S->next = S->next->next;sum++;}return sum;}ElementType GetRow(Stack S){if (IsEmpty(S)){printf("Stack is empty\n");return 0; //返回值防止编译器报错   }elsereturn S->next->row;}ElementType GetColumn(Stack S){if (IsEmpty(S)){printf("Stack is empty\n");return 0; //返回值防止编译器报错   }elsereturn S->next->column;}void Push(ElementType _row, ElementType _column, Stack S){PtrToNode tempNode;tempNode = (PtrToNode)malloc(sizeof(struct Node));if (tempNode == NULL){printf("Out Of Space!\n");}else{tempNode->next = S->next;tempNode->row = _row;tempNode->column = _column;S->next = tempNode;}}


#include <stdio.h>#include <stdlib.h>#include <time.h>#include "stackformaze.h"//迷宫复杂度,越大,迷宫没有出口的概率越大,详情见initMaze函数#define COMPLEXITY 3typedef struct coordinate{int maze[20][20];int flag[400];//标记某点是否有路可走,1表示有,0表示没有,与二维数组的数据关系为index = i * size + j}Maze;void initMaze(Maze &M, int size);int main(){int size = 10;//迷宫的尺寸size * size,size不能大于20,不能小于5Maze myMaze;//迷宫二维数组Stack stepForMaze = InitStack();//记录正确路线坐标initMaze(myMaze, size);int tempRow;//当前所在行号int tempCol;//当前所在列号Push(1, 1, stepForMaze);//起点录入//未到终点且栈不为空则继续循环printf("探索过程为:\n");while ((GetRow(stepForMaze) != size - 2 || GetColumn(stepForMaze) != size - 2) && !IsEmpty(stepForMaze)){tempRow = GetRow(stepForMaze);//当前所在行号tempCol = GetColumn(stepForMaze);//当前所在列号myMaze.flag[tempRow * size + tempCol] = 0;//标记该点来过了,防止通过Pop以外的方式回退printf("(%d,", tempRow);printf("%d) -> ", tempCol);if (myMaze.maze[tempRow][tempCol + 1] == 0 && myMaze.flag[tempRow * size + tempCol + 1] == 1)Push(tempRow, tempCol + 1, stepForMaze);else if (myMaze.maze[tempRow + 1][tempCol] == 0 && myMaze.flag[(tempRow + 1) * size + tempCol] == 1)Push(tempRow + 1, tempCol, stepForMaze);else if (myMaze.maze[tempRow][tempCol - 1] == 0 && myMaze.flag[tempRow * size + tempCol - 1] == 1)Push(tempRow, tempCol - 1, stepForMaze);else if (myMaze.maze[tempRow - 1][tempCol] == 0 && myMaze.flag[(tempRow - 1) * size + tempCol] == 1)Push(tempRow - 1, tempCol, stepForMaze);else{Pop(stepForMaze);//四个方向都走不通则出栈}}printf("\n");if (IsEmpty(stepForMaze)){printf("迷宫没有出路!\n");}else{printf("最终得出的可行路径为:\n");while (!IsEmpty(stepForMaze)){printf("(%d,", GetRow(stepForMaze));printf("%d) <- ", GetColumn(stepForMaze));Pop(stepForMaze);}}system("pause");}//生成一个size *size大小的迷宫并输出,包括四周墙壁,size不能大于20,不能小于5void initMaze(Maze &M,int size){srand((int)time(0));if (size > 20 || size < 5){printf("size不能大于20,不能小于5!\n");exit(-1);}for (int i = 0; i < size; i++){//初始化四周的墙壁if (i == 0 || i == size - 1){for (int j = 0; j < size; j++){M.maze[i][j] = 1;M.flag[i * size + j] = 1;}}else{M.maze[i][0] = 1;M.maze[i][size - 1] = 1;M.flag[i * size + 0] = 1;M.flag[i * size + size - 1] = 1;}}for (int i = 1; i < size - 1; i++){//利用随机数初始化迷宫for (int j = 1; j < size - 1; j++){//保证起点附近四个单位为0if ((i == 1 && j == 1) || (i == 1 && j == 2) ||(i == 2 && j == 1) || (i == 2 && j == 2)){M.maze[i][j] = 0;M.flag[i * size + j] = 1;}else if (i == size - 2 && j == size - 2){//保证终点为空M.maze[i][j] = 0;M.flag[i * size + j] = 1;}else{//如此处理使得0出现的概率大于1int temp = rand() % 10;if (temp > COMPLEXITY){M.maze[i][j] = 0;M.flag[i * size + j] = 1;}else{M.maze[i][j] = 1;M.flag[i * size + j] = 1;}}}}for (int i = 0; i < size; i++){//打印迷宫for (int j = 0; j < size; j++){printf("%d ", M.maze[i][j]);}printf("\n");}}



0 0
原创粉丝点击