SOURCE

var minPushBox = function(grid){
    var m = grid.length;
    var n = grid[0].length;
    var sx =-1,sy = -1, bx = -1, by = -1,tx = -1, ty = -1;
    for(let i = 0; i < m; i++){
        for(let j = 0; j < n; j++){
           if(grid[i][j] === 'S'){
               sx = i;
               sy = j;
           }else if(grid[i][j] === 'B'){
               bx = i;
               by = j;
           }else if(grid[i][j] === 'T'){
               tx = i;
               ty = j;
           }
        }
    }
    console.log(`人物坐标:${sx}, ${sy}`);
    console.log(`箱子坐标:${bx}, ${by}`);
    console.log(`目标坐标:${tx}, ${ty}`);

    var dirs = [[0,1],[0,-1],[1,0],[-1,0]];
    var queue = [];
    var step = 0;
    queue.push([bx,by]);
    while(queue.length){
        if(step == 20){
            return ;
        }
        let queue1 = [];
        console.log('queue', queue);   
        for(let i =  queue.length -1; i >= 0; i--){
            let arr = queue.shift();
            for(let i = 0; i < dirs.length; i++){
                let sx1 = arr[0] + dirs[i][0];
                let sy1 = arr[1] + dirs[i][1];
                if(sx1 < 0 || sx1 > m || sy1 > n || grid[sx1][sy1] === '#'){
                    continue;
                }
                let bx1 = arr[0] - dirs[i][0];
                let by1 = arr[1] - dirs[i][1];
                if(grid[bx1][by1] === 'T'){
                    console.log('step', step + 1);
                    return false;
                }
                if(!ok(grid, m, n , bx1, by1)){
                    continue;
                }
            
                let a = [bx1, by1];
                queue1.push(a);
            }
        }
        queue = queue1.concat([]);
        step++;
    }  
};


const ok = (grid, m, n, x, y) => { // 不越界且不在墙上
    return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] !== '#';
};


var grid =  [["#","#","#","#","#","#"],
             ["#","T","#","#","#","#"],
             ["#",".",".","B",".","#"],
             ["#",".","#","#",".","#"],
             ["#",".",".",".","S","#"],
             ["#","#","#","#","#","#"]];
minPushBox(grid);
console 命令行工具 X clear

                    
>
console