-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem2.py
More file actions
105 lines (83 loc) · 2.57 KB
/
problem2.py
File metadata and controls
105 lines (83 loc) · 2.57 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import random
class Problem2:
def __init__(self,table,col,row,dictionary):
self.table = table
self.dictionary = dictionary
self.row = row
self.col = col
self.v = 0
def generateStart(self):
return self.table
def maxLen(self) :
max = len(self.dictionary[0])
for i in self.dictionary :
if max < len(i) :
max = len(i)
return max
def neighbors(self,currentSol):
"""
:param currentSol : current state
:return: the best neighbor for current state
"""
neighbors = []
m = random.randint(0,int(self.row) - 1)
n = random.randint(0, int(self.col) - 1)
x = random.randint(0, int(self.row) - 1)
y = random.randint(0, int(self.col) - 1)
o = currentSol[m][n]
currentSol[m][n] = currentSol[x][y]
currentSol[x][y] = o
neighbors.append(currentSol)
return neighbors
def actions(self,i,j):
actions = ['r', 'u', 'l', 'd']
if i == 0:
if 'u' in actions:
actions.remove('u')
if i == int(self.row) - 1 :
if 'd' in actions:
actions.remove('d')
if j == 0:
if 'l' in actions:
actions.remove('l')
if j == int(self.col) - 1:
if 'r' in actions:
actions.remove('r')
# print(i,j,actions)
return actions
word = ""
def value(self,state):
self.v = 0
for i in range(len(state)) :
for j in range(len(state[i])) :
self.word = ""
# print(i,j)
self.findWord(state,i,j,0,"")
# print(self.v)
return self.v
def findWord(self,state,x,y,v,word):
act = self.actions(x,y)
word = word + state[x][y]
# print(word)
if word in self.dictionary:
# print(state)
# print('yesssssss')
# print(word)
self.v+=1
# return
# if self.v >= len(self.dictionary) :
# return
if len(word) >= self.maxLen():
return
for a in act:
if a == 'r':
self.findWord(state, x, y + 1,v,word)
if a == 'l':
# print(x, y)
self.findWord(state, x, y - 1,v,word)
if a == 'u':
# print(x, y)
self.findWord(state, x - 1, y,v,word)
if a == 'd':
# print(x, y)
self.findWord(state, x + 1, y,v,word)