CCC '17 J3 - Exactly Electrical

View as PDF

Submit solution

Points: 3 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem type
Canadian Computing Competition: 2017 Stage 1, Junior #3

You live in Grid City, which is composed of integer-numbered streets which run east-west (parallel to the x-axis) and integer-numbered avenues which run north-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 x-coordinate. All intersections are labelled by their integer coordinates: for example, avenue 7 and street -3 intersect at (7,-3).

You drive a special electric car which uses up one unit of electrical charge moving between adjacent intersections: that is, moving either north or south to the next street, or moving east or west to the next avenue). Until your battery runs out, at each intersection, your car can turn left, turn right, go straight through, or make a U-turn. You may visit the same intersection multiple times on the same trip.

Suppose you know your starting intersection, your destination intersection and the number of units of electrical charge in your battery. Determine whether you can travel from the starting intersection to the destination intersection using the charge available to you in such a way that your battery is empty when you reach your destination.

Input Specification

The input consists of three lines. The first line contains a followed by b, indicating the starting coordinate (a,b) (-1000 \le a \le 1000; -1000 \le b \le 1000).

The second line contains c followed by d, indicating the destination coordinate (c,d) (-1000 \le c \le 1\,000; -1\,000 \le d \le 1\,000).

The third line contains an integer t (0 \le t \le 10\,000) indicating the initial number of units of electrical charge of your battery.

For 3 of the 15 available marks, 0 \le a, b, c, d \le 2.

For an additional 3 of the 15 marks available, t \le 8.

Output Specification

Output Y if it is possible to move from the starting coordinate to the destination coordinate using exactly t units of electrical charge. Otherwise output N.

Sample Input 1

3 4
3 3

Sample Output 1


Explanation for Sample Output 1

One possibility is to travel from (3,4) to (4,4) to (4,3) to (3,3).

Sample Input 2

10 2
10 4

Sample Output 2


Explanation for Sample Output 2

It is possible to get from (10,2) to (10,4) using exactly 2 units of electricity, by going north 2 units.

It is also possible to travel using 4 units of electricity as in the following sequence:

\displaystyle (10,2) \to (10,3) \to (11,3) \to (11,4) \to (10,4).

It is also possible to travel using 5 units of electricity from (10,2) to (11,4) by the following sequence:

\displaystyle (10,2) \to (10,3) \to (11,3) \to (12,3) \to (12,4) \to (11,4).

It is not possible to move via any path of length 5 from (10,2) to (10,4), however.


  • 3
    NonNonCroissant  commented on Nov. 27, 2023, 4:22 a.m.

    This question sure wants to stop climate change!

  • 2
    julian34  commented on April 16, 2021, 2:18 a.m.

    • -4
      Haoyun  commented on Feb. 1, 2022, 6:51 p.m.

      What? Why Edmonton?

    • 18
      julian33  commented on April 16, 2021, 9:08 p.m.

      why do i have a fan account???

  • 13
    Dav_Did  commented on Nov. 13, 2020, 4:56 p.m.

    Yes, finally, future is here.

    • 1
      littlemouseAM  commented on Feb. 16, 2021, 2:50 a.m.

      Not the future lol. If the future is like this (i.e. they have to use the entire battery to arrive at their destination) then I would be very concerned.

      • 36
        Duckson  commented on Feb. 16, 2021, 7:06 p.m.

        You must be fun at parties