-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDSAgent.py
36 lines (32 loc) · 1.26 KB
/
IDSAgent.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
from tiles import Tiles
from PapaAgent import PapaAgent
class IDSAgent(PapaAgent):
def dfs_forIDS(self, maxDepth):
Stack = [(0, 0, set(), [(0, 0)], 0)]
self.visited.clear()
while Stack:
x, y, coins, path, curdepth = Stack.pop()
if (x, y) == self.target and len(coins) == self.maxCoins:
return 1, path
if curdepth < maxDepth:
for dx, dy in self.directions:
nx, ny = x + dx, y + dy
if self.maze.IsValidPos(nx, ny):
AddedCoins = set(coins)
if self.maze.maze[ny][nx] == Tiles.Coin:
AddedCoins.add((nx, ny))
if (nx, ny, frozenset(AddedCoins)) not in self.visited:
self.visited.add((nx, ny, frozenset(AddedCoins)))
Stack.append((nx, ny, AddedCoins, path + [(nx, ny)], curdepth + 1))
return 0, None
def IDS(self):
depth = 1
completed = False
while not completed:
done, path = self.dfs_forIDS(depth)
completed = done
depth += 3
if completed:
return 1,path
else:
return 0,None