我想写一个Python脚本,以确定是否给定的nonogram是独一无二的。我目前的剧本跑得太长,所以我想知道是否有人有任何想法。Python的nonogram独特
据我了解,一般nonogram问题是NP难问题。然而,我知道两条关于我的给定非图形的信息:
- 当分别将黑/白框分别表示为0和1时,我知道每个图像有多少个。
- 我只考虑6x6 nonograms。
我最初使用蛮力的方法(所以2 ^36案件)。然而,知道了(1),我能够将其缩小到n选k(36个选择数为零)的情况。但是,当k接近18时,这仍然是〜2^33个案例。需要几天运行。
任何想法,我怎么可能会加速这个吗?它甚至有可能吗?
同样,我不关心解决办法是什么 - 我已经拥有它。我试图确定的是,如果解决方案是独一无二的。
编辑: 这是不完全的完整代码,但有总体思路:
def unique(nonogram):
found = 0
# create all combinations with the same number of 1s and 0s as incoming nonogram
for entry in itertools.combinations(range(len(nonogram)), nonogram.count(1)):
blank = [0]*len(nonogram) # initialize blank nonogram
for element in entry:
blank[element] = 1 # distribute 1s across nonogram
rows = find_rows(blank) # create row headers (like '2 1')
cols = find_cols(blank)
if rows == nonogram_rows and cols == nonogram_cols:
found += 1 # row and col headers same as original nonogram
if found > 1:
break # obviously not unique
if found == 1:
print('Unique nonogram')
什么是你当前的脚本是什么样子? – squiguy
Nonogram的定义是不是暗示你总是有0和1的编码数?总和所有行值(或所有列值)以获得计数。 – Koterpillar
Koterpillar - 我有数。我想知道是否有其他非图表生成相同的计数。 –