Description:
A robot dog is about to take a walk in a rectangular grid-shaped park that represents an "O" on a passing road and an "X" on an obstacle. The walk will take place according to pre-populated commands in the robot puppy, which will be given in the following format.
["Direction distance"] "Direction distance" .... ]
For example, "E5" means that the robot dog has moved five spaces east of its current location. The robot dog checks the following two things before executing the command.
Make sure you leave the park when moving in a given direction.
Verify that you encounter obstacles while moving in a given direction.
If either of the above is true, the robot dog ignores the command and executes the following command.
If the park is W in width and H in length, the coordinates at the top left of the park are (0, 0), and the coordinates at the bottom right are (H - 1, W - 1).
When the parameters are given a string array park representing the park and a string array routes containing commands to be performed by the robot dog, complete the solution function so that the robot dog returns in the order of [vertical coordinate, lateral coordinate].
Restrictions:
- 3 ≤ length of park ≤ 50
- 3 ≤ park[i] length ≤ 50
- Park[i] is made up of the following characters and is given only one starting point.
- S : Starting point
O : A movable passage
X : Obstacles - A park is a rectangular shape.
- 1 ≤ routes length ≤ 50
- Each element in routes represents the command that the robot puppy will perform.
- The robot puppy executes the commands in order from the first element of routes.
- The elements of routes are structured like "op n", where op means the direction to move and n means the number of compartments to move.
- op is made up of one of the following four.
- N : Move north by the given compartment.
S: Move south by the given compartment.
Woman: It moves west by a given space.
E : Move east by the given compartment. - 1 ≤ n ≤ 9
Code:
def solution(park, routes):
maxx = len(park)
maxy = len(park[0])
for i in range(len(park)):
for j in range(len(park[i])):
if park[i][j] == "S":
start_x = i
start_y = j
for route in routes:
move = route.split(" ")
drct = move[0]
unit = int(move[1])
if drct == 'E':
if start_y + unit >= maxy:
continue
if 'X' in park[start_x][start_y+1:start_y+unit+1]:
continue
start_y += unit
if drct == 'W':
if start_y - unit < 0:
continue
if 'X' in park[start_x][start_y-unit:start_y]:
# Test2
# 01234567
# park = ["OOOXOSOO",
# "OXXXXXXX",
# "OOOXXXXX"]
# routes = ["W 3","S 2","W 1"]
continue
start_y -= unit
if drct == 'N':
if start_x - unit < 0:
continue
if 'X' in [line[start_y] for line in park][start_x-unit:start_x]:
continue
start_x -= unit
if drct == 'S':
if start_x + unit >= maxx:
continue
if 'X' in [line[start_y] for line in park][start_x+1:start_x+unit+1]:
continue
start_x += unit
return start_x, start_y
Discussion:
It was very crucial to determine the starting point(used maxx to know the domain by calculating the length of the whole park, used maxy to know the range of the function by calculating only the first length of park[0]), the direction of x and y coordinate[downward = x, rightward = y) and calculating the move of the puppy(where to add or subtract x-coordinate or y-coordinate).
Additionally, determining the location of obstacle "X" was very significant to continue to the next move of the puppy.
First, if you look at the 24th line, park[start_x][start_y-unit:start_y], the two-dimensional array means park[row][column]. Like the picture above, W3... If you go three spaces to the left, it gets blocked.
So this is the start_y-unit:start_y that calculates the horizontal range that is blocked. It is a code to check if there is an 'X' in the length of the unit based on start_y.
Conversely, if 'X' in [line[start_y] for line in park] [start_x-unit:start_x] in the 36th line is a code to search by column.