Created
October 4, 2017 06:42
-
-
Save h4ckm03d/2e7c6806542e4168f6d77f1a8e14b7bb to your computer and use it in GitHub Desktop.
Board 2D search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
board = [ | |
['a', 'a', 'u', 'r'], | |
['l', 'l', 'g', 'n'], | |
['y', 'q', 'h', 'y'], | |
['p', 'r', 'a', 'l'], | |
] | |
def find_word(board, word): | |
for y,row in enumerate(board): | |
for x, col in enumerate(row): | |
if board[y][x]==word[0] and next(board, word, x, y): | |
print ("GO ", board[y][x]) | |
return True | |
return False | |
def next(board, word, x, y): | |
if len(word)==0: | |
print("==> sucess") | |
return True | |
if x < 0 or y < 0 or x >= len(board[0]) or y >= len(board) or board[y][x]=="#" or board[y][x]!=word[0]: | |
return False | |
tmp = board[:] | |
tmp[y][x] = "#" | |
print(word, x, y) | |
return next(tmp, word[1:], x+1, y) or \ | |
next(tmp, word[1:], x, y+1) or \ | |
next(tmp, word[1:], x, y-1) or \ | |
next(tmp, word[1:], x-1, y) | |
wd = "alyn" | |
print (board, wd, len(board), len(board[0])) | |
print (find_word(board, wd)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment