Clean BFS C++ Code, do check out if stuck


#1
int Solution::snakeLadder(vector<vector<int> > &A, vector<vector<int> > &B){
    vector<vector<int>> grid(10, vector<int>(10));
    vector<vector<bool>> visited(10, vector<bool>(10, false));
    queue<int> q;
    int steps= 0;
    for(int i=0; i<10; i++){
        for(int j=0; j<10; j++){
            grid[i][j]=i*10+j+1;
        }
    }
    for(int i=0; i<A.size(); i++){
        int x= (A[i][0]-1)/10, y= (A[i][0]-1)%10;
        grid[x][y]= A[i][1];
    }
    for(int i=0; i<B.size(); i++){
        int x= (B[i][0]-1)/10, y= (B[i][0]-1)%10;
        grid[x][y]= B[i][1];
    }
    q.push(1);
    visited[0][0]= true;
    while(!q.empty()){
        int size= q.size();
        while(size--){
            int num= q.front();
            q.pop();
            if(num==10*10) return steps;
            for(int i=1; i<=6; i++){
                if(num+i>10*10) break;
                int r= (num+i-1)/10;
                int c= (num+i-1)%10;
                if(r>=0 && c>=0 && r<10 && c<10 && !visited[r][c]){
                    q.push(grid[r][c]);
                    visited[r][c]=true;
                }
            }
        }
        steps++;
    }
    return -1;
}