Robot
InterviewTime limit1sMemory limit256 MB
Given a grid with obstacles, a start cell, and a fixed cycling order of the four directions, simulate the robot that moves straight until blocked and report where it stops.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Matrix, Array
- Solved
- No attempts yet
Problem
Haebin likes robots. Instead of giving one command at a time, she fixes the order of the movement directions in advance and lets the robot move on its own.
The room is a grid with rows and columns. Position is the top left cell and the bottom right cell is . The first value is the row and the second value is the column. Some cells hold an obstacle.
The robot moves by these rules.
- The robot moves one cell at a time in its current direction, in a straight line, until it can go no further.
- The robot cannot enter the next cell if that cell lies outside the room, holds an obstacle, or was already visited. The straight move ends there, and the next direction in Haebin's order becomes the current direction. The direction after the last one is the first one again.
- Every cell the robot enters becomes a visited cell. The starting cell is visited from the beginning.
- If the robot cannot enter a cell in any of the four directions, it stops.
You are given the size of the room, the obstacle positions, the starting position of the robot, and the order of the movement directions. Find the position where the robot stops.
Input
The first line contains the number of rows and the number of columns of the room. ()
The second line contains the number of obstacles . ()
Each of the next lines contains the position , of one obstacle. (, )
The next line contains the starting position , of the robot. (, ) There is no obstacle on the starting position.
The last line contains the order of the movement directions as four integers. 1 is up, 2 is down, 3 is left, and 4 is right, and the four numbers are a permutation that uses 1, 2, 3, 4 once each.
Output
Print the row number and the column number of the cell where the robot stopped, separated by a space, on one line.