-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbotClean.py
More file actions
171 lines (115 loc) · 4.38 KB
/
Copy pathbotClean.py
File metadata and controls
171 lines (115 loc) · 4.38 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
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
"""
The goal of Artificial Intelligence is to create a rational agent (Artificial Intelligence 1.1.4).
An agent gets input from the environment through sensors and acts on the environment with actuators.
In this challenge, you will program a simple bot to perform the correct actions based on environmental input.
Meet the bot MarkZoid.
It's a cleaning bot whose sensor is a head mounted camera and whose actuators are the wheels beneath it.
It's used to clean the floor.
The bot here is positioned at the top left corner of a 5*5 grid.
Your task is to move the bot to clean all the dirty cells.
Input Format
The first line contains two space separated integers which indicate the current position of the bot.
The board is indexed using Matrix Convention
5 lines follow representing the grid.
Each cell in the grid is represented by any of the following 3 characters: 'b' (ascii value 98) indicates the bot's current position, 'd' (ascii value 100) indicates a dirty cell and '-' (ascii value 45) indicates a clean cell in the grid.
Note If the bot is on a dirty cell, the cell will still have 'd' on it.
Output Format
The output is the action that is taken by the bot in the current step, and it can be either one of the movements in 4 directions or cleaning up the cell in which it is currently located.
The valid output strings are LEFT, RIGHT, UP and DOWN or CLEAN.
If the bot ever reaches a dirty cell, output CLEAN to clean the dirty cell.
Repeat this process until all the cells on the grid are cleaned.
Sample Input #00
0 0
b---d
-d--d
--dd-
--d--
----d
Sample Output #00
RIGHT
Resultant state
-b--d
-d--d
--dd-
--d--
----d
Sample Input #01
0 1
-b--d
-d--d
--dd-
--d--
----d
Sample Output #01
DOWN
Resultant state
----d
-d--d
--dd-
--d--
----d
Task
Complete the function next_move that takes in 3 parameters posr, posc being the co-ordinates of the bot's current position and board which indicates the board state to print the bot's next move.
The codechecker will keep calling the function next_move till the game is over or you make an invalid move.
Scoring
Your score is (200 - number of moves the bot makes)/40. CLEAN is considered a move as well.
Once you submit, your bot will be played on four grids with three of the grid configurations unknown to you.
The final score will be the sum of the scores obtained in each of the four grids.
Education Links
Introduction to AI by Stuart Russell and Peter Norvig
Motor cognition
"""
#!/usr/bin/python
# Head ends here
def next_move(posr, posc, board):
if board[posr][posc] == 'd':
return print('CLEAN')
for i in range(1, 5):
min_r = max(0, posr - i)
min_c = max(0, posc - i)
max_r = min(4, posr + i)
max_c = min(4, posc + i)
if board[posr][min_c] == 'd':
return print('LEFT')
if board[posr][max_c] == 'd':
return print('RIGHT')
if board[min_r][posc] == 'd':
return print('UP')
if board[max_r][posc] == 'd':
return print('DOWN')
for j in range(1, i+1):
col = max(min_c, posc-j)
if board[min_r][col] == 'd':
return print('LEFT')
if board[max_r][col] == 'd':
return print('LEFT')
col = min(max_c, posc+j)
if board[min_r][col] == 'd':
return print('RIGHT')
if board[max_r][col] == 'd':
return print('RIGHT')
row = max(min_r, posr-j)
if board[row][min_c] == 'd':
return print('UP')
if board[row][max_c] == 'd':
return print('UP')
row = min(max_r, posr+j)
if board[row][min_c] == 'd':
return print('DOWN')
if board[row][max_c] == 'd':
return print('DOWN')
print("")
# Tail starts here
if __name__ == "__main__":
# pos = [int(i) for i in input().strip().split()]
# board = [[j for j in input().strip()] for i in range(5)]
# next_move(pos[0], pos[1], board)
# pos = 0, 0
# board = ['b---d', '-d--d', '--dd-', '--d--', '----d']
# next_move(pos[0], pos[1], board)
# pos = 0, 1
# board = ['-b--d', '-d--d', '--dd-', '--d--', '----d']
# next_move(pos[0], pos[1], board)
pos = 1, 1
board = ['-----', '-b---', '---d-', '---d-', '--d-d']
next_move(pos[0], pos[1], board)