在python中生成一个满足条件的随机矩阵

问题描述 投票:0回答:1
i手动定义python中的以下16个矩阵:

matrices = { "Simulation 1": [ [1, 1, 1, 1, 1, 2], [1, 1, 1, 1, 2, 2], [1, 1, 1, 2, 2, 2], [1, 1, 1, 2, 3, 2], [1, 1, 1, 1, 3, 3], [1, 1, 1, 1, 3, 3] ], "Simulation 2": [ [1, 1, 2, 2, 2, 2], [1, 1, 1, 2, 2, 2], [1, 1, 2, 2, 2, 2], [1, 1, 1, 2, 2, 2], [1, 1, 1, 2, 2, 3], [1, 1, 1, 3, 3, 3] ], "Simulation 3": [ [1, 1, 2, 2, 2, 2], [1, 1, 1, 2, 2, 2], [1, 1, 1, 2, 3, 3], [1, 1, 2, 2, 3, 3], [1, 1, 1, 3, 3, 3], [1, 1, 3, 3, 3, 3] ], "Simulation 4": [ [1, 1, 1, 1, 2, 2], [1, 1, 1, 2, 2, 2], [3, 1, 3, 3, 3, 2], [3, 3, 3, 3, 3, 2], [3, 3, 3, 3, 3, 2], [3, 3, 3, 3, 3, 3] ], "Simulation 5": [ [1, 1, 1, 2, 2, 2], [1, 1, 1, 2, 2, 2], [1, 1, 1, 1, 3, 2], [1, 3, 3, 3, 3, 3], [3, 3, 3, 3, 3, 3], [3, 3, 3, 3, 3, 3] ], "Simulation 6": [ [1, 1, 1, 1, 1, 2], [1, 1, 1, 2, 2, 2], [1, 1, 1, 1, 2, 2], [1, 1, 1, 1, 2, 3], [1, 3, 3, 3, 3, 3], [1, 3, 3, 3, 3, 3] ], "Simulation 7": [ [1, 1, 1, 1, 2, 2], [1, 1, 1, 1, 1, 2], [1, 1, 1, 2, 2, 2], [1, 1, 3, 2, 2, 3], [1, 1, 3, 3, 3, 3], [3, 3, 3, 3, 3, 3] ], "Simulation 8": [ [1, 1, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 3, 3], [2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3] ], "Simulation 9": [ [1, 1, 2, 2, 2, 2], [1, 1, 1, 2, 2, 2], [1, 1, 1, 1, 2, 2], [1, 1, 1, 1, 3, 2], [1, 1, 1, 1, 3, 3], [1, 1, 3, 3, 3, 3] ], "Simulation 10": [ [1, 1, 1, 2, 2, 2], [1, 1, 2, 2, 2, 2], [1, 1, 2, 2, 2, 2], [1, 1, 2, 2, 2, 3], [1, 1, 1, 1, 3, 3], [1, 1, 1, 3, 3, 3] ], "Simulation 11": [ [1, 1, 1, 2, 2, 2], [1, 1, 2, 2, 2, 2], [1, 1, 2, 2, 2, 3], [1, 1, 1, 2, 3, 3], [1, 1, 1, 3, 3, 3], [1, 1, 1, 3, 3, 3] ], "Simulation 12": [ [1, 1, 1, 1, 2, 2], [1, 1, 1, 1, 2, 2], [1, 1, 1, 1, 2, 2], [3, 1, 1, 3, 3, 3], [3, 3, 3, 3, 3, 3], [3, 3, 3, 3, 3, 3] ], "Simulation 13": [ [1, 1, 1, 2, 2, 2], [1, 1, 1, 1, 2, 2], [1, 1, 1, 2, 2, 2], [1, 1, 1, 3, 3, 3], [1, 3, 3, 3, 3, 3], [3, 3, 3, 3, 3, 3] ], "Simulation 14": [ [1, 1, 1, 2, 2, 2], [1, 1, 1, 1, 1, 2], [1, 1, 1, 1, 1, 2], [1, 1, 1, 3, 3, 2], [1, 3, 3, 3, 3, 3], [1, 3, 3, 3, 3, 3] ], "Simulation 15": [ [1, 1, 1, 2, 2, 2], [1, 2, 2, 2, 2, 2], [1, 1, 1, 2, 2, 2], [1, 1, 1, 1, 3, 3], [1, 1, 1, 3, 3, 3], [1, 1, 1, 3, 3, 3] ], "Simulation 16": [ [1, 1, 3, 2, 2, 2], [1, 1, 3, 2, 3, 3], [1, 1, 3, 3, 3, 3], [1, 1, 3, 3, 3, 3], [1, 1, 3, 3, 3, 3], [1, 1, 3, 3, 3, 3] ] }
当可视化时,这些看起来像这样:

enter image description here 基质中的位置是这样理解的:

positions = [ [1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36] ]

这些矩阵具有以下属性:

1=红色,2 =蓝色,3 =绿色

位置1始终是红色,位置6总是蓝色,位置36始终为绿色
    同一颜色的所有圆都可以到达相同颜色的所有其他圆圈,而无需接触任何其他颜色
  • here是无效矩阵的一个示例(即节点1(红色)无法到达其他红色节点而不会踏上蓝色):

我有以下问题:

有一些算法可以用于快速生成此问题的(随机)有效矩阵吗?可以使用像树/图之类的东西可以有效地生成10000个这样的解决方案吗? enter image description here 一种方法是在三个初始彩色角中进行随机的洪水填充。 the是可能的实现:

import random def make_matrix(n): mat = [[0] * n for _ in range(n)] frontier = set() def place(row, col, color): mat[row][col] = color frontier.discard((row, col, 1)) frontier.discard((row, col, 2)) frontier.discard((row, col, 3)) for next_row, next_col in (row-1, col), (row+1, col), (row, col-1), (row, col+1): if 0 <= next_row < n and 0 <= next_col < n: if mat[next_row][next_col] == 0: frontier.add((next_row, next_col, color)) place(0, 0, 1) place(0, n-1, 2) place(n-1, n-1, 3) while frontier: place(*random.choice(list(frontier))) return mat there是一个示例运行:

mat = make_matrix(6) for row in mat: print(row)
python matrix graph tree
1个回答
0
投票
主要变量为

frontier

,它具有一组可能的下一步操作。一个动作包括涂色,因此定义了单元格的坐标和颜色代码(1、2或3)。

时迭代从该集合中随机选择一个动作,并应用该动作。这也意味着可能采取了一组可能的动作:有些不再可能,而另一些则可能是可能的。

因此,这会重复直到没有更多的细胞可以颜色为止。
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.