简体   繁体   English

在Grid(nxn)中,如何找到从一个点到另一点的k,k + 1,k + 2条路径?

[英]In a Grid(nxn), how to find k,k+1,k+2 paths from one point to another point?

Consider the grid (a matrix with n=3): 考虑网格(n = 3的矩阵):

0 1 2
3 4 5
6 7 8

I need to find k+1 , k+2 paths between any point to any point (here from 3 to 2),where k is the shortest distance or number of hops ( k=3 ). 我需要找到任意点到任意点(此处为3到2)之间的k+1k+2条路径,其中k是最短距离或跳数( k=3 )。 How do I go about find paths of dist k=4 or k=5 ? 如何查找dist k=4k=5路径?

I have a program (in java) for finding all the k paths : 我有一个用于查找所有k路径的程序(在Java中):

public ArrayList<Path> findPath(int y1, int x1, int y2, int x2){

       int vert = y1-y2;//1
       int hori = x1-x2;//-2
       int max = (vert > 0 ? vert : -vert)+(hori > 0 ? hori : -hori);

       return findPath(y1,x1,vert,hori,vert,hori,0,max);
    }

    public ArrayList<Path> findPath(int y, int x, int vert, int hori, int v, int h, int level, int max){
       if(level > max){
           System.exit(1);
       }
       System.out.println(y+","+x+","+vert+","+hori+","+v+","+h);
       ArrayList<Path> ret = new ArrayList<Path>();
       if(v == 0 && h == 0){
           ret.add(new Path());
           ret.get(0).pushPath(network[y][x]);
           return ret;
       }

       int vm = vert > 0 ? -1 : 1;//-1
       int hm = hori > 0 ? -1 : 1;//1
       System.out.println(vm+","+hm);

       ArrayList<Path> a = new ArrayList<Path>();
       if(v!=0){ a = findPath(y+vm, x, vert, hori, v+vm, h, level+1, max); }

       ArrayList<Path> b = new ArrayList<Path>();
       if(h!=0){ b = findPath(y, x+hm, vert, hori, v, h+hm, level+1, max); }

       for(Path p : a){
           p.pushPath(network[y][x]);
          }

       for(Path p : b){
           p.pushPath(network[y][x]);
       }

       ret = new ArrayList<Path>(a);
       ret.addAll(b);
       return ret;  

     }

By using the distance formula, I am able to restrict the number of vertical and horizontal movements and use recursion to find all the points in the shortest path. 通过使用距离公式,我可以限制垂直和水平移动的数量,并可以使用递归找到最短路径中的所有点。 Can someone please suggest something similar for paths with a dist >3? 有人可以为dist> 3的路径建议类似的东西吗?

您可能会看到Johnson算法(第一部分第二部分) ,该算法在有向图中枚举了所有基本电路(循环)。

Ignore the fact that it is a grid. 忽略它是网格的事实。 Just consider it as a graph. 只需将其视为图形即可。 You have points. 你有分数。 Each point has neighbors. 每个点都有邻居。 Each point has a minimum distance to the final point. 每个点到终点都有一个最小距离。 Here is some Python to demonstrate an algorithm. 这是一些Python来演示算法。 (Translation to Java should not be hard.) (翻译成Java应该不难。)

def paths(p1, p2, length):
    answer = []
    if 0 == length and p1 == p2:
        answer = [p1]
    elif length <= distance(p1, p2):
        for p in neighbors(p1):
            answer.extend([ [p1] + x for x in paths(p, p2, length-1) ])
    return answer

As a significant optimization you may note that in your case all paths from p1 to p2 have the same length mod 2. Therefore for length k + i there are no paths at all unless i is even. 作为一项重要的优化,您可能会注意到,从p1p2所有路径都具有相同的长度模2。因此,对于长度k + i ,除非i是偶数,否则根本没有路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果顺序无关紧要,如何从具有k个元素的集合列表中创建具有k + 1个元素的集合列表? - How to create list of sets with k+1 elements from list of set with k elements if order does not matter? 从标签流中随时查找前k个标签 - Find top k tag at any point in time, from a stream of tags Java快速找到k个最接近给定2D点的点 - Java find k closest points to a given 2D point, quickly 如何将一个 Map 中的 K 和另一个 Map 中的 V 写入一个文件? - How do I write to one file a K from one Map, and a V from another? 如何以 1k 或 2k 的批次从数据库中获取结果? - How to get result from DB in batches of 1k or 2k? 如何创建多图<K,V>从地图<K, Collection<V> &gt;? - How to create a Multimap<K,V> from a Map<K, Collection<V>>? 从N个列表中找到K个最大值 - Find K max values from a N List 如何在矩阵中找到从一个点到另一个点的逃生路线? - How to find escape routes from one point to another in a matrix? 如何找到与休眠值最接近的k个值? - how to find k nearest values to a value with hibernate? 给定 2 个字符串和整数 k,以恰好 k 步将一个字符串转换为另一个字符串 - Given 2 strings and integer k, convert one string into another in exactly k steps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM