-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathBoggleSearch.py
178 lines (126 loc) · 5.17 KB
/
BoggleSearch.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from functools import reduce
def findword(board, word):
width = len(board[0])
mergedBoard = reduce(lambda prev, this: prev + this, board)
if len(word) == 1:
if word[0] in mergedBoard:
return True
else: return False
chWithInds = {}
for ch in word:
if(ch in mergedBoard):
if(ch not in chWithInds):
if(mergedBoard.count(ch) > 1):
thisInds = [ind for ind, val in enumerate(mergedBoard) if val == ch]
else:
thisInds = [mergedBoard.index(ch)]
chWithInds[ch] = thisInds
else:
return False
prevIndCache = []
matched = False
for i, ch in enumerate(word[1:], 1):
thisChInds = chWithInds[ch]
if(prevIndCache != []):
prevChInds = prevIndCache
prevIndCache = []
else:
prevChInds = chWithInds[word[i-1]]
matched = False
for tI in thisChInds:
thisCoord = ((tI - (tI % width)) / width, tI % width)
for pI in prevChInds:
prevCoord = ((pI - (pI % width)) / width, pI % width)
rowDiff = thisCoord[0] - prevCoord[0]
colDiff = thisCoord[1] - prevCoord[1]
#print(ch, thisCoord, word[i-1], prevCoord, rowDiff, colDiff)
if((rowDiff in [-1, 0, 1]) and (colDiff in [-1, 0, 1])):
prevIndCache.append(tI)
if(pI in chWithInds[word[i - 1]]): chWithInds[word[i - 1]].remove(pI)
matched = True
break
if not matched: break
if(matched): return True
else: return False
#_______ALTERNATE________
def findWord(board, word):
width = len(board[0])
mergedBoard = reduce(lambda prev, this: prev + this, board)
if len(word) == 1:
if word[0] in mergedBoard:
return True
else: return False
if(word[0] in mergedBoard):
startChInds = [ind for ind, val in enumerate(mergedBoard) if val == word[0]]
for stChInd in startChInds:
prevInd = stChInd
for ch in word[1:]:
if(mergedBoard[prevInd - 1] == ch): prevInd -= 1
elif(mergedBoard[prevInd + 1] == ch): prevInd += 1
elif(mergedBoard[prevInd - width] == ch): prevInd = prevInd - width
elif(mergedBoard[prevInd - width - 1] == ch): prevInd = prevInd - width - 1
elif(mergedBoard[prevInd - width + 1] == ch): prevInd = prevInd - width + 1
elif(mergedBoard[prevInd + width] == ch): prevInd = prevInd - width
elif(mergedBoard[prevInd - width - 1] == ch): prevInd = prevInd + width - 1
elif(mergedBoard[prevInd - width + 1] == ch): prevInd = prevInd + width + 1
else: break
else: return True
else: return False
else: return False
# Final program:
visited = []
def find_word(board, word):
global visited
visited = [[False for j in range(len(board[0]))] for i in range(len(board))]
for r in range(len(board)):
for c in range(len(board[r])):
if(word[0] == board[r][c] and search(r, c, 0, board, word)):
return True
return False
def search(r, c, index, board, word):
global visited
if(index == len(word)):
return True
if(r < 0 or r >= len(board) or c < 0 or c >= len(board[r]) or board[r][c] != word[index] or visited[r][c]):
return False
visited[r][c] = True
if(search(r - 1, c, index + 1, board, word) or
search(r - 1, c - 1, index + 1, board, word) or
search(r - 1, c + 1, index + 1, board, word) or
search(r + 1, c, index + 1, board, word) or
search(r + 1, c + 1, index + 1, board, word) or
search(r + 1, c - 1, index + 1, board, word) or
search(r, c - 1, index + 1, board, word) or
search(r, c + 1, index + 1, board, word)):
return True
visited[r][c] = False
return False
def Test():
testBoard = [
["E","A","R","A"],
["N","L","E","C"],
["I","A","I","S"],
["B","Y","O","R"]
]
assert( find_word(testBoard, "C" ) == True)
assert( find_word(testBoard, "EAR" ) == True)
assert( find_word(testBoard, "EARS" ) == False)
assert( find_word(testBoard, "BAILER" ) == True)
assert( find_word(testBoard, "RSCAREIOYBAILNEA") == True)
assert( find_word(testBoard, "CEREAL" ) == False)
assert( find_word(testBoard, "ROBES" ) == False)
def callAll():
testBoard = [
["E","A","R","A"],
["N","L","E","C"],
["I","A","I","S"],
["B","Y","O","R"]
]
find_word(testBoard, "C" )
find_word(testBoard, "EAR" )
find_word(testBoard, "EARS" )
find_word(testBoard, "BAILER" )
find_word(testBoard, "RSCAREIOYBAILNEA")
find_word(testBoard, "CEREAL" )
find_word(testBoard, "ROBES" )
Test()