我有一个带有矩阵的文件,这是我的单词搜索和一些我需要在其中搜索的单词。
O T N E G R A S A E
R N N C O R A L L O
O A I B L U E E V G
U T O R E N T I I A
V I O L E T T O O R
O C R A R I A E L O
D A B I M A L V A P
I P C I E L O G L R
C O R P O S O U A O
A P I E N O M I L P
ACIDO
ARGENTO
BLU
CIELO
CORALLO
CORPOSO
ELETTRICO
LATTE
LIMONE
MALVA
NERO
OCRA
OPACITA
ORO
PAGLIERINO
PIENO
PORPORA
PRIMITIVO
VIOLA
VIOLETTO
对于解决方案,我已经考虑过类似的事情:
with open('cp5_Colori.txt', 'r') as f:
import pprint
data=f.read().replace("\t","")
A=[]
B=set()
data=data.split("\n\n")
word_list=data[1].split()
lista_orizzontale=data[0].split()
puzzle=[list(row) for row in lista_orizzontale]
for parola in word_list:
for lista in puzzle:
x=puzzle.index(lista)
for carattere in lista:
y=lista.index(carattere)
if carattere.upper() == parola[0]:
for direction in [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]:
(dx, dy) = direction
for i in range(len(parola)):
if ((x+dx*i)<len(puzzle)) and ((y+dy*i)<len(lista)) == True:
if puzzle[x+dx*i][y+dy*i].upper()== parola[i]:
puzzle[x+dx*i][y+dy*i]=puzzle[x+dx*i][y+dy*i].lower()
pprint.pprint(puzzle)
要解决这个难题,您需要搜索,然后从单词搜索中删除列表中所有出现的单词(如果有多个)。
将保留的图表字母按行和列顺序排列,将构成游戏的解决方案。 我不知道如何继续以及如何找到“ SANGUEBLU”解决方案
答案 0 :(得分:1)
进行以下初始化:
puzzle = [l.split() for l in '''O T N E G R A S A E
R N N C O R A L L O
O A I B L U E E V G
U T O R E N T I I A
V I O L E T T O O R
O C R A R I A E L O
D A B I M A L V A P
I P C I E L O G L R
C O R P O S O U A O
A P I E N O M I L P'''.splitlines()]
word_list = '''ACIDO
ARGENTO
BLU
CIELO
CORALLO
CORPOSO
ELETTRICO
LATTE
LIMONE
MALVA
NERO
OCRA
OPACITA
ORO
PAGLIERINO
PIENO
PORPORA
PRIMITIVO
VIOLA
VIOLETTO'''.splitlines()
以下代码将解决您的问题:
from itertools import product
removals = []
for word in word_list:
for row in range(len(puzzle)):
for col in range(len(puzzle[row])):
for dr, dc in product(range(-1, 2), repeat=2):
if dr or dc:
removal = []
for i in range(len(word)):
r = row + dr * i
c = col + dc * i
if not (0 <= r < len(puzzle) and 0 <= c < len(puzzle[row])) or puzzle[r][c] != word[i]:
break
removal.append((r, c))
else:
removals.append(removal)
for removal in removals:
for row, col in removal:
puzzle[row][col] = None
print(''.join(char for row in puzzle for char in row if char))
这将输出:
SANGUEBLU
答案 1 :(得分:0)
使用代码对字母进行小写转换后,可以通过以下方式获得结果:
crossed = [['o', 't', 'n', 'e', 'g', 'r', 'a', 'S', 'a', 'e'], ['r', 'n', 'n', 'c', 'o', 'r', 'a', 'l', 'l', 'o'], ['o', 'a', 'i', 'b', 'l', 'u', 'e', 'e', 'v', 'g'], ['U', 't', 'o', 'r', 'e', 'n', 't', 'i', 'i', 'a'], ['v', 'i', 'o', 'l', 'e', 't', 't', 'o', 'o', 'r'], ['o', 'c', 'r', 'a', 'r', 'i', 'a', 'e', 'l', 'o'], ['d', 'a', 'b', 'i', 'm', 'a', 'l', 'v', 'a', 'p'], ['i', 'p', 'c', 'i', 'e', 'l', 'o', 'G', 'l', 'r'], ['c', 'o', 'r', 'p', 'o', 's', 'o', 'U', 'a', 'o'], ['a', 'p', 'i', 'e', 'n', 'o', 'm', 'i', 'l', 'p']]
import re
result = ''.join([ ''.join(line) for line in crossed ])
print(result)
hidden_word = ''.join(re.findall(r'[A-Z]', result))
print (hidden_word) #=> SUGU
但是看来您的代码中的单词缩排了太多。