现在的位置: 首页 > 综合 > 正文

迷宫寻找路径

2014年01月21日 ⁄ 综合 ⁄ 共 861字 ⁄ 字号 评论关闭

 /*在一个15*15的二位哦0 1数组中寻找一条可以从(1,1)到达(14,13)的路径;

最终路径保存在thePath的vector向量中*/

bool findPath(int *mapArray){

 stack<position> path;
 vector<position> thePath;
 int temp[15][15];
 for(int i=0;i<15;i++){

  for(int j=0;j<15;j++){

   temp[i][j]=mapArray[i][j];
  }
 }

 position a,move[4];
 a.i=1;
 a.j=1;
 
 move[0].i=0;
 move[0].j=-1;
 move[1].i=0;
 move[1].j=1;
 move[2].i=-1;
    move[2].j=0;
 move[3].i=1;
 move[3].j=0;

 path.push(a);
 temp[0][1]=1;

 while(a.i!=14||a.j!=13){
  int z;
  for(z=0;z<4;z++){
   if(!temp[a.i+move[z].i][a.j+move[z].j]){

    a.i+=move[z].i;
    a.j+=move[z].j;
    break;
   }  
   
  }
  if(z==4){
   if(path.empty()) return false;
    path.pop();

  a=path.top();

  }else{
   path.push(a);
   temp[a.i][a.j]=1;

  }
  

 

 }

 cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;

 while(!path.empty()){
  a=path.top();
  path.pop();
  cout<<a.i<<" "<<a.j<<endl;
  thePath.push_back(a);
 }

 

   

}

抱歉!评论已关闭.