Algorithm 在M×N大小的网格上随机生成自回避多边形

Algorithm 在M×N大小的网格上随机生成自回避多边形,algorithm,polygon,Algorithm,Polygon,我需要一个算法来在二维网格上随机生成一个大小(M x N)内的自回避多边形。 自回避多边形的定义为。 这是网格上的闭合路径(环),它本身不进行交互 如果可能,该算法将更好地以相同的概率生成任何可能的自回避多边形 我可以想出迷宫生成算法,使用深度优先搜索生成一棵树,然后树的圆周长就是一个自回避多边形。 但是这种方法不能生成所有可能的自回避多边形,例如网格中的最大矩形(mxn) 以下算法将生成1个闭合多边形。但它没有使用任何图论概念。作为一种没有提到的语言,我已经用python编写了代码。如果需要,

我需要一个算法来在二维网格上随机生成一个大小(M x N)内的自回避多边形。 自回避多边形的定义为。 这是网格上的闭合路径(环),它本身不进行交互

如果可能,该算法将更好地以相同的概率生成任何可能的自回避多边形

我可以想出迷宫生成算法,使用深度优先搜索生成一棵树,然后树的圆周长就是一个自回避多边形。 但是这种方法不能生成所有可能的自回避多边形,例如网格中的最大矩形(mxn)


以下算法将生成1个闭合多边形。但它没有使用任何图论概念。作为一种没有提到的语言,我已经用python编写了代码。如果需要,可以很容易地更改它以查找所有多边形

import random

currentpath = [(0,0)]
length = 2 #any actual grid is 3x3 length is 2 however
height = 2
initial = (0,0)
allpaths = []

def backtrack(currentpath,currentnode):
    if(currentnode == (0,0) and len(currentpath)>1):
        return True
    directions = [0,1,2,3]
    while(len(directions) > 0):
        x = random.choice(directions)
        if(x == 0):
            #left
            nextnode = (currentnode[0] + 1, currentnode[1])
            if(currentnode[0] == length or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else :
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
        if(x == 1):
            #right
            nextnode = (currentnode[0] - 1, currentnode[1])
            if (currentnode[0] == 0 or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else:
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
        if(x == 2):
            #up
            nextnode = (currentnode[0], currentnode[1] + 1)
            if (currentnode[1] == height or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else:
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
        if(x == 3):
            #down
            nextnode = (currentnode[0], currentnode[1] - 1)
            if (currentnode[1] == 0 or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else:
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
    if(len(directions)==0):
        return False

backtrack(currentpath,initial)
print (currentpath)
随机导入
currentpath=[(0,0)]
长度=2#任何实际网格为3x3,但长度为2
高度=2
初始值=(0,0)
所有路径=[]
def回溯(currentpath,currentnode):
如果(currentnode==(0,0)和len(currentpath)>1:
返回真值
方向=[0,1,2,3]
而(长度(方向)>0):
x=随机选择(方向)
如果(x==0):
#左
nextnode=(currentnode[0]+1,currentnode[1])

如果(currentnode[0]==长度或(currentpath和nextnode中的nextnode!=(0,0))或(nextnode==(0,0)和len(currentpath)下面的算法将生成1个闭合多边形。但它不使用任何图论概念。由于没有提到语言,我已经用python编写了代码。如果需要,可以很容易地更改以查找所有多边形

import random

currentpath = [(0,0)]
length = 2 #any actual grid is 3x3 length is 2 however
height = 2
initial = (0,0)
allpaths = []

def backtrack(currentpath,currentnode):
    if(currentnode == (0,0) and len(currentpath)>1):
        return True
    directions = [0,1,2,3]
    while(len(directions) > 0):
        x = random.choice(directions)
        if(x == 0):
            #left
            nextnode = (currentnode[0] + 1, currentnode[1])
            if(currentnode[0] == length or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else :
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
        if(x == 1):
            #right
            nextnode = (currentnode[0] - 1, currentnode[1])
            if (currentnode[0] == 0 or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else:
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
        if(x == 2):
            #up
            nextnode = (currentnode[0], currentnode[1] + 1)
            if (currentnode[1] == height or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else:
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
        if(x == 3):
            #down
            nextnode = (currentnode[0], currentnode[1] - 1)
            if (currentnode[1] == 0 or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else:
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
    if(len(directions)==0):
        return False

backtrack(currentpath,initial)
print (currentpath)
随机导入
currentpath=[(0,0)]
长度=2#任何实际网格为3x3,但长度为2
高度=2
初始值=(0,0)
所有路径=[]
def回溯(currentpath,currentnode):
如果(currentnode==(0,0)和len(currentpath)>1:
返回真值
方向=[0,1,2,3]
而(长度(方向)>0):
x=随机选择(方向)
如果(x==0):
#左
nextnode=(currentnode[0]+1,currentnode[1])

如果(currentnode[0]==长度或(currentpath和nextnode中的nextnode!=(0,0))或(nextnode==(0,0)和len(currentpath)算法需要生成全部还是随机创建one@RanikaNisal创建一个就可以了。算法需要生成全部还是随机创建one@RanikaNisal创建一个是可以的。很好!但有一点,随机游走方法似乎可以生成概率较高的较小多边形和概率较低的较大多边形。呵呵我能提高大型复杂多边形的几率吗?这种方法最糟糕的情况是成本极高。想想这种情况,如果空间是100 X 100,那么如果从一个像漩涡一样的形状开始时运气不好,除非遍历所有可能的路径直到回溯,否则尾巴永远不会碰到头部。这种方法所基于的算法是constra内部满意度问题(CSP)。如果你想降低时间复杂度,你可以在约束列表中添加更多约束。例如,如果你有一种算法方法来确定你是否确实处于漩涡中。这种方法的优点是,你可以根据自己的意愿添加约束,避免进入路径。至于查找较小的路径,我真的没有答案。Sele选择随机方向是为了找到一条真正的随机路径。到目前为止,我对每个方向都有两个约束条件。(1)检查我们是否处于阻止我们去那个方向的边缘;(2)检查当前路径中是否已经有下一个节点。在实际运行@Ranika Nisal的代码时,如果
长度>=4高度>=4,我经常会遇到死循环。很好!但有一点,随机行走方法似乎会生成概率较高的较小多边形和概率较低的较大多边形。我如何提高c适用于大型复杂多边形?这种方法最糟糕的情况是成本极高。考虑一下这种情况,如果空间为100 X 100,如果从一个类似于螺旋池的形状开始时运气不好,除非遍历所有可能的路径直到回溯,否则尾巴永远不会碰到头部。这种方法基于的算法是约束满足pr问题(CSP)。如果你想降低时间复杂度,你可以在约束列表中添加更多约束。例如,如果你有一种算法方法来确定你是否确实处于漩涡中。这种方法的优点是,你可以根据自己的意愿添加约束,避免进入路径。至于查找较小的路径,我真的没有答案。Sele选择随机方向是为了找到一条真正的随机路径。到目前为止,我对每个方向都有两个约束条件。(1)检查我们是否处于阻止我们去那个方向的边缘;(2)检查当前路径中是否已经有下一个节点。在实际运行@Ranika Nisal的代码时,如果
长度>=4高度>=4,我通常会得到一个死循环。