-
Notifications
You must be signed in to change notification settings - Fork 0
/
rushhour.py
208 lines (187 loc) · 5.94 KB
/
rushhour.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'''
pardis ghavami - 9717023147
HW2 - Q5 implementation
rush hour puzzel
'''
import sys
import copy
class State:
def __init__(self, board):
self.board = copy.deepcopy(board)
self.set_details()
self.parent = None
self.depth = 0
def set_details(self):
cars = set()
for i in self.board:
for j in i:
cars.add(j)
cars.remove('.')
length = {}
row = {}
col = {}
dir = {}
for ch in cars:
count = 1
changed_dir = False
for i in self.board:
for j, value in enumerate(i):
if value == ch:
if count == 1:
row[ch] = self.board.index(i)
col[ch] = j
else:
length[ch] = count
count += 1
if changed_dir:
dir[ch] = 'v'
else:
dir[ch] = 'h'
if count > 1:
changed_dir = True
self.cars = cars
self.length = length
self.row = row
self.col = col
self.dir = dir
def change_board(self, i, j, value):
self.board[i][j] = value
self.set_details()
def is_GOAL(self):
if self.dir['r'] == 'h':
if self.col['r'] + self.length['r'] == len(self.board[0]):
return True
elif self.dir['r'] == 'v':
if self.row['r'] + self.length['r'] == len(self.board):
return True
return False
def __eq__(self, other):
return self.board == other.board and self.row == other.row and self.col == other.col and self.cars == other.cars and self.length == other.length and self.dir == other.dir
def __hash__(self):
return hash((tuple(i) for i in self.board))
def parse_board(string_board):
return [list(line) for line in string_board.strip().splitlines()]
def expand(s):
children = []
for ch in s.cars:
if s.dir[ch] == 'h':
board_f = copy.deepcopy(s.board) #moving forward
i = s.row[ch]
j = s.col[ch]
c = s.col[ch] + s.length[ch]
dots = 0
while c < len(s.board[0]) and s.board[i][c] == '.':
dots += 1
c += 1
if(dots >= 1):
for x in range(dots):
for l in range(s.length[ch] - 1, -1, -1):
board_f[i][j+l+1] = ch
board_f[i][j+l] = '.'
j += 1
new_s = State(board_f)
new_s.parent = s
new_s.depth = s.depth + 1
children.append(new_s)
board_b = copy.deepcopy(s.board) #moving backward
i = s.row[ch]
j = s.col[ch]
c = s.col[ch] - 1
dots = 0
while c >= 0 and s.board[i][c] == '.':
dots += 1
c -= 1
if(dots >= 1):
for x in range(dots):
for l in range(s.length[ch]):
board_b[i][j+l-1] = ch
board_b[i][j+l] = '.'
j -= 1
new_s = State(board_b)
new_s.parent = s
new_s.depth = s.depth + 1
children.append(new_s)
elif s.dir[ch] == 'v':
board_d = copy.deepcopy(s.board) #moving down
i = s.row[ch]
j = s.col[ch]
c = s.row[ch] + s.length[ch]
dots = 0
while c < len(s.board) and s.board[c][j] == '.':
dots += 1
c += 1
if(dots >= 1):
for x in range(dots):
for l in range(s.length[ch] - 1, -1, -1):
board_d[i+l+1][j] = ch
board_d[i+l][j] = '.'
i += 1
new_s = State(board_d)
new_s.parent = s
new_s.depth = s.depth + 1
children.append(new_s)
board_u = copy.deepcopy(s.board) #moving up
i = s.row[ch]
j = s.col[ch]
c = s.row[ch] - 1
dots = 0
while c >= 0 and s.board[c][j] == '.':
dots += 1
c -= 1
if(dots >= 1):
for x in range(dots):
for l in range(s.length[ch]):
board_u[i + l - 1][j] = ch
board_u[i + l][j] = '.'
i -= 1
new_s = State(board_u)
new_s.parent = s
new_s.depth = s.depth + 1
children.append(new_s)
return children
def ids(board):
l = 0
while True:
result = dfs(board,l)
if result is not "cutoff":
return result
l += 1
def dfs(board, l):
frontier = list()
expanded = set()
result = None
frontier.append(board)
while frontier:
s = frontier.pop()
if s.is_GOAL():
return s
if s.depth > l:
result = "cutoff"
else:
expanded.add(s)
children = expand(s)
for child in children:
if child not in expanded:
frontier.append(child)
return result
def print_path(goal):
path = []
path.append(goal)
parent = goal.parent
while parent:
path.append(parent)
parent = parent.parent
i = 1
while i <= len(path):
s = path[len(path) - i]
print(i , "\n")
i += 1
for r in s.board:
print(r , "\n")
def main():
lis= parse_board(sys.stdin.read())
start = State(lis)
goal = ids(start)
print_path(goal)
if __name__ == '__main__':
main()