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]
]
}
当可视化时,这些看起来像这样:
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始终为绿色有一些算法可以用于快速生成此问题的(随机)有效矩阵吗?可以使用像树/图之类的东西可以有效地生成10000个这样的解决方案吗?
一种方法是在三个初始彩色角中进行随机的洪水填充。
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)
frontier
时迭代从该集合中随机选择一个动作,并应用该动作。这也意味着可能采取了一组可能的动作:有些不再可能,而另一些则可能是可能的。
因此,这会重复直到没有更多的细胞可以颜色为止。