Advertisement
Nyvin

pathfinding

Dec 13th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. struct field
  2. {
  3.     field(int x, int y) : _x( x ),_y( y ), _c(createChar( x,y )), distance(99999999){}
  4.     field() {}
  5.     int _x;
  6.     int _y;
  7.     char _c;
  8.     int distance;
  9. };
  10.  
  11.  
  12. void move(field* f, std::vector<std::vector<field>>& m, std::queue<field*>& q)
  13. {
  14.     //up
  15.     if(f->_y - 1 >= 0)
  16.     {
  17.         if(m.at(f->_y - 1).at(f->_x)._c != '#')
  18.         {
  19.             auto tmp = &m.at(f->_y - 1).at(f->_x);
  20.             tmp->_c = 'O';
  21.             if (f->distance + 1 < tmp->distance)
  22.             {
  23.                 tmp->distance = f->distance + 1;
  24.                 q.push(tmp);
  25.             }
  26.         }
  27.     }
  28.     //down
  29.     if (f->_y + 1 < m.size())
  30.     {
  31.         if (m.at(f->_y + 1).at(f->_x)._c != '#')
  32.         {
  33.             auto tmp = &m.at(f->_y + 1).at(f->_x);
  34.             tmp->_c = 'O';
  35.             if (f->distance + 1 < tmp->distance)
  36.             {
  37.                 tmp->distance = f->distance + 1;
  38.                 q.push(tmp);
  39.             }
  40.         }
  41.     }
  42.     //right
  43.     if (f->_x + 1 < m.at(f->_y).size())
  44.     {
  45.         if (m.at(f->_y).at(f->_x + 1)._c != '#')
  46.         {
  47.             auto tmp = &m.at(f->_y).at(f->_x + 1);
  48.             tmp->_c = 'O';
  49.             if (f->distance + 1 < tmp->distance)
  50.             {
  51.                 tmp->distance = f->distance + 1;
  52.                 q.push(tmp);
  53.             }
  54.         }
  55.     }
  56.     //left
  57.     if (f->_x - 1 >= 0)
  58.     {
  59.         auto tmp = &m.at(f->_y).at(f->_x - 1);
  60.         if (tmp->_c != '#')
  61.         {
  62.             tmp->_c = 'O';
  63.             if (f->distance + 1 < tmp->distance)
  64.             {
  65.                 tmp->distance = f->distance + 1;
  66.                 q.push(tmp);
  67.             }
  68.         }
  69.     }
  70. }
  71.  
  72. void PathFinding(std::vector<std::vector<field>>& m)
  73. {
  74.     field position(1, 1);
  75.     field end(31, 39);
  76.     m.at(1).at(1).distance = 0;
  77.     m.at(1).at(1)._c = 'O';
  78.     std::queue<field*> q;
  79.     q.push(&m.at(1).at(1));
  80.     while(!q.empty())
  81.     {
  82.         move(q.front(), m, q);
  83.         q.pop();
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement