Advertisement
Guest User

Untitled

a guest
Nov 20th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. void path_finding(int x,int y){
  2.  
  3. vector<vector<int> >used;
  4. vector<vector<CellType> > cells = world.getCells();
  5. vector<Trooper>troopers = world.getTroopers();
  6.  
  7. for(int i = 0; i<world.getWidth(); i++){
  8.     vector<int> temp;  
  9.     for(int j=0; j<world.getHeight(); j++)
  10.         temp.push_back(-1);
  11.     used.push_back(temp);
  12. }
  13.     vector<pair<int,int> > oldwave,wave;
  14.     const int dy[]={0,1,0,-1},dx[]={1,0,-1,0};
  15.     int step=0,nx=0,ny=0;
  16.     used.at(self.getX()).at(self.getY())=step;
  17.     oldwave.push_back(make_pair(self.getX(),self.getY()));
  18.  
  19.     while(!oldwave.empty()){
  20.         wave.clear();
  21.         ++step;
  22.         for(vector<pair<int,int> >::iterator i = oldwave.begin();i!=oldwave.end();i++){
  23.             for(int d=0;d<4;++d){
  24.                 int nx=i->first+dx[d];
  25.                 int ny=i->second+dy[d];
  26.                 if(nx==-1 || ny==-1
  27.                     || nx==30
  28.                     || ny==20
  29.                     || used.at(nx).at(ny)!=-1)continue;
  30.                 if(cells[nx][ny]==FREE && used[nx][ny]!=WALL ||(nx==x&&ny==y)){
  31.                     wave.push_back(make_pair(nx,ny));
  32.                     used.at(nx).at(ny)=step;
  33.                 }
  34.                 if(nx==x&&ny==y)
  35.                     goto done;
  36.             }
  37.         }
  38.         oldwave=wave;
  39.     }
  40. done:
  41.     wave.clear();
  42.  
  43.     while(used[x][y]!=1 && used[x][y]!=-1){
  44.         for(int d=0;d<4;++d){
  45.             int nx=x+dx[d],ny=y+dy[d];
  46.             if(nx==-1 || ny==-1
  47.                 || nx==30
  48.                 || ny==20
  49.                 || cells[nx][ny]!=FREE
  50.                 || used.at(nx).at(ny) == WALL)continue;
  51.             if(used.at(nx).at(ny) < used[x][y]){
  52.                 wave.push_back(make_pair(nx,ny));
  53.                 x=nx;y=ny;
  54.                 break;
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement