Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct field
- {
- field(int x, int y) : _x( x ),_y( y ), _c(createChar( x,y )), distance(99999999){}
- field() {}
- int _x;
- int _y;
- char _c;
- int distance;
- };
- void move(field* f, std::vector<std::vector<field>>& m, std::queue<field*>& q)
- {
- //up
- if(f->_y - 1 >= 0)
- {
- if(m.at(f->_y - 1).at(f->_x)._c != '#')
- {
- auto tmp = &m.at(f->_y - 1).at(f->_x);
- tmp->_c = 'O';
- if (f->distance + 1 < tmp->distance)
- {
- tmp->distance = f->distance + 1;
- q.push(tmp);
- }
- }
- }
- //down
- if (f->_y + 1 < m.size())
- {
- if (m.at(f->_y + 1).at(f->_x)._c != '#')
- {
- auto tmp = &m.at(f->_y + 1).at(f->_x);
- tmp->_c = 'O';
- if (f->distance + 1 < tmp->distance)
- {
- tmp->distance = f->distance + 1;
- q.push(tmp);
- }
- }
- }
- //right
- if (f->_x + 1 < m.at(f->_y).size())
- {
- if (m.at(f->_y).at(f->_x + 1)._c != '#')
- {
- auto tmp = &m.at(f->_y).at(f->_x + 1);
- tmp->_c = 'O';
- if (f->distance + 1 < tmp->distance)
- {
- tmp->distance = f->distance + 1;
- q.push(tmp);
- }
- }
- }
- //left
- if (f->_x - 1 >= 0)
- {
- auto tmp = &m.at(f->_y).at(f->_x - 1);
- if (tmp->_c != '#')
- {
- tmp->_c = 'O';
- if (f->distance + 1 < tmp->distance)
- {
- tmp->distance = f->distance + 1;
- q.push(tmp);
- }
- }
- }
- }
- void PathFinding(std::vector<std::vector<field>>& m)
- {
- field position(1, 1);
- field end(31, 39);
- m.at(1).at(1).distance = 0;
- m.at(1).at(1)._c = 'O';
- std::queue<field*> q;
- q.push(&m.at(1).at(1));
- while(!q.empty())
- {
- move(q.front(), m, q);
- q.pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement