This page is still under construction.

Parts of this page are still being built. What you see may change.

Automotive Navigation

Time limit5sMemory limit256 MB

Summary
From a street map, a start point, and per-step distance and compass readings, the program lists every location where the car can be at time t.
Level

Medium6 of 10

Topics
BFS, Graph, Simulation
Solved
No attempts yet

Problem

The International Commission for Perfect Cars (ICPC) built a city sized test course for driver assistance systems. Your company, Automotive Control Machines (ACM), runs the test drives on that course.

The course is made of straight streets, and each street runs either east to west or north to south. No street has a dead end, so each end of a street meets another street. There are no grade separated crossings either, so whenever two perpendicular streets pass through the same point they meet there, and a car can turn from one street to the other at that point. A car may not make a U-turn, and a car never leaves the streets. Every street has zero width.

The GPS unit of one car broke down while the car was running on the course, and the driver got lost. The odometer and the electronic compass still work.

You know where the car was at the moment the GPS unit broke, which is time 0. From then on you read the odometer and the compass remotely, once every time unit. A compass reading is one of north, east, south, and west. If you read the compass at the exact moment the car is turning, the reading can be the direction before the turn or the direction after it.

The direction the car was heading at time 0 is unknown. Consider every direction that agrees with the street the car was running on at that moment.

Write a program that reports every location where the car can be right now.

Input

The input is a single test case.

The first line contains four integers nn, x0x_0, y0y_0 and tt: the number of streets (4≤n≤504 \le n \le 50), the x and y coordinates of the car at time 0 when the GPS unit broke, and the current time (1≤t≤1001 \le t \le 100). The point (x0,y0)(x_0, y_0) lies on some street.

Each of the next nn lines contains four integers xsx_s, ysy_s, xex_e, yey_e and describes a street running from (xs,ys)(x_s, y_s) to (xe,ye)(x_e, y_e), where (xs,ys)≠(xe,ye)(x_s, y_s) \ne (x_e, y_e). Every street runs east to west or north to south, so xs=xex_s = x_e or ys=yey_s = y_e holds. No two parallel streets overlap or meet. In this coordinate system the x axis points east and the y axis points north. Every input coordinate is at least 0 and at most 50.

Each of the remaining tt lines contains an integer did_i (1≤di≤101 \le d_i \le 10), the measured distance the car ran from time i−1i - 1 to time ii, and a letter cic_i, the measured direction of the car at time ii, which is N for north, E for east, W for west, or S for south.

Output

Print every location where the car can be at time tt that agrees with the measurements. Print one location per line as two integers separated by a single space.

Sort the locations in lexicographic order, so (xi,yi)(x_i, y_i) comes before (xj,yj)(x_j, y_j) when xi<xjx_i < x_j, or when xi=xjx_i = x_j and yi<yjy_i < y_j.

At least one location on a street agrees with the measurements.

Examples3

  1. Example 1

    Input
    4 2 1 1
    1 1 1 2
    2 2 2 1
    2 2 1 2
    1 1 2 1
    9 N
    
    Expected output
    1 1
    2 2
    
  2. Example 2

    Input
    6 0 0 2
    0 0 2 0
    0 1 2 1
    0 2 2 2
    0 0 0 2
    1 0 1 2
    2 0 2 2
    2 E
    7 N
    
    Expected output
    0 1
    1 0
    1 2
    2 1
    
  3. Example 3

    Input
    7 10 0 1
    5 0 10 0
    8 5 15 5
    5 10 15 10
    5 0 5 10
    8 5 8 10
    10 0 10 10
    15 5 15 10
    10 N
    
    Expected output
    5 5
    8 8
    10 10
    15 5