標籤:

迷宮 待添加

#ifndef MAZE_H#define MAZE_H#include <list.h>#include <stdio.h>#include <SeqStack.h>//#include <iostream>//統一統一,一律認為(0,0)在左上角,且0算橫坐標和縱坐標,向右,i++;向下,j++using namespace std;//七行七列//通道塊class PostType{public: int i;//橫坐標,從左上角看,00 int j; PostType(int mi = 0,int mj = 0){ i=mi;j=mj; }// copy(PostType& m){// this->i=m.i;// this->j=m.j;// }};//棧元素class SElemType : public PostType {public: int curstep;//通道塊在路徑上的路徑序號 PostType current;//現在這個通道塊 int di;//方向初始值是0,上1下2左3右4,當前前進方向 SElemType(int curnum=0,int d=0,PostType curone=(0,0)){ curstep=curnum; current=curone; di=d; }};class Maze{public: int maze[7][7]= {//這個題就不用加什麼外框了,0可以走,1不行 {1,1,1,1,1,1,1}, {1,0,0,0,0,0,1}, {1,0,1,1,0,1,1}, {1,0,1,0,0,0,1}, {1,0,0,0,1,0,1}, {1,0,1,1,0,0,1}, {1,1,1,1,1,1,1} }; //全public得了 PostType curpos;//當前塊 SeqStack<SElemType> stack; int curstep=0; Maze(){cout<<"ok.one made"<<endl;} bool Pass(PostType curpos)//判斷當前塊是否可通過,檢查01 { if(maze[curpos.i][curpos.j]==0) return true; return false; } void FootPrint(PostType curpos){ maze[curpos.i][curpos.j]=2; ///////////////////////////////////////// //打上路徑標記2 } void MarkPrint(PostType curpos){ maze[curpos.i][curpos.j]=3; //打上死路標記3 } PostType NextPos(PostType curpos,int di){ PostType s=curpos; switch(di){ //上1下2左3右4 //一律認為(0,0)在左上角,且0算橫坐標和縱坐標,向右,i++;向下,j++ case 1:s.j--;break; case 2:s.j++;break; case 3:s.i--;break; case 4:s.i++;break; default:break; } return s; } bool MazePath(PostType start,PostType end){ //我不按ppt上走了,就是自己,不加MazeType maze了 curpos=start; curstep=1;//這個只是個計數的;do{ if(Pass(curpos)){ FootPrint(curpos); SElemType temp(curstep,1,curpos); stack.Push(temp); if(curpos.i==end.i&&curpos.j==end.j) return true;//到終點 curpos=NextPos(curpos,1); curstep++;//沒到終點,探索下一步 } else { SElemType temp; if(!stack.IsEmpty()){ //沒空 stack.Pop(temp); while(temp.di==4&&!stack.IsEmpty()){ MarkPrint(temp.current);//死路 stack.Pop(temp); } if(temp.di<4){ temp.di++; stack.Push(temp); curpos=NextPos(temp.current,temp.di); } } } }while(!stack.IsEmpty());return false;}};#endif // MAZE_H

推薦閱讀:

linux忘記root密碼怎麼辦?
[B0] Python基礎
學習計算機,從抽象到具體,再到抽象
1-10 轉CS的優勢之一:前置條件少
愚人節,說說假新聞和謠言,它們傳播得比真相更快

TAG:計算機科學 |