为了账号安全,请及时绑定邮箱和手机立即绑定

【请大牛指教一二】深度优先搜索寻找路线,想打印所有路线的时候出错

【请大牛指教一二】深度优先搜索寻找路线,想打印所有路线的时候出错

moyemoji 2018-07-26 13:14:37
//ma是迷宫 //mb用来标记走过的路mb.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; i++) {                         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 (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);
查看完整描述

1 回答

?
Caballarii

TA贡献1123条经验 获得超629个赞

最后结果对不对我不知道,首先你定义的ma变量就写错了,数组最后一个元素少了左中括号[

查看完整回答
反对 回复 2018-07-26
  • 1 回答
  • 0 关注
  • 1676 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信