-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBotClean Partially Observable
71 lines (59 loc) · 2.13 KB
/
BotClean Partially Observable
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
#!/usr/bin/python3
def next_move(posx, posy, board):
move = ''
if board[posx][posy] == 'd':
move = 'CLEAN'
return move
# Check adjacent cells
adjacent = []
for i in range(4):
x = posx + d_row[i]
y = posy + d_col[i]
# Check if adjacent cell is dirty
if 0 <= x < 5 and 0 <= y < 5 and board[x][y] == 'd':
adjacent.append((x, y))
if adjacent:
# Find the cell with minimum distance
best = 0
mindist = distance(posx, posy, adjacent[0][0], adjacent[0][1])
for i in range(len(adjacent)):
if mindist > distance(posx, posy, adjacent[i][0], adjacent[i][1]):
mindist = distance(posx, posy, adjacent[i][0], adjacent[i][1])
best = i
move = direction(posx, posy, adjacent[best][0], adjacent[best][1])
return move
# Move in the direction of the nearest dirty cell
adjacent = []
for i in range(4):
x = posx + d_row[i]
y = posy + d_col[i]
# Check if adjacent cell is not visited
if 0 <= x < 5 and 0 <= y < 5 and board[x][y] != 'o':
adjacent.append((x, y))
# Find the cell with minimum distance
best = 0
mindist = distance(posx, posy, adjacent[0][0], adjacent[0][1])
for i in range(len(adjacent)):
if mindist > distance(posx, posy, adjacent[i][0], adjacent[i][1]):
mindist = distance(posx, posy, adjacent[i][0], adjacent[i][1])
best = i
move = direction(posx, posy, adjacent[best][0], adjacent[best][1])
return move
def distance(x1, y1, x2, y2):
return abs(x1 - x2) + abs(y1 - y2)
def direction(x1, y1, x2, y2):
if x1 == x2 and y1 < y2:
return 'RIGHT'
if x1 == x2 and y1 > y2:
return 'LEFT'
if x1 > x2 and y1 == y2:
return 'UP'
if x1 < x2 and y1 == y2:
return 'DOWN'
#global variables
d_row = [1, 0, -1, 0]
d_col = [0, 1, 0, -1]
if __name__ == "__main__":
pos = [int(i) for i in input().strip().split()]
board = [[j for j in input().strip()] for i in range(5)]
print(next_move(pos[0], pos[1], board))