Title Description

Farmer John recently expanded his farm. From the perspective of cows, the farm is quite infinite! Cows think of the grazing area on the farm as an infinite two-dimensional matrix composed of square squares, each of which has delicious grass (each square is regarded as a square on the chessboard). Farmer John's n Cows (1)<= N <= 50) initially, it is located in different squares, part towards the north and part towards the East.

Every hour, each cow performs one of the following:

- If the grass in her current grid has been eaten by other cows, she will stop.
- Finish all the grass in her current square and move one square in the direction she is facing.

After a period of time, a bald track will be left behind each cow.

If two cows move to the same square with grass in one move, they will share the grass in this square and continue to move in the direction they are facing in the next hour.

Ask for the amount of grass each cow eats. Some cows never stop and eat unlimited grass.

Input format (read from terminal / standard input):

The first line of input contains N. Following N Lines, each line describes the starting position of a cow, including a character N (for North) or E (for East), and two non negative integers X And Y (0)<= x <= 10^9，0 <= y <= 10 ^ 9) represents the coordinates of the grid. All x The coordinates are different, all y The coordinates are different.

In order to make the direction and coordinates as clear as possible, if a cow is in a square (x,y) And moving north, she will reach the grid (x,y+1). If she moves East, she will reach the grid (x+1, y).

Output format (output to terminal / standard output):

Output N that 's ok. Output i Line contains the i in the input The number of squares of grass eaten by a cow. If a cow can eat unlimited grass, output "Infinity" for the cow.

Input example:

6 E 3 5 N 5 3 E 4 6 E 10 4 N 11 2 N 8 1

Output example:

5 3 Infinity Infinity 2 5

Nature of test point:

- In test points 2-5, all coordinates shall not exceed 100.
- There are no additional restrictions on test points 6-10.

Question: Brian Dean

Ideas and solutions:

1. Requirements: n cows. Each cow is given a coordinate xy and travel direction E or n. each cow can eat the grass in the current coordinate point and move forward a square grid every hour. If the grass in front has been eaten, the cow cannot move forward. Calculate the distance that each cow can move forward

2. It can be found from the analysis that if the coordinate x+y is small, it may be blocked by the one with larger x+y. then sort according to x+y. start with the one with larger x+y and judge whether the current cow will be blocked by the one with larger x+y one by one. Note that all cows larger than the current cow x+y must be traversed, because if they are not blocked by this cow, they may be blocked by the next cow

3. The main conditions for obstruction are as follows:

a. The same direction will not interfere with each other

b. Different directions, but the same x+y will not hinder each other. If two cows reach a square at the same time, they can share

c. If the direction is different and x+y is different, find the shortest distance that can block the current cow. Pay attention to the stop of the previous cow and the position of x and Y coordinates

Knowledge points:

Simulation, mathematics, sorting (not sorting), simulation (small amount of data)

code:

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; #define INF 1000000009 struct point{ char dir; long long x, y; int id; long long ans; }P[55]; bool cmp1(point A, point B){ return A.x + A.y > B.x + B.y; } bool cmp2(point A, point B){ return A.id < B.id; } int main(){ int n; cin >> n; for (int i = 1; i <= n; i++){ cin >> P[i].dir >> P[i].x >> P[i].y; P[i].id = i; P[i].ans = INF; } sort(P + 1, P + n + 1, cmp1); for (int i = 1; i <= n; i++){ for (int j = i - 1; j >= 1; j--){ if (P[i].x + P[i].y == P[j].x + P[j].y)continue; if (P[i].dir == P[j].dir)continue; //If the current point goes East and the nearest one goes north, it may be blocked if (P[i].dir == 'E' && P[j].dir == 'N'){ if (P[i].y < P[j].y)continue; if (P[j].ans == INF){ P[i].ans = min(P[i].ans, P[j].x - P[i].x); }else { if (P[j].y + P[j].ans > P[i].y){ P[i].ans = min(P[i].ans, P[j].x - P[i].x); } } }else if (P[i].dir == 'N' && P[j].dir == 'E'){ //Do not exit. If it cannot be blocked by point j, it may be blocked by point j-1 if (P[i].x < P[j].x)continue; if (P[j].ans == INF){ P[i].ans = min(P[i].ans, P[j].y - P[i].y); }else { if (P[j].x + P[j].ans > P[i].x){ P[i].ans = min(P[i].ans, P[j].y - P[i].y); } } } } } sort(P + 1, P + n + 1, cmp2); for(int i = 1; i <= n; i++){ if (P[i].ans == INF)cout << "Infinity" << endl; else cout << P[i].ans << endl; } return 0; }

reflection:

1. Two details need to be considered:

a. If you are not blocked by the current cow, you may be blocked by the next cow, so the current result is not the final result, and the final result is after traversing all the cows. Therefore, you need a challenge arena to find the minimum possible distance, and the initial value of INF should be set larger

b. It is necessary to consider the situation of hole drilling, that is, even if the two directions are different, there is the possibility of blocking, but this possibility is determined not only by the end point, but also by the starting point