-
Notifications
You must be signed in to change notification settings - Fork 0
/
Word_search.py
33 lines (29 loc) · 1.01 KB
/
Word_search.py
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
def word_search(board, word):
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == word[0]:
# A recursive search here
pointer = 0
if search(i, j, board, word, pointer):
return True
return False
def search(i, j, board, word, pointer):
if pointer>=len(word):
return True
if i<0 or j<0 or i>=len(board) or j>=len(board[0]) or word[pointer]!=board[i][j]:
return False
character = board[i][j]
board[i][j] = "Visited"
if search(i-1, j, board, word, pointer + 1):
return True
if search(i, j-1, board, word, pointer + 1):
return True
if search(i+1, j, board, word, pointer + 1):
return True
if search(i, j+1, board, word, pointer + 1):
return True
board[i][j] = character
return False
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
word = "ABCCED"
print(word_search(board, word))