0

Hello this is the code "Chess knight problem" where knight has the start and end points - x,y on the chessboard. The first problem was to find the shortest path from the source to the destination point. I used bfs algorithm to solve it and it worked, but then I tried to modify algorithm in order to get the full path from s to d. The path itself stores in the "node" structure. My problem that I cannot pass vector<pair<int,int>> parameter to the next step in bfs

q.push( {_x,_y, dist+1, path.push_back(make_pair(_x,_y)) } );

on this line I have an error when I try to pass parameters to the next node, but after removing the third parameter "path.push_back..." it works - how to pass it? thank you.

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<queue>


using namespace std;


struct node{
    int x,y,dist;

    vector<pair<int,int>> path;

    bool operator==(const node& p)const{
        return x == p.x && y == p.y;
    }

    bool operator<(const node& p)const{
        return x < p.x ||(x == p.x && y < p.y);
    }
};


bool is_ok(int x, int y){

    return (x > 0 && y > 0 && x < 8 && y < 8);
}

pair<int, vector<pair<int,int>>> bfs(node s, node d){

    queue<node> q;
    q.push(s);

    map<node, bool> visited;
    vector<int> col = {1,1,2,2,-2,-2,-1,-1};
    vector<int> row = {2,-2,1,-1,1,-1,2,-2};

    while(!q.empty()){

        node u = q.front(); q.pop();

        int dist = u.dist;
        vector<pair<int,int>> path(u.path.begin(), u.path.end());

        if(u.x = d.x && u.y == d.y)
            return make_pair(dist, path);

        if(!visited.count(u)){

            visited[u] = true;
            for(int i = 0; i < 8; i ++){

                int _x = u.x + col[i];
                int _y = u.y + row[i];

                if (is_ok(_x,_y))

                    q.push( {_x,_y, dist+1, path.push_back(make_pair(_x,_y)) } );


            }

        }

    }

}

int main(){



return 0;
}
  • 1
    Two problems. a) `vector::push_back()` returns `void`. It is meaningless to use the result of `push_back` anywhere, since `push_back` doesn't return any result. b) `node` has three members - why are you trying to initialize it with four values? Where is the fourth one supposed to go? Perhaps you meant two separate statements: `if (is_ok(_x,_y)) { path.push_back(...); q.push({_x, _y, dist+1}); }` – Igor Tandetnik Sep 10 '17 at 11:57
  • Hello. I can't do like that because I need add pair only once for all successor steps of BFS, but in your case - for the first successor it adds paid once, for the second I will add the same thing twice ( because it extends the given parameter once more) - but I don't need this repeating. – Suleyman Suleymanzade Sep 10 '17 at 17:52
  • 1
    Something like this, maybe: `{ Node node{_x,_y, dist+1, path}; node.path.push_back({_x, _y}); q.push(node); }` I'm not sure I understand what you are trying to do, but this should give you enough of the pieces to put a solution together. – Igor Tandetnik Sep 10 '17 at 17:59
  • Thank you very much ! Now it works – Suleyman Suleymanzade Sep 10 '17 at 18:25

0 Answers0