시간 제한메모리 제한제출정답맞힌 사람정답 비율
3.5 초 1024 MB644100.000%

문제

In Nlogonia, several campaigns aim to transform the bicycle into the main mean of transportation in the country. One of the actions to promote bikes is a Hackathon focused on the development of applications to facilitate cyclists’ day-to-day.

Your university’s team has a promising idea. As Nlogonia is a very rainy country, sometimes those who go out in the rain do not intend to get wet but do so purely for lack of option. The idea then is to develop an application capable of generating a route between two points that guarantees that it is possible to make the journey without getting wet, such that there isn’t another route with the same guarantee that would take less time.

For the prototype to be presented at the Hackathon, consider that all points of interest are arranged in the Cartesian plane and have integer coordinates. Rain clouds are modeled as simple polygons (non-contiguous edges do not intersect) with each edge parallel to one of the axes. Each cloud moves one unit of distance per unit of time in one of the cardinal directions (North, South, East, or West). A cyclist also moves one unit of distance per unit of time in cardinal directions. The cyclist can change direction at any of the points of interest; changing directions happens instantly. In addition, during the journey the cyclist can stand still at any of the points of interest, protecting themselves from the rain for any integer amount of units of time desired. The cyclist gets wet if they are not stopped at a point of interest (protecting themselves from the rain) and there’s at least one cloud above them. A cloud is not considered to be above the cyclist when the cyclist is at its border.

While other team members are concerned with generating the data and creating the graphical interface for the Hackathon, your task is to develop the part of the software responsible for generating the best route for a cyclist who wants to travel between two points of interest without getting wet.

The picture above corresponds to the first two samples. In the first sample, as the cloud moves east, the cyclist can go directly to the destination, first moving west and then north, for a total of seven units of time. In the second sample, the cloud moves south, so the cyclist can only move one unit of distance west without getting wet. If the cyclist tried to move west a second time, the cloud would be above them. The fastest way then is to wait one unit of time after moving west, then move north following the border of the cloud, and finally west towards the destination, for a total of eight units of time.

입력

The first line contains four integers Xo, Yo, Xd, and Yd (0 ≤ Xo, Yo, Xd, Yd ≤ 100), indicating that the cyclist starts at point (Xo, Yo) and wants to arrive to point (Xd, Yd). The second line contains an integer N (0 ≤ N ≤ 100) representing the number of rain clouds. After these lines, there are N groups of lines, each group describing a cloud.

Within each group describing a cloud, the first line contains a character C and an integer V (4 ≤ V ≤ 100). The character C is one of the uppercase letters “N”, “S”, “E” or “W”, indicating respectively that the cloud moves in the direction North (y ascending), South (y descending), East (x ascending) or West (x descending). The value V represents the number of vertices of the polygon that models the cloud. Each of the next V lines contains two integers X and Y (0 ≤ X, Y ≤ 100), denoting that the point (X, Y ) is a vertex of the polygon. Vertices appear in clockwise order. All the given vertices are actual corners of the polygon.

출력

Output a single line with an integer indicating the minimum amount of units of time needed to travel from (Xo, Yo) to (Xd, Yd) without getting wet.

예제 입력 1

4 0 1 4
1
E 8
0 5
3 5
3 2
5 2
5 1
2 1
2 2
0 2

예제 출력 1

7

예제 입력 2

4 0 1 4
1
S 8
0 5
3 5
3 2
5 2
5 1
2 1
2 2
0 2

예제 출력 2

8

예제 입력 3

1 2 1 3
1
N 4
1 4
2 4
2 1
1 1

예제 출력 3

1

예제 입력 4

0 0 0 1
1
W 4
1 1
1 0
0 0
0 1

예제 출력 4

2

예제 입력 5

20 1 1 10
2
E 4
1 30
15 30
15 0
1 0
S 4
0 29
100 29
100 22
0 22

예제 출력 5

32

예제 입력 6

42 42 42 42
0

예제 출력 6

0

출처

ICPC > Regionals > Latin America > Latin America Regional Contests > Latin America Regional Contests 2021 C번

  • 문제를 만든 사람: Paulo Cezar Pereira Costa