Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Math 在给定长度的网格中计算可能的排列?_Math - Fatal编程技术网

Math 在给定长度的网格中计算可能的排列?

Math 在给定长度的网格中计算可能的排列?,math,Math,我有一个4x4的格子,上面满是字母。如何计算从任意点到任意点(由2到10个点组成)的所有可能路线 管线内的所有点必须垂直、水平或对角连接到同一管线内的另一点。例如,你可以从A到B,从A到E,从A到F,但不能从A到C 每个点在路线中只能使用一次 以下是25种可能排列的示例: +---+---+---+---+ | A | B | C | D | +---+---+---+---+ | E | F | G | H | +---+---+---+---+ | I | J | K | L | +---+

我有一个4x4的格子,上面满是字母。如何计算从任意点到任意点(由2到10个点组成)的所有可能路线

管线内的所有点必须垂直、水平或对角连接到同一管线内的另一点。例如,你可以从A到B,从A到E,从A到F,但不能从A到C

每个点在路线中只能使用一次

以下是25种可能排列的示例:

+---+---+---+---+
| A | B | C | D |
+---+---+---+---+
| E | F | G | H |
+---+---+---+---+
| I | J | K | L |
+---+---+---+---+
| M | N | O | P |
+---+---+---+---+

- AB
- ABC
- ABCD
- ABCDH
- ABCDHG
- ABCDHGF
- ABCDHGFE
- ABCDHGFEI
- ABCDHGFEIJ
- AE
- AEI
- AEIM
- AEIMN
- AEIMNJ
- AEIMNJF
- AIEMNJFB
- AIEMNJFBC
- AIEMNJFBCG
- AFKP
- PONM
- FGKL
- NJFB
- MNJGD

现在我要澄清这个问题。我不是问如何得到所有的排列。我想问的是可能的排列总数(即整数)是多少,以及如何计算。

如评论中所述,这个问题可以用java中的基本DFS来回答,从左上角(0,0)开始

编辑:我添加了
if(计数(访问)>10)返回

static int count=0;
静态整数计数(布尔值[][]b){
int r=0;

对于(int i=0;i问题的要求非常复杂,我怀疑是否有简单的数学计算——至少我想不出一个。下面是递归Python代码,用于查找路径计数

SIDE = 4        # Length of side of grid
MAXLEN = 10     # Maximum path length allowed
SIDE2 = SIDE + 2
DIRS = (        # offsets for directions
        -1 * SIDE2 - 1,  # up & left
        -1 * SIDE2 + 0,  # up
        -1 * SIDE2 + 1,  # up & right
         0 * SIDE2 - 1,  # left
         0 * SIDE2 + 1,  # right
         1 * SIDE2 - 1,  # down & left
         1 * SIDE2 + 0,  # down
         1 * SIDE2 + 1,  # down & right
)

def countpaths(loc, pathlen):
    """Return the number of paths starting at the point indicated by
    parameter loc of length at most parameter pathlen, not repeating
    points or using points marked False in global variable isfree[]."""
    global isfree
    pathcnt = 1  # count sub-path of just this one point
    if pathlen > 1:
        isfree[loc] = False
        for dir in DIRS:
            if isfree[loc + dir]:
                pathcnt += countpaths(loc + dir, pathlen - 1)
        isfree[loc] = True
    return pathcnt

# Init global boolean array variable to flag which points are still available
isfree = [1 <= r <= SIDE and 1 <= c <= SIDE
          for r in range(SIDE2) for c in range(SIDE2)]

# Use the symmetries of the square grid to find count of paths in grid
allpathcnt = 0
for r in range(1, (SIDE + 1) // 2 + 1):  # do a triangular slice of the grid
    for c in range(1, r + 1):
        # Find the number of similar (by symmetry) points in the grid
        if 2 * r - 1 == SIDE:
            if r == c:
                sym = 1  # center of entire grid
            else:
                sym = 4  # center of column
        else:
            if r == c:
                sym = 4  # diagonal
            else:
                sym = 8  # other
        # Add paths starting at this kind of point removing those of length 1
        allpathcnt += sym * (countpaths(r * SIDE2 + c, MAXLEN) - 1) 

print('Total path count is ' + str(allpathcnt))
SIDE=4#网格边长度
MAXLEN=10#允许的最大路径长度
侧面2=侧面+2
DIRS=(#方向的偏移量
-1*侧面2-1,#向上和向左
-1*SIDE2+0,#向上
-1*侧面2+1,#向上和向右
0*侧2-1,#左
0*SIDE2+1,#右
1*侧面2-1,#向下和向左
1*侧面2+0,#向下
1*侧面2+1,#向下和向右
)
def计数路径(loc、pathlen):
“”“返回从所指示的点开始的路径数。”
最大参数路径长度的参数loc,不重复
点或使用全局变量isfree[]中标记为False的点。“”
全球免费
pathcnt=1#只计算这一点的子路径
如果路径长度>1:
isfree[loc]=False
对于目录中的目录:
如果isfree[loc+dir]:
pathcnt+=计数路径(loc+dir,路径-1)
isfree[loc]=真
返回路径
#初始化全局布尔数组变量以标记哪些点仍然可用

isfree=[1我认为这是“旅行商问题”(TSP)的一种变体。也许这个答案会帮助你:这不是旅行推销员的问题。我想到的第一个想法是,你可以通过对称走很多捷径……这不是很明显。但它很可能已经被研究过。一件事可能是计算1X1、2X2、3X3、4X4网格的前几个项,然后将这些数字插入On Lin整型序列百科全书我不懂Java,所以我问:这段代码是否强制要求每个计数路径有2到10个点?是的,至少这是目的。每次调用函数
dfs(…)
时,count
都是递增的,
dfs(…)
是为每个新顶点调用的。您的答案似乎与我的答案不同,我想知道为什么?我说您的
105837
答案只计算从点A开始的路径,即
(0,0)是否正确
?如果是这样,您能确认或取消确认我对从任何点开始的路径的计数吗?@Rory Daulton是的,您的正确,如果您是指总和,我确实得到了
1626144
,您的答案似乎与我的答案不同,我想知道为什么?@unska:我也想知道为什么。因为我不懂Java,所以我无法检查其他代码。当我得到t时ime我将对我自己的代码进行更多的检查,但到目前为止它通过了我所有的测试。@unska:现在Bobas_Pett已经更正了他的代码,以说明最大路径长度,我看到了区别。他的答案只计算从网格中A点开始的路径,而我的答案是从所有点开始的路径。我同意他的答案,有1058条路径37个长度有限的路径从A开始,但也有109330个路径从B开始,82039个路径从F开始。我已经将我的代码推广到任何大小的正方形网格,并做了更多的测试,我相信我的答案是正确的。
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(1,2)(0,3): 105834
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(1,2)(1,3): 105835
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(1,2)(2,3): 105836
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(2,0): 105837
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(2,0)(1,0): 105838
105837
SIDE = 4        # Length of side of grid
MAXLEN = 10     # Maximum path length allowed
SIDE2 = SIDE + 2
DIRS = (        # offsets for directions
        -1 * SIDE2 - 1,  # up & left
        -1 * SIDE2 + 0,  # up
        -1 * SIDE2 + 1,  # up & right
         0 * SIDE2 - 1,  # left
         0 * SIDE2 + 1,  # right
         1 * SIDE2 - 1,  # down & left
         1 * SIDE2 + 0,  # down
         1 * SIDE2 + 1,  # down & right
)

def countpaths(loc, pathlen):
    """Return the number of paths starting at the point indicated by
    parameter loc of length at most parameter pathlen, not repeating
    points or using points marked False in global variable isfree[]."""
    global isfree
    pathcnt = 1  # count sub-path of just this one point
    if pathlen > 1:
        isfree[loc] = False
        for dir in DIRS:
            if isfree[loc + dir]:
                pathcnt += countpaths(loc + dir, pathlen - 1)
        isfree[loc] = True
    return pathcnt

# Init global boolean array variable to flag which points are still available
isfree = [1 <= r <= SIDE and 1 <= c <= SIDE
          for r in range(SIDE2) for c in range(SIDE2)]

# Use the symmetries of the square grid to find count of paths in grid
allpathcnt = 0
for r in range(1, (SIDE + 1) // 2 + 1):  # do a triangular slice of the grid
    for c in range(1, r + 1):
        # Find the number of similar (by symmetry) points in the grid
        if 2 * r - 1 == SIDE:
            if r == c:
                sym = 1  # center of entire grid
            else:
                sym = 4  # center of column
        else:
            if r == c:
                sym = 4  # diagonal
            else:
                sym = 8  # other
        # Add paths starting at this kind of point removing those of length 1
        allpathcnt += sym * (countpaths(r * SIDE2 + c, MAXLEN) - 1) 

print('Total path count is ' + str(allpathcnt))