深度遍历--广度遍历

深度遍历(DFS)
遍历完父节点的所有子节点的子节点的子节点...再遍历其兄弟节点。

const DFS = {
    nodes: [],
    do (root) {
        for (let i = 0;i < root.length;i++) { 
            var node = root[i];
            // 过滤 text 节点、script 节点
            if ((node.nodeType != 3) && (node.nodeName != 'SCRIPT')) {
                this.nodes.push(node);
                this.do(node);
            }
        }
        return this.nodes;
    }
}
console.log(DFS.do(document.body.childNodes));
广度遍历(BFS)
遍历完父节点的所有兄弟节点再遍历其子节点。

const BFS = {
    nodes: [],
    do (roots) {
        var children = [];
        for (let i = 0;i < roots.length;i++) {
            var root = roots[i];
            // 过滤 text 节点、script 节点
            if ((root.nodeType != 3) && (root.nodeName != 'SCRIPT')) {
                if (root.childNodes.length) children.push(...root.childNodes);
                this.nodes.push(root);
            }
        }
        if (children.length) {
            var tmp = this.do(children);
        } else { // 如果没有子节点
           return this.nodes;
        }
        return tmp;
    }
}
console.log(BFS.do(document.body.childNodes));

深度遍历迷宫

const maze = [
  [0, 1, 0, 0, 0],
  [0, 1, 1, 1, 0],
  [0, 0, 1, 0, 0],
  [0, 1, 1, 1, 1],
  [0, 0, 0, 0, 0]
];

function dfs(x, y, path) {
  // 如果当前位置越界或者是墙,返回 false 表示此路径不通
  if (x < 0 || x >= maze.length || y < 0 || y >= maze[0].length || maze[x][y] === 1) {
    return false;
  }

  // 将当前位置标记为已访问
  maze[x][y] = 1;

  // 将当前位置添加到路径中
  path.push([x, y]);

  // 如果当前位置是终点,找到一条可行路径
  if (x === endX && y === endY) {
    return true;
  }

  // 向上、向下、向左、向右四个方向进行深度优先搜索
  if (dfs(x - 1, y, path) || dfs(x + 1, y, path) || dfs(x, y - 1, path) || dfs(x, y + 1, path)) {
    return true;
  }

  // 如果当前路径不通,回溯到上一个位置
  path.pop();
  return false;
}

const startX = 0;
const startY = 0;
const endX = 4;
const endY = 4;

const path = [];
const foundPath = dfs(startX, startY, path);

if (foundPath) {
  console.log("可行路径:", path);
} else {
  console.log("没有找到可行路径。");
}

广式遍历迷宫

const maze = [
  [0, 1, 0, 0, 0],
  [0, 1, 1, 1, 0],
  [0, 0, 1, 0, 0],
  [0, 1, 1, 1, 1],
  [0, 0, 0, 0, 0]
];

function bfs(startX, startY, endX, endY) {
  const queue = [];
  const visited = new Set();
  const path = [];

  // 初始节点入队列
  queue.push([startX, startY]);
  visited.add(`${startX}-${startY}`);

  while (queue.length > 0) {
    const [x, y] = queue.shift();
    path.push([x, y]);

    // 如果到达终点,找到一条可行路径
    if (x === endX && y === endY) {
      return path;
    }

    // 向上、向下、向左、向右四个方向进行广度优先搜索
    const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];

    for (const [dx, dy] of directions) {
      const nx = x + dx;
      const ny = y + dy;

      // 如果下一个节点在迷宫范围内且未访问过,将其加入队列
      if (
        nx >= 0 &&
        nx < maze.length &&
        ny >= 0 &&
        ny < maze[0].length &&
        maze[nx][ny] === 0 &&
        !visited.has(`${nx}-${ny}`)
      ) {
        queue.push([nx, ny]);
        visited.add(`${nx}-${ny}`);
      }
    }
  }

  // 没有找到可行路径
  return null;
}

const startX = 0;
const startY = 0;
const endX = 4;
const endY = 4;

const result = bfs(startX, startY, endX, endY);

if (result) {
  console.log("可行路径:", result);
} else {
  console.log("没有找到可行路径。");
}

可以的大家点点关注-总结不易谢谢大家-也可以留言需要哪类的我也可以尝试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值