Sortest and simple bfs solution


#1

int Solution::snakeLadder(vector<vector > &A, vector<vector > &B)
{
int x,y,c,id;
queue<pair<int,int> > q;
map<int,bool> mp;
q.push({0,0});
while(!q.empty())
{
int num=q.front().first;
c=q.front().second;q.pop();
if(num==99) return c;
if(num>99 || mp[num]) continue;
mp[num]=true;
id=-1;
for(int i=0;i<A.size();i++)
if(A[i][0]-1==num) {id=A[i][1]-1;break;}
if(id!=-1) {q.push({id,c});continue;}
id=-1;
for(int i=0;i<B.size();i++)
if(B[i][0]-1==num) {id=B[i][1]-1;break;}
if(id!=-1) {q.push({id,c});continue;}
q.push({num+6,c+1});q.push({num+5,c+1});q.push({num+4,c+1});
q.push({num+3,c+1});q.push({num+2,c+1});q.push({num+1,c+1});
}
return -1;
}