Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void path_finding(int x,int y){
- vector<vector<int> >used;
- vector<vector<CellType> > cells = world.getCells();
- vector<Trooper>troopers = world.getTroopers();
- for(int i = 0; i<world.getWidth(); i++){
- vector<int> temp;
- for(int j=0; j<world.getHeight(); j++)
- temp.push_back(-1);
- used.push_back(temp);
- }
- vector<pair<int,int> > oldwave,wave;
- const int dy[]={0,1,0,-1},dx[]={1,0,-1,0};
- int step=0,nx=0,ny=0;
- used.at(self.getX()).at(self.getY())=step;
- oldwave.push_back(make_pair(self.getX(),self.getY()));
- while(!oldwave.empty()){
- wave.clear();
- ++step;
- for(vector<pair<int,int> >::iterator i = oldwave.begin();i!=oldwave.end();i++){
- for(int d=0;d<4;++d){
- int nx=i->first+dx[d];
- int ny=i->second+dy[d];
- if(nx==-1 || ny==-1
- || nx==30
- || ny==20
- || used.at(nx).at(ny)!=-1)continue;
- if(cells[nx][ny]==FREE && used[nx][ny]!=WALL ||(nx==x&&ny==y)){
- wave.push_back(make_pair(nx,ny));
- used.at(nx).at(ny)=step;
- }
- if(nx==x&&ny==y)
- goto done;
- }
- }
- oldwave=wave;
- }
- done:
- wave.clear();
- while(used[x][y]!=1 && used[x][y]!=-1){
- for(int d=0;d<4;++d){
- int nx=x+dx[d],ny=y+dy[d];
- if(nx==-1 || ny==-1
- || nx==30
- || ny==20
- || cells[nx][ny]!=FREE
- || used.at(nx).at(ny) == WALL)continue;
- if(used.at(nx).at(ny) < used[x][y]){
- wave.push_back(make_pair(nx,ny));
- x=nx;y=ny;
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement