Given start and destination grid points and a battery charge t, decide whether a walk of exactly t unit steps connects them.
Easy3MathImplementationBrute forceInterviewNo attempts yetTime limit1sMemory limit512 MBGrid City is made of integer-numbered streets that run east to west (parallel to the x-axis) and integer-numbered avenues that run north to south (parallel to the y-axis). The streets and avenues have infinite length, and there is a street for every integer y-coordinate and an avenue for every integer x-coordinate. Every intersection is labelled by its integer coordinates. For example, avenue 7 and street −3 meet at (7,−3).
You drive an electric car that uses one unit of charge to move between two adjacent intersections: one unit to move north or south to the next street, and one unit to move east or west to the next avenue. While the battery still holds charge, at each intersection the car can turn left, turn right, go straight through, or make a U-turn. You may visit the same intersection several times on one trip.
You know your starting intersection, your destination intersection, and the amount of charge in the battery. Decide whether you can travel from the start to the destination so that the battery is empty exactly when you arrive.
The input consists of three lines.
The first line contains two integers a and b, the starting coordinate (a,b) (−1000≤a≤1000, −1000≤b≤1000).
The second line contains two integers c and d, the destination coordinate (c,d) (−1000≤c≤1000, −1000≤d≤1000).
The third line contains an integer t, the initial number of units of charge in the battery (0≤t≤10000).
Print Y if you can move from the starting coordinate to the destination coordinate using exactly t units of charge. Otherwise print N.
Going from (3,4) through (4,4) and (4,3) to (3,3) uses 3 units of charge.
From (10,2) you can reach (10,4) with 2 units by driving north twice. You can also reach it with 4 units by passing through (10,3), (11,3), and (11,4). No path of length 5 reaches it.
From (10,2) you can reach (11,4) with 5 units by passing through (10,3), (11,3), (12,3), and (12,4).