Untitled
unknown
python
2 years ago
1.4 kB
9
Indexable
board = """
DNG
IO0
HMB"""
board = board.split()
def get_word(pos, board):
return board[pos[0]][pos[1]]
def replace_str_index(text,index=0,replacement=''):
return '%s%s%s'%(text[:index],replacement,text[index+1:])
def get_move(pos, board):
moves = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
col_len = len(board)
row_len = len(board[0])
res = []
for move in moves:
col_pos = move[0] + pos[0]
row_pos = move[1] + pos[1]
if 0 > col_pos or col_pos > col_len - 1 or 0 > row_pos or row_pos > row_len - 1 or board[col_pos][row_pos] == '0':
continue
res.append([col_pos, row_pos])
return res
def dynamic_board(pos, board, step):
queue = []
word = board[pos[0]][pos[1]]
queue.append([word, board, pos])
while queue:
cur_word, cur_board, cur_pos = queue.pop(0)
print(cur_word)
if len(cur_word) == step:
break
moves = get_move(cur_pos, cur_board)
for move in moves:
new_word = cur_word + get_word(move, cur_board)
new_board = cur_board
new_board[move[0]] = replace_str_index(new_board[move[0]], move[1], '0')
queue.append([new_word, new_board, move])
dynamic_board([0, 0], board, 3)Editor is loading...