Snake Ladder Problem!


#1

please help me fixing the bug in this :frowning:

//code
int Solution::snakeLadder(vector<vector > &A, vector<vector > &B) {
map<int,int> lad;
vector visited(101,0);
for(int i=0;i<A.size();i++){
int start=A[i][0];
int dest=A[i][1];
lad[start]=dest;
}

map<int,int> snake;
for(int i=0;i<B.size();i++){
    int start=B[i][0];
    int dest=B[i][1];
    snake[start]=dest;
}

int moves=0;
queue<int> q;
q.push(1);
visited[1]=true;
bool found=false;

while(!q.empty() and !found){
    int s=q.size();
    while(s--){
       int t=q.front();
       q.pop();
       for(int d=1;d<=6;d++){
           if(t+d==100)
           {
               found=true;
                
           };
           else if(t+d<=100 && lad[t+d] && !visited[lad[t+d]]){
               visited[lad[t+d]]=true;
               if(lad[t+d]==100)found=true;
               q.push(lad[t+d]);
               
           }
           
           else if(t+d<=100 && snake[t+d] && !visited[snake[t+d]]){
               visited[snake[t+d]]=true;
                if(snake[t+d]==100)found=true;
               q.push(snake[t+d]);
           }
           else if(t+d<=100 && !visited[lad[t+d]] && !lad[t+d] 
           && !snake[t+d]){
               visited[t+d]=1;
               q.push(t+d);
           }
           
       }
    }
    moves++;

}

if(found){
return moves;
}else{
return -1;
}

}


#2

in the last else if case make !visited[t+d] instead of !visited[lad[t+d]]