• 数据结构设计——用栈实现迷宫问题的求解


    本篇文章中所有数据结构都是后期整理的,如有问题欢迎指正,转载请注明出处http://www.cnblogs.com/a1982467767/p/8889583.html

    求解迷宫问题

     

    1,问题描述 

     以一个m*n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。迷宫问题要求求出从入口(1,1)到出口(m,n)的一条通路,或得出没有通路的结论。 基本要求: 首先实现一个以链表作存储结构的栈类型,然后编写一个求迷宫问题的非递归程序,求得的通路,其中:(i,j)指示迷宫中的一个坐标, d表示走到下一坐标的方向。 左上角(1,1)为入口,右下角(m,n)为出口。 

    2.设计思路: 

      用栈实现迷宫问题的求解; 

    3.实验代码: 

    栈实现迷宫求解问题: 

    ************************************************************************************************************

    
    
      1 //maze_stack.cpp
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 #include<windows.h>
      5 #include"seqstack.h"
      6 
      7 #define MAX_ROW  12
      8 #define MAX_COL  14
      9 
     10 int maze[12][14] = {
     11     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     12     1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1,
     13     1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1,
     14     1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1,
     15     1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1,
     16     1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1,
     17     1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
     18     1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1,
     19     1, 0, 0, 0, 0, 1 ,0 ,0, 0 ,0 ,1 ,0 ,1 ,1,
     20     1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1,
     21     1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
     22     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
     23 };
     24 
     25 void print_line()
     26 {
     27     system("cls");
     28     printf("迷宫如下‘■’代表墙,数字 或者‘☆’表示路径
    ");
     29     int i, j;
     30     for (i = 0; i < MAX_ROW; i++){
     31         for (j = 0; j < MAX_COL; j++)
     32             if (maze[i][j] == 1)       printf("");
     33             else if (maze[i][j] >= 3){
     34                 printf("%2d", maze[i][j] - 2);
     35                 /*if (i == MAX_ROW-2 && j == MAX_COL-2)  printf("★");
     36                 else                   printf("☆");*/        
     37             }    
     38             else  printf("  ");
     39             printf("
    ");
     40         }
     41     printf("已到达出口...
    ");
     42     printf("可见使用栈求出的路径并非最优路径,根据我依次探索的方向不同,结果也将不同
    ");
     43 }
     44 
     45 void visit(mark p,int sign, PSeqStack S)
     46 {
     47     Push_SeqStack(S,p);
     48     switch (sign)
     49     {
     50     case 1: p.col++; Push_SeqStack(S, p); maze[p.row][p.col] = 2; break;//向右
     51     case 2: p.row++; Push_SeqStack(S, p); maze[p.row][p.col] = 2; break;//向下
     52     case 3: p.col--; Push_SeqStack(S, p); maze[p.row][p.col] = 2; break;//向左
     53     case 4: p.row--; Push_SeqStack(S, p); maze[p.row][p.col] = 2; break;//向上
     54     }
     55 }
     56 
     57 int main()
     58 {
     59     struct point p = { 1, 1 };
     60     maze[p.row][p.col] = 2;//遍历过的点设置为2
     61     PSeqStack S = Init_SeqStack();
     62     Push_SeqStack(S,p);
     63     while (!Empty_SeqStack(S))
     64     {
     65         Pop_SeqStack(S, &p);
     66         if (p.row == MAX_ROW - 2 && p.col == MAX_COL - 2)
     67             break;
     68         if (p.col + 1 < MAX_COL - 1 && maze[p.row][p.col + 1] == 0)//向右
     69         {    
     70             visit(p, 1, S);
     71             continue;
     72         }
     73         if (p.row + 1 < MAX_ROW - 1 && maze[p.row + 1][p.col] == 0)//向下
     74         {
     75             visit(p, 2, S);
     76             continue;
     77         }
     78         if (p.col - 1 >= 1 && maze[p.row][p.col - 1] == 0)//向左
     79         {
     80             visit(p, 3, S);
     81             continue;
     82         }
     83         if (p.row - 1 >= 1 && maze[p.row - 1][p.col] == 0)//向上
     84         {
     85             visit(p, 4, S);
     86             continue;
     87         }//以上是对迷宫的四个方向进行操作
     88     }
     89     if (p.row == MAX_ROW - 2 && p.col == MAX_COL - 2)//是否为出口
     90     {
     91         int count = GetLength_SeqStack(S)+3;//为了与迷宫0,1,2的区别所以基数要以3开始
     92         printf("成功找到出口,路径倒序输出:
    ");
     93         printf("(%d,%d)
    ", p.row, p.col);
     94         maze[p.row][p.col] = count;
     95         while (!Empty_SeqStack(S))//按照前驱进行查找
     96         {
     97             count--;
     98             Pop_SeqStack(S, &p);
     99             maze[p.row][p.col] = count;
    100             printf("(%d,%d)
    ", p.row, p.col);            
    101         }
    102         printf("3秒后打印路径......");
    103         Sleep(3000);
    104         print_line();
    105     }
    106     else {
    107         printf("没有出路
    ");
    108     }
    109     system("pause");
    110     return 0;
    111 }
    112 //end maze_stack.cpp
    
    

    *************************************************************************************************************

    
    
     1 //seqstack.h
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #define MAXSIZE 100
     5 
     6 typedef struct point{
     7     int  row, col;
     8 }mark;
     9 
    10 typedef mark DataType;
    11 
    12 typedef struct {
    13     DataType data[MAXSIZE];
    14     int top;
    15 }SeqStack, * PSeqStack;
    16 
    17 PSeqStack Init_SeqStack (void)
    18 {
    19     PSeqStack S;
    20     S = (PSeqStack)malloc(sizeof(SeqStack));
    21     if (S)
    22         S->top = -1;
    23     else
    24         exit(-1);
    25     return S;
    26 }
    27 
    28 int Empty_SeqStack(PSeqStack S)
    29 {
    30     //return (S->top==-1);
    31     if (S->top == -1)
    32         return 1;
    33     else
    34         return 0;
    35 }
    36 
    37 int Push_SeqStack(PSeqStack S,DataType x)
    38 {
    39     if (S->top == MAXSIZE - 1)
    40     {
    41         printf("栈满不能入栈
    ");
    42         return 0;
    43     }
    44     else 
    45     {
    46         S->top++;
    47         S->data[S->top] = x;
    48         return 1;
    49     }
    50 }
    51 
    52 int Pop_SeqStack(PSeqStack S,DataType *x)
    53 {
    54     if(Empty_SeqStack(S))
    55         return 0;
    56     else
    57     {
    58         *x = S->data[S->top];
    59         S->top--;
    60         return 1;
    61     }
    62 }
    63 
    64 int GetTop_SeqStack(PSeqStack S ,DataType *x)
    65 {
    66     if(Empty_SeqStack(S))
    67         return 0;
    68     else
    69     {
    70         *x = S->data[S->top];
    71         return 1;
    72     }
    73 }
    74 int GetLength_SeqStack(PSeqStack S)
    75 {
    76     return S->top + 1;
    77 }
    78 
    79 void Distory_SeqStack(PSeqStack *S)
    80 {
    81     if(*S)
    82         free(*S);
    83     *S = NULL;
    84 }//end seqstack.h
    
    
    
     

    4.运行结果:

    栈求解迷宫:

     

  • 相关阅读:
    telerik:RadGrid 在表格中编辑更新数据
    给已存在的表添加一个新字段
    Microsoft.Office.Interop.Excel 导出Excel
    反射导出 Excel
    aspx页面中获取当前浏览器url
    图片切换效果
    .net错误处理机制(转)
    javascript:void(0)知多少
    30款jQuery常用网页焦点图banner图片切换 下载 (转)
    Html.RenderPartial与Html.RenderAction区别(转)
  • 原文地址:https://www.cnblogs.com/a1982467767/p/8889583.html
Copyright © 2020-2023  润新知