Advertisement
Guest User

Untitled

a guest
Jun 15th, 2013
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4.  
  5. using namespace std;
  6.  
  7. class Cell{
  8.     int x;
  9.     int y;
  10. public:
  11.     int getx(){return x;}
  12.     int gety(){return y;}
  13.     void setx(int x){this->x=x;}
  14.     void sety(int y){this->y=y;}
  15.    
  16.     bool operator==(Cell o){return x==o.x && y==o.y;}
  17.     Cell operator=(Cell o){x=o.x; y=o.y; return *this;}
  18.    
  19.     Cell(int x,int y):x(x),y(y){}
  20.     Cell():x(0),y(0){}
  21. };
  22. vector<Cell> getShortestPath(Cell ori, Cell dest, int array[], int width, int height);
  23.  
  24.  
  25.  
  26. int main(){
  27.     int ejemplo[]={
  28.         0,1,0,1,0,0,0,0,        //0: void
  29.         0,1,0,1,0,0,0,0,        //1: obstacle
  30.         0,1,0,1,0,0,0,0,
  31.         0,1,0,1,0,0,0,0,
  32.         0,1,0,1,0,0,0,0,
  33.         0,1,0,1,0,0,0,0,
  34.         0,0,0,1,0,0,0,0,
  35.         0,0,0,0,0,0,0,0};
  36.        
  37.     vector<Cell> camino= getShortestPath(Cell(0,0),Cell(7,0),ejemplo,8,8);
  38.     for(int i=0;i<camino.size();i++){
  39.         cout<<"("<<camino[i].getx()<<", "<<camino[i].gety()<<")"<<endl;
  40.     }
  41.    
  42. }
  43.  
  44. vector<Cell> getShortestPath(Cell ori, Cell dest, int array[], int width, int height){
  45.    
  46.     if(ori==dest) return vector<Cell>();
  47.     unsigned int *sizes=new unsigned int[width*height];
  48.     Cell *prev=new Cell[width*height];
  49.     for(int i=0;i<width*height;i++){sizes[i]=-1; prev[i]=Cell(-1,-1);}
  50.    
  51.     sizes[ori.getx()+ori.gety()*width]=0;
  52.     prev[ori.getx()+ori.gety()*width]=ori;
  53.  
  54.    
  55.     queue<Cell> porVisitar;
  56.     porVisitar.push(ori);
  57.    
  58.     while(!porVisitar.empty()){
  59.         Cell cur=porVisitar.front();
  60.         porVisitar.pop();
  61.         cout<<porVisitar.size()<<endl;
  62.         for(int i=-1;i<2;i++)
  63.         for(int j=-1;j<2;j++)
  64.         if( (cur.getx()+j)>=0 && (cur.getx()+j)<width && (cur.gety()+i)>=0 && (cur.gety()+i)<height &&      //is not out of bounds
  65.             array[(cur.getx()+j)+(cur.gety()+i)*width]==0 &&            //is not an obstable
  66.             sizes[cur.getx()+cur.gety()*width]+1 < sizes[(cur.getx()+j)+(cur.gety()+i)*width]       //there is not a better path
  67.         ){
  68.             sizes[(cur.getx()+j)+(cur.gety()+i)*width]=sizes[cur.getx()+cur.gety()*width]+1;
  69.             prev[(cur.getx()+j)+(cur.gety()+i)*width]=Cell(cur.getx(),cur.gety());
  70.             porVisitar.push(Cell(cur.getx()+j,cur.gety()+i));
  71.         }
  72.  
  73.     }
  74.    
  75.     if(prev[dest.getx()+dest.gety()*width]==Cell(-1,-1)) return vector<Cell>();
  76.    
  77.     Cell pp=dest;
  78.     vector<Cell> res(sizes[dest.getx()+dest.gety()*width]+1);
  79.     for(int i=res.size()-1; !(pp==ori); i--){
  80.         res[i]=pp;
  81.         pp=prev[pp.getx()+pp.gety()*width];
  82.     }
  83.    
  84.     return res;
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement