-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbogglegame.py
More file actions
85 lines (82 loc) · 2.28 KB
/
bogglegame.py
File metadata and controls
85 lines (82 loc) · 2.28 KB
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
from collections import Counter
class Boggle():
def search(self,matrix,word):
worddict = Counter()
for c in word:
worddict[c] +=1
row = len(matrix)
if row ==0:
return False
col = len(matrix[0])
if col == 0:
return False
self.visited = [[False] * col for x in xrange(row)]
for i in xrange(row):
for j in xrange(col):
if matrix[i][j] in worddict:
matcheddict = Counter()
matcheddict[matrix[i][j]] +=1
if self.dfs(matrix,word,matrix[i][j],worddict,matcheddict,i,j,row,col):
return True
return False
def dfs(self,matrix,word,matched,worddict,matcheddict,i,j,row,col):
if self.visited[i][j]:
return False
self.visited[i][j] = True
mathced_l = len(matched)
if mathced_l == len(word):
if sorted(matched) == sorted(word):
return True
else:
self.visited[i][j] = False
return False
if i-1>=0:
next = matrix[i-1][j]
next_i = i-1
next_j = j
if next in worddict:
if matcheddict[next] +1 <= worddict[next]:
matcheddict[next] +=1
if self.dfs(matrix,word,matched+next,worddict,matcheddict,next_i,next_j,row,col):
return True
next = matrix[i-1][j]
if i+1 <row:
next = matrix[i+1][j]
next_i = i+1
next_j = j
if next in worddict:
if matcheddict[next] +1 <= worddict[next]:
matcheddict[next] +=1
if self.dfs(matrix,word,matched+next,worddict,matcheddict,next_i,next_j,row,col):
return True
next = matrix[i-1][j]
if j-1>=0:
next = matrix[i][j-1]
next_i = i
next_j = j-1
if next in worddict:
if matcheddict[next] +1 <= worddict[next]:
matcheddict[next] +=1
if self.dfs(matrix,word,matched+next,worddict,matcheddict,next_i,next_j,row,col):
return True
next = matrix[i-1][j]
if j+1 < col:
next = matrix[i][j+1]
next_i = i
next_j = j+1
if next in worddict:
if matcheddict[next] +1 <= worddict[next]:
matcheddict[next] +=1
if self.dfs(matrix,word,matched+next,worddict,matcheddict,next_i,next_j,row,col):
return True
self.visited[i][j] = False
return False
st = "ECEA,ALEP,HNBO,QTTY"
st = st.split(",")
matrix = [row[:] for row in st]
b = Boggle()
assert(b.search(matrix,"PEACE"))
assert(b.search(matrix,"CELEB"))
assert(b.search(matrix,"ELAN"))
assert(b.search(matrix,"CAPE"))
assert(not b.search(matrix,"POPE"))