Walking Robot
0:00:00
A robot has been designed to navigate a two-dimensional 4x4 matrix by only moving forward or turning right when blocked by a wall of the matrix. Its starting position is in the top left corner of the matrix, denoted by (0,0), and the robot’s final destination is the bottom right corner.
Determine the full path of the robot before it hits the final destination or starts repeating the path.
The ‘walls’ of the matrix are either one of the four borders of the map or any block found within.
A ‘Block’ is donated by having 1 in the cell corresponding to it where an empty cell is having ‘0’.
Example 1:
#Input
robot_map = [
[0,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,0]]
#Output
robot_path(robot_map) -> [(0,0),(0,1),(0,2),(0,3),(1,3),(2,3),(3,3)]
As seen in these diagrams, the robot’s path starts with this sequence of steps:
- Goes forward three times and hits the wall. Turns right.
- Goes forward three times (orientation has changed), 3. Reaches the final destination, and stops.

Example 2:
#Input
robot_map = [
[0,0,1,0],[0,0,0,0],[0,1,0,0],[0,0,0,0]]
#Output
robot_path(robot_map) -> [(0,0),(0,1),(1,1),(1,0)]
Here, the robot moves forward one space, but hits the wall. After turning right, another wall is hit, starting a loop.

.
.
.
.
Comments