[ask Daniel for advice one or two] depth first search to find the route to the specified location, trying to print out all the routes made an error.

I want to output all possible routes, but in the end, I only output one route. I haven"t figured out Orz

for a long time.

the following is the source code:

//ma
//mbmb.fill(0);
//0
//1
//(x0,y0)
//position=[]
//
//direction=[[-1,0],[0,-1],[1,0],[0,1]];
//destination=[x0,y0];
 
let ma = [[0, 0, 1, 0],
          [0, 0, 0, 0],          
          [0, 0, 1, 0],          
          [0, 1, 0, 0],          
          [0, 0, 0, 1]];
 
let mb = [[1, 0, 0, 0],          
          [0, 0, 0, 0],          
          [0, 0, 0, 0],          
          [0, 0, 0, 0],          
          [0, 0, 0, 0]];
 
let position = new Array(15);
position[0] = [0, 0];
let direction = [[-1, 0], [0, -1], [1, 0], [0, 1]];
let destination = [3, 2];
 
function maze(step) {    
    //    
    if (position[step][0] === destination[0] && position[step][1] === destination[1]) {       
        console.log(position);        
        return;    
    }
     
    for (let i = 0; i < 4; iPP) {                
        let new_x = position[step][0] + direction[i][0];        
        let new_y = position[step][1] + direction[i][1]; 
                       
        if (new_x < 0 || new_x > 4 || new_y < 0 || new_y > 3) {            
            continue;        
        }
         
        if (ma[new_x][new_y] === 0 && mb[new_x][new_y] === 0) {           
            position[step + 1] = [new_x, new_y];           
            mb[new_x][new_y] = 1;         
            maze(step + 1);           
            mb[new_x][new_y] = 0;       
        }  
    }    
    return;
}
                   
maze(0);
Mar.31,2021

(I don't know js) doesn't see it. But why do you judge that the sentence of ma doesn't seem to have.. You can output the intermediate process and see for yourself

Menu