ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

js算法:获取可能的路径

2021-09-11 23:30:57  阅读:157  来源: 互联网

标签:string Point 路径 js 算法 neighbor target history name


提问

给了以下点和路径,选取一点输出所有可能走过的点,走过的点不能重复
比如选取a,输出 [[a,b,c],[a,c,b,d,f],[a,b,d,e],…]**
在这里插入图片描述

设计数据格式

class Point {
  name = "";
  neighbor: string[] = [];
}
cosnt data: Point[] = [
  { name: "a", neighbor: ["b", "c"] },
  { name: "b", neighbor: ["a", "c", "d"] },
  { name: "c", neighbor: ["a", "b"] },
  { name: "d", neighbor: ["b", "e", "f", "g"] },
  { name: "e", neighbor: ["d"] },
  { name: "f", neighbor: ["d"] },
]

每一个点用Point类表示,name代表点的名字,neighbor表示该点的邻居

实现过程思路

一组走过的点使用string[]表示
实现一个方法,传入一个target,即点的name,返回所有可能走过的点的组合string[][]。(history参数下面会说到)

type getPath = (target: string, history?: string[]) => string[][];

比如我们从a开始,下一个点可能是b或者c,将会有两种分支。目前我们走的这组点为[‘a’],那么走下一步的时候我们要复制一下我们走过的点的组合,即复制成两个[‘a’]。多个分支复制成多个。那么[‘a’]就是下一次递归的history参数。

然后假设我们现在走过的是[‘a’, ‘b’],下一个点就不能有a了,也就是b点此时可选的邻居是[‘c’, ‘d’]。
实现一个方法,传入走过的点的组合,返回可选的下一个点

type getNext = (history: string[]) => string[];

如果下一步没路走了,也就代表我们找到一组可以走的点。

我们走了一步(target),得到当前走过的点(history),并且也知道下一步可以走哪(getNext),那么接下来就是用getPath进行递归。

具体实现

export class Point {
  name = "";
  neighbor: string[] = [];
}

export default class PathService {
  constructor(props: Point[]) {
    this.data = this.deepCopy(props);
  }

  data: Point[] = [];

  getData() {
    return this.deepCopy(this.data);
  }

  setData(pointList: Point[]) {
    this.data = this.deepCopy(pointList);
    this.resList = [];
  }

  resList: string[][] = [];

  getResList(): string[][] {
    const res: string[][] = [];
    for (const v of this.resList) {
      res.push([...v]);
    }
    return res;
  }

  /**
   * 深拷贝一个Point[]
   * @param pointList
   */
  deepCopy(pointList: Point[]): Point[] {
    const result: Point[] = [];
    for (const v of pointList) {
      result.push({
        name: v.name,
        neighbor: [...v.neighbor],
      });
    }
    return result;
  }

  /**
   * 获取下一次可选路径
   * @param historyArr 已走过的路径
   */
  getNext(historyArr: string[]): string[] {
    let nextData: string[] = [];
    // 获取刚走过的点
    const target = this.data.find(
      (v) => v.name === historyArr[historyArr.length - 1]
    );
    // console.log(target);
    if (target) {
      nextData = target.neighbor;
    }
    // 将走过的点过滤掉
    nextData = nextData.filter((v) => historyArr.indexOf(v) === -1);
    // // console.log(nextData)
    return nextData;
  }

  /**
   * 输入其实目标,获取
   * @param target
   * @param history
   */
  getPath(target: string, history?: string[]): string[][] {
    // console.log(target);
    let nextHistory: string[] = [];
    if (history) {
      // console.log(history);
      nextHistory = [...history];
    }
    // 先走一步
    nextHistory.push(target);
    // console.log('nextHistory', nextHistory)
    // 下一步能走哪呢
    const targetNavigation = this.getNextData(nextHistory || []);
    // console.log("targetNavigation", targetNavigation);
    // 没路走了
    if (targetNavigation.length === 0) {
      this.resList.push(nextHistory);
    }
    // 还有路走,可走的分支进行递归
    for (const v of targetNavigation) {
      this.getPath(v, nextHistory);
    }

    // 递归结束,全在this.resList里存好了
    return this.getResList();
  }
}

测试

    const data: Point[] = [
      { name: "a", neighbor: ["b", "c"] },
      { name: "b", neighbor: ["a", "c", "d"] },
      { name: "c", neighbor: ["a", "b"] },
      { name: "d", neighbor: ["b", "e", "f", "g"] },
      { name: "e", neighbor: ["d"] },
      { name: "f", neighbor: ["d"] },
    ];
    let pathService = new PathService(data);
    let result = pathService.getPath("a");
    console.log(result);

在这里插入图片描述

标签:string,Point,路径,js,算法,neighbor,target,history,name
来源: https://blog.csdn.net/qq_38678923/article/details/120244276

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有