-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboggle_recursive.py
More file actions
47 lines (46 loc) · 1.2 KB
/
boggle_recursive.py
File metadata and controls
47 lines (46 loc) · 1.2 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
from copy import deepcopy
def valid_recursive(y,x,s,b,depth):
#print(y,x,s)
#print(b[y][x])
print(depth)
if s=='' or b[y][x]==None:
return False
elif b[y][x]==s:
return True
elif b[y][x][0]==s[0]:
m=len(b)
n=len(b[0])
#print("line10")
b2=deepcopy(b)
b2[y][x]=None
bool=False
for j in range(y-1,y+2):
for i in range(x-1,x+2):
if j<0: continue
if j>=m: continue
if i<0: continue
if i>=n: continue
if (j,i)==(y,x): continue
if valid_recursive(j,i,s[1:],b2,depth+1):
#print(y,x,s[1:])
return True
return False
def find_word(b,s):
m=len(b)
n=len(b[0])
for y in range(m):
for x in range(n):
if b[y][x]==s[0] and valid_recursive(y,x,s,b,0): return True
return False
def print_board(b):
for row in b:
for cell in row:
print(cell,end="")
print('')
if __name__ == "__main__":
b=[ ["I","L","A","W"],
["B","N","G","E"],
["I","U","A","O"],
["A","S","R","L"] ]
print_board(b)
print(find_word("BINGO",b))