zoukankan      html  css  js  c++  java
  • 迷宫求解(数据结构栈应用)

     数据结构课作业。--------

    参考资料:《数据结构(C语言版)》严蔚敏 吴伟民 编著;

      1 #include <cstdio>
      2 #include <cstdlib>
      3 #include <iostream>
      4 #include <cstring>
      5 using namespace std;
      6 
      7 #define INIT_SIZE 100 //存储空间初始分配量
      8 #define SIZE_ADD 10  //存储空间分配增量
      9 #define MAXLEN 10   //迷宫大小
     10 
     11 struct Coord
     12 {
     13       int r;
     14       int c;
     15       bool operator == (const Coord &b) const
     16       {
     17         return (r==b.r && c==b.c);
     18       }
     19 };
     20 
     21 struct Node
     22 {
     23        int  ord;
     24        Coord seat;
     25        int  di;
     26 };
     27 struct MazeType
     28 {
     29        int r;
     30        int c;
     31        char map[MAXLEN][MAXLEN];
     32 };
     33 int dir[][2]={1,0, -1,0, 0,1, 0,-1};
     34 bool flag;
     35 
     36 class Stack
     37 {
     38     public:
     39         Stack ()
     40         {
     41             base = (Node *) malloc(INIT_SIZE * sizeof (Node));
     42                 if (!base) exit(0);
     43             top = base;
     44             size = INIT_SIZE;
     45         }
     46         bool empty () const { return top==base; }
     47         void pop(Node &e);
     48         void push(Node a);
     49 
     50     private:
     51         Node * top;
     52         Node * base;
     53         int size;
     54 };
     55 
     56 void Stack::push(Node item)
     57 {
     58     if (top - base >= size)
     59     {
     60         base = (Node *) realloc(base, (size+SIZE_ADD)*sizeof(Node));
     61         if (!base) exit(0);
     62         top = base + size;
     63         size += SIZE_ADD;
     64     }
     65     *top++ = item;
     66 }
     67 
     68 void Stack::pop(Node &e)
     69 {
     70     if (top==base) return;
     71     e = * -- top;
     72 }
     73 
     74 void MadeMap (MazeType &maze)
     75 {
     76 
     77     freopen ("test.in","r",stdin);
     78     flag = false;
     79     cin >> maze.r >> maze.c;
     80         for(int i=0; i<maze.r; i++)
     81               for(int j=0; j<maze.c; j++)
     82                    scanf (" %c",&maze.map[i][j]);
     83 }      
     84 
     85 
     86 Coord NextPos(Coord &curpos, int i)
     87 {
     88 
     89       Coord ans;
     90       ans = curpos;
     91       ans.r += dir[i][0];
     92       ans.c += dir[i][1];
     93       return ans;
     94 }
     95 
     96 
     97 void MazePath(MazeType &maze, Coord start, Coord end)
     98 {
     99       Stack S;
    100       Coord curpos;  //当前坐标
    101       Node e;
    102 
    103       curpos=start;
    104       int curstep=1;
    105 
    106       do
    107       {    
    108          if(maze.map[curpos.r][curpos.c]=='.')  //此路可通
    109          {
    110              maze.map[curpos.r][curpos.c]='*';
    111 
    112              e.ord=curstep;
    113              e.seat=curpos;
    114              e.di=1;
    115 
    116              S.push(e);
    117              if(curpos == end)
    118              {
    119                  flag = true;
    120                  return;
    121             }
    122              curpos=NextPos(curpos,0);
    123              curstep++;                        
    124           }
    125           else
    126           {
    127                if(!S.empty())
    128                {
    129                     S.pop(e);
    130                     while(e.di==3 && !S.empty())
    131                           S.pop(e);
    132                     if(e.di < 4)
    133                   {
    134                          e.di++;
    135                          S.push(e);            
    136                          curpos=NextPos(e.seat,e.di);
    137                     }
    138                  }
    139              }
    140        }while(!S.empty());
    141 }
    142 
    143 void PrintMaze(MazeType &maze)
    144 {
    145 
    146      if (flag)
    147      {
    148          cout << "迷宫路程图:
    ";
    149          for(int i=0;i<maze.r;i++)
    150          {
    151             cout << "      ";
    152             for(int j=0;j<maze.c;j++)
    153                 printf("%2c",maze.map[i][j]);
    154             cout << endl;
    155          }
    156       }
    157       else
    158           cout << "
    *****    次迷宫无法从起点走到终点。   ******
    
    ";
    159 }
    160 
    161 int main()
    162 {
    163 
    164     MazeType maze;
    165     Coord start,end;
    166        
    167     MadeMap (maze);
    168 
    169     start.r = start.c = 0;
    170     end.c = maze.c-1;
    171     end.r = maze.r-1;
    172 
    173     MazePath(maze, start, end);
    174     PrintMaze(maze);
    175     return 0;
    176 }
  • 相关阅读:
    Java中equals和==的区别
    Mybatis中#{}和${}
    AJAX发送带Header的请求
    golang定时任务
    mysql中一条insert语句批量插入多条记录
    golang 从mysql取datetime类型数据
    MySQL 慢查询日志
    mysql日志文件开启及详解:General_log 和 Binlog
    如何在普清的屏上调试CSS样式二倍图背景
    transition、animation在macbook air上图片动画边缘抖动
  • 原文地址:https://www.cnblogs.com/khan724/p/4092007.html
Copyright © 2011-2022 走看看