View Code
1 #include <iostream> 2 3 using namespace std; 4 5 const int SIZE_X=10; 6 const int SIZE_Y=10; 7 8 //data struct 9 class mPoint 10 { 11 public: 12 mPoint(int rx=0,int ry=0,bool rcan_move_to=false) 13 { 14 x=rx; 15 y=ry; 16 can_move_to=rcan_move_to; 17 next=NULL; 18 } 19 public: 20 mPoint *next; 21 int x; 22 int y; 23 bool can_move_to; 24 }; 25 26 //function declaration 27 class mStack 28 { 29 public: 30 mStack();//constructor 31 int push(mPoint point); 32 mPoint pop(); 33 int getLength(); 34 mPoint getTop(); 35 void printStack(); 36 private: 37 mPoint *base;//base pointer 38 mPoint *top; //top pointer 39 int length; //length of stack 40 }; 41 42 43 mStack::mStack() 44 { 45 length=0; 46 base=NULL; 47 top=NULL; 48 } 49 int mStack::push(mPoint point) 50 { 51 mPoint *mpNode=new mPoint(); 52 *mpNode=point; 53 if(length==0) 54 top=base=mpNode; 55 else 56 { 57 top->next=mpNode; 58 top=mpNode; 59 } 60 return ++length; 61 } 62 mPoint mStack::getTop() 63 { 64 return *top; 65 } 66 mPoint mStack::pop() 67 { 68 if(length<=0) 69 return NULL; 70 mPoint retPoint=*top; 71 top=base; 72 while(top->next!=NULL) 73 { 74 if(top->next->next==NULL) 75 { 76 delete(top->next); 77 top->next=NULL; 78 break; 79 } 80 top=top->next; 81 } 82 if(length==1) 83 { 84 delete(base); 85 base=top=NULL; 86 } 87 length--; 88 return retPoint; 89 } 90 91 int mStack::getLength() 92 { 93 return length; 94 } 95 void mStack::printStack() 96 { 97 mPoint *p=base; 98 while(p!=NULL) 99 { 100 cout<<"("<<p->x<<","<<p->y<<")"<<endl; 101 p=p->next; 102 } 103 104 } 105 106 int main() 107 { 108 mPoint mpArray[SIZE_X][SIZE_Y]; 109 bool initArray[SIZE_X][SIZE_Y]={ 110 {false,false,false,false,false,false,false,false,false,false}, 111 {false,true ,true ,false,true ,true ,true ,false,true ,false}, 112 {false,true ,true ,false,true ,true ,true ,false,true ,false}, 113 {false,true ,true ,true ,true ,false,false,true ,true ,false}, 114 {false,true ,false,false,false,true ,true ,true ,true ,false}, 115 {false,true ,true ,true ,false,true ,true ,true ,true ,false}, 116 {false,true ,false,true ,true ,true ,false,true ,true ,false}, 117 {false,true ,false,false,false,true ,false,false,true ,false}, 118 {false,false,true ,true ,true ,true ,true ,true ,true ,false}, 119 {false,false,false,false,false,false,false,false,false,false}};//迷宫矩阵 120 for(int i=0;i<SIZE_X;i++)//init 121 { 122 for(int j=0;j<SIZE_Y;j++) 123 { 124 mpArray[i][j].x=i; 125 mpArray[i][j].y=j; 126 mpArray[i][j].can_move_to=initArray[i][j]; 127 } 128 } 129 mPoint startp(1,1,true);//entry 130 mPoint endp(8,8,true); //exit 131 132 mStack mpath; 133 mpath.push(startp); 134 135 mPoint mp=startp; 136 while(true) 137 { 138 if(mp.x==endp.x && mp.y==endp.y) 139 break;//success 140 if(mpArray[mp.x+1][mp.y].can_move_to)//search down 141 { 142 mpArray[mp.x+1][mp.y].can_move_to=false; 143 mpath.push(mPoint(mp.x+1,mp.y)); 144 mp=mpArray[mp.x+1][mp.y]; 145 continue; 146 } 147 if(mpArray[mp.x-1][mp.y].can_move_to)//search up 148 { 149 mpArray[mp.x-1][mp.y].can_move_to=false; 150 mpath.push(mPoint(mp.x-1,mp.y)); 151 mp=mpArray[mp.x-1][mp.y]; 152 continue; 153 } 154 if(mpArray[mp.x][mp.y+1].can_move_to)//search right 155 { 156 mpArray[mp.x][mp.y+1].can_move_to=false; 157 mpath.push(mPoint(mp.x,mp.y+1)); 158 mp=mpArray[mp.x][mp.y+1]; 159 continue; 160 } 161 if(mpArray[mp.x][mp.y-1].can_move_to)//serch left 162 { 163 mpArray[mp.x][mp.y-1].can_move_to=false; 164 mpath.push(mPoint(mp.x,mp.y-1)); 165 mp=mpArray[mp.x][mp.y-1]; 166 continue; 167 } 168 if(0==mpath.getLength()) 169 { 170 cout<<"No path!"<<endl; 171 return -1; 172 } 173 mpath.pop(); 174 mp=mpath.getTop(); 175 } 176 cout<<"Path:"<<endl; 177 mpath.printStack();//ouput path 178 179 return 0; 180 }