Exactly Electrical
InterviewTime limit1sMemory limit512 MB
Given start and destination grid points and a battery charge t, decide whether a walk of exactly t unit steps connects them.
- Level
Easy3 of 10
- Topics
- Math, Implementation, Brute force
- Solved
- No attempts yet
Problem
Grid 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 meet at .
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.
Input
The input consists of three lines.
The first line contains two integers and , the starting coordinate (, ).
The second line contains two integers and , the destination coordinate (, ).
The third line contains an integer , the initial number of units of charge in the battery ().
Output
Print Y if you can move from the starting coordinate to the destination coordinate using exactly units of charge. Otherwise print N.
Hint
Going from through and to uses 3 units of charge.
From you can reach with 2 units by driving north twice. You can also reach it with 4 units by passing through , , and . No path of length 5 reaches it.
From you can reach with 5 units by passing through , , , and .