Simple BFS C++ solution!


#1
int Solution::snakeLadder(vector<vector<int> > &A, vector<vector<int> > &B) {
    unordered_map<int,int>adj;
    for(auto ladder:A){
        adj[ladder[0]]=(ladder[1]);
    }
    for(auto snake:B){
        adj[snake[0]]=(snake[1]);
    }
    vector<int>vis(101,-1);
    queue<int>q;
    q.push(1);
    vis[1]=0;
    while(q.size()){
        int node=q.front();
        q.pop();
        if(node==100)return vis[100];
        for(int i=1;i<=6;i++){
            int child=node+i;
            if(child>100)continue;
            if(adj.find(child)!=adj.end())child=adj[child];
            if(vis[child]==-1){
                vis[child]=vis[node]+1;
                q.push(child);
            }
        }
    }
    return -1;
}