Chinaunix首页 | 论坛 | 博客
  • 博客访问: 260199
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2015-03-07 17:46:40


点击(此处)折叠或打开

  1. template<class Type>
  2. class Stack
  3. {
  4.     public:
  5.         Stack()
  6.         {
  7.             capacity = STACK_SIZE;
  8.             base = new Type[capacity];
  9.             top = 0;
  10.         }
  11.         ~Stack()
  12.         {
  13.             capacity = 0;
  14.             delete []base;
  15.             base = NULL;
  16.             top = 0;
  17.         }
  18.     public:
  19.         bool isfull()const
  20.         {
  21.             return top >= capacity;
  22.         }
  23.         bool isempty()const
  24.         {
  25.             return top == 0;
  26.         }
  27.         void push(Type &x)
  28.         {
  29.             if(!isfull())
  30.             {
  31.                 base[top++] = x;
  32.             }
  33.         }
  34.         void pop()
  35.         {
  36.             if(!isempty())
  37.             {
  38.                 top--;
  39.             }
  40.         }
  41.         Type gettop()const
  42.         {
  43.             if(!isempty())
  44.             {
  45.                 return base[top-1];
  46.             }
  47.         }
  48.     private:
  49.         enum{STACK_SIZE=100};
  50.         Type *base;
  51.         size_t top;
  52.         size_t capacity;
  53. };

  54. /////////////////////////////////////////////
  55. #define ROW 10
  56. #define COL 10

  57. typedef enum
  58. {
  59.     RIGHT=1,
  60.     DOWN,
  61.     LEFT,
  62.     UP,
  63. }DIR;

  64. typedef int Maze[ROW][COL];//
  65. typedef struct Pos
  66. {
  67.     int x;
  68.     int y;
  69.     bool operator==(const Pos &p)
  70.     {
  71.         return ((x==p.x)&&(y==p.y));
  72.     }
  73. }Pos;

  74. typedef struct Man
  75. {
  76.     Pos pos;
  77.     DIR di;
  78. }Man;


  79. void ShowMaze(Maze maze)
  80. {
  81.     for(int i=0; i<10; ++i)
  82.     {
  83.         for(int j=0; j<10; ++j)
  84.         {
  85.             cout<<maze[i][j]<<" ";
  86.         }
  87.         cout<<endl;
  88.     }
  89. }

  90. bool Pass(Maze maze, Pos pos)
  91. {
  92.     return maze[pos.x][pos.y] == 0;
  93. }

  94. void FootMaze(Maze maze,Pos pos)
  95. {
  96.     maze[pos.x][pos.y] = 2;
  97. }

  98. Pos NextPos(Pos pos, DIR di)
  99. {
  100.     switch(di)
  101.     {
  102.     case RIGHT:
  103.         pos.y += 1;
  104.         break;
  105.     case DOWN:
  106.         pos.x += 1;
  107.         break;
  108.     case LEFT:
  109.         pos.y -= 1;
  110.         break;
  111.     case UP:
  112.         pos.x -= 1;
  113.         break;
  114.     }
  115.     return pos;
  116. }

  117. void MarkMaze(Maze maze, Pos pos)
  118. {
  119.     maze[pos.x][pos.y] = 4;
  120. }

  121. bool PassMaze(Maze maze,Pos start, Pos end)
  122. {
  123.     Man man; //pos di
  124.     Stack<Man> st;
  125.     Pos curpos = start;
  126.     do
  127.     {
  128.         if(Pass(maze,curpos))
  129.         {
  130.             man.pos = curpos;
  131.             man.di = RIGHT;
  132.             FootMaze(maze,man.pos);
  133.             if(curpos == end)
  134.                 return true;
  135.             st.push(man);
  136.             curpos = NextPos(man.pos,man.di);
  137.         }
  138.         else
  139.         {
  140.             if(!st.isempty())
  141.             {
  142.                 man = st.gettop();
  143.                 while(man.di==UP && !st.isempty())
  144.                 {
  145.                     st.pop();
  146.                     MarkMaze(maze,man.pos);
  147.                     if(!st.isempty())
  148.                     {
  149.                         man = st.gettop();
  150.                     }
  151.                     else
  152.                     {
  153.                         return false;
  154.                     }
  155.                     
  156.                 }
  157.                 if(man.di < UP)
  158.                 {
  159.                     st.pop();
  160.                     man.di = (DIR)(man.di+1);
  161.                     st.push(man);
  162.                     curpos = NextPos(man.pos,man.di);
  163.                 }
  164.             }
  165.         }
  166.     }while(!st.isempty());
  167.     return false;
  168. }

  169. void main()
  170. {
  171.     Pos start = {0,1};
  172.     Pos end = {9,8};
  173.     Maze maze =
  174.     {
  175.         {1,0,1,1,1,1,1,1,1,1},
  176.         {1,0,0,0,0,0,0,0,0,1},
  177.         {1,0,1,1,1,1,1,1,1,1},
  178.         {1,0,0,1,0,0,0,0,0,1},
  179.         {1,0,1,1,1,1,1,1,0,1},
  180.         {1,0,1,0,0,0,0,0,0,1},
  181.         {1,0,0,0,0,1,0,1,1,1},
  182.         {1,0,0,0,0,0,0,0,0,1},
  183.         {1,0,0,0,1,1,1,1,0,1},
  184.         {1,1,1,0,0,0,0,0,0,1}
  185.     };
  186.     ShowMaze(maze);
  187.     if(!PassMaze(maze,start,end))
  188.     {
  189.         cout<<"没有出路!"<<endl;
  190.         return;
  191.     }
  192.     cout<<"-------------------"<<endl;
  193.     ShowMaze(maze);

  194. }

阅读(1275) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~