待走迷宫问题(续)

  

我的地图文件(MazeMap。txt)如下:

大小:(a)   1,1,1,1,1,1,1,1,1,1,   1,1,1,1,1,1,1,1,1,1   0,0,0,1,1,1,1,1,1,1   1,1,0,1,1,1,1,1,1,1   1,1,0,1,1,1,1,1,1,1   1,1,0,1,1,1,1,1,1,1   1,1,0,0,0,0,0,0,1,1   1,1,0,1,1,1,1,0,1,1   1,1,0,1,1,1,1,0,1,1   1,1,0,1,1,1,1,0,1,1


下面的pos类用来保存某个位置的坐标

GetMaze函数用来判断地图格式的合法性,若合法则读取地图内容,并将内容存入参数arr所指向的内存中。

struct  pos   {   pos (int  row =, 0,, int  col =, 0)   :_row(行)   _col (col)   {}   int  _row;   int  _col;   };      void  GetMaze (arr int  *,,, int 及深圳,,int ,,, int ,上校)   {   FILE  * fout =, fopen (“MazeMap.txt”,“r”);   断言(输出端);   char  * title =, new  char [40];   title =, fgets(标题,7日,输出端);   if  (strcmp(标题,“大小:("))   {   cout  & lt; & lt;,“地图文件错误!”,& lt; & lt;, endl;   throw  1;   }   row =,获取(输出端),安康;87;   title =, fgets(标题,,2,,输出端);   if  (strcmp(标题,,","))   {   cout  & lt; & lt;,“地图文件错误!”,& lt; & lt;, endl;   throw  1;   }   col =,获取(输出端),安康;87;   时间=arr  new  int [row  *,坳];   时间=sz  row  *,上校;   title =, fgets(标题,,2,,输出端),,,   for  (int 小姐:=,0;,小姐:& lt;,深圳;,我)   {   char  ch =,获取(输出端);   if  (ch  !=,的,,,,,ch  !=, ' \ n ',,,, ch  !=, ' \ 0 ')   {   * (+ arr  i),=, ch 作用;“0”;   我+ +;   }   }   }

<强>

<强>

bool  MazePath (int  * arr, int  n, const  pos ,,, stack,和路径),,,//假设下边沿为迷宫的出口   {   pos  cur =,入口;   path.push(坏蛋);   while  (! path.empty ())   {   + * (arr  n  *, (cur._row) + cur._col),=, 2;   if  (cur._row ==, n 作用;1)   {   return 真实;   }//向下   if    ((cur._row  +, 1, & lt;, 10),,,, (* (+ arr  n  *,(时间+ cur._row  1), +, cur._col),==, 0)),   {   + * (arr  n  *,(时间+ cur._row  1), +, cur._col),=, 2;   + + cur._row;   path.push(坏蛋);   继续;   }//向上   if    ((cur._row 作用;1,祝辞=,0),,,,(* (+ arr  n  *, (cur._row 安康;1),+,cur._col),==, 0)),,   {   + * (arr  n  *, (cur._row 安康;1),+,cur._col),=, 2;   ——cur._row;   path.push(坏蛋);   继续;   }//向左   if    ((cur._col 作用;1,祝辞=,0),,,,(* (+ arr  n  *, cur._row  +, cur._col 作用;1),==,0)),,,,,   {   + * (arr  n  *, cur._row  +, cur._col 作用;1),=,2;   ——cur._col;   path.push(坏蛋);   继续;   }//向右   if  ((cur._col  +, 1, & lt;, 10),,,, (* (+ arr  n  *, cur._row  +, cur._col  +, 1),==, 0)),,,,,,   {   + * (arr  n  *, cur._row  +, cur._col  +, 1),=, 2;   + + cur._col;   path.push(坏蛋);   继续;   }//走不通   时间=cur._col  path.top () ._col;   时间=cur._row  path.top () ._row;   path.pop ();   }   }




<强>

template  & lt; typename  T>   void  ClearPath (stack,,栈)   {   while  (! stack.empty ())   {   stack.pop ();   }   }      static  void  SaveBestPath (stack,,,, vector<, stack,祝辞,path_vec)   {   stack待走迷宫问题(续)