Under construction

Still being built. Things may change

Robot

Interview

Time limit1sMemory limit256 MB

Summary
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 RR rows and CC columns. Position (0,0)(0, 0) is the top left cell and the bottom right cell is (R−1,C−1)(R - 1, C - 1). 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 RR and the number of columns CC of the room. (3≤R,C≤10003 \le R, C \le 1000)

The second line contains the number of obstacles kk. (0≤k≤10000 \le k \le 1000)

Each of the next kk lines contains the position brb_r, bcb_c of one obstacle. (0≤br≤R−10 \le b_r \le R - 1, 0≤bc≤C−10 \le b_c \le C - 1)

The next line contains the starting position srs_r, scs_c of the robot. (0≤sr≤R−10 \le s_r \le R - 1, 0≤sc≤C−10 \le s_c \le C - 1) 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.

Examples3

  1. Example 1

    Input
    3 3
    1
    1 0
    1 1
    1 2 3 4
    
    Expected output
    0 0
    
  2. Example 2

    Input
    3 3
    0
    0 0
    4 2 3 1
    
    Expected output
    1 1
    
  3. Example 3

    Input
    3 3
    4
    0 1
    1 0
    1 2
    2 1
    1 1
    1 2 3 4
    
    Expected output
    1 1