Accel Thinking

CP Weekly Challenges

CCC 2017 J3 Exactly Electrical Solution: Manhattan Distance and Parity

Use Manhattan distance and parity to decide whether the car can reach its destination while using exactly all available charge.

Having enough battery to reach the destination is not the same as using exactly all the battery. CCC 2017 J3 Exactly Electrical is a compact lesson in turning one word in the statement into a second algorithmic condition: check both Manhattan distance and parity.

Where This Fits in the CCC DSA Map

What to Study Next

What the Problem Is Asking

Grid City is an infinite integer grid. Each move to an adjacent intersection, north, south, east, or west, uses one unit of charge. The input gives a start (a,b), a destination (c,d), and charge t. Output Y exactly when the car can arrive at the destination with an empty battery.

The car may turn around and revisit intersections. Those details matter because they allow two-step detours.

Where Beginners Often Get Stuck

Checking only distance <= t proves that the battery is large enough for a shortest path. It does not prove that every extra unit can be spent. From (0,0) to (1,0), the shortest distance is 1. With t=2, one move reaches the destination but one unit remains. Leaving and returning would require two more moves, so the correct output is N.

Core Algorithm Idea

The shortest number of moves is the Manhattan distance:

distance = |a - c| + |b - d|

If t < distance, the trip is impossible. If the shortest path leaves an even number of extra moves, every pair can be spent by moving to a neighbouring intersection and back. An odd leftover cannot be removed by such round trips.

Therefore:

possible exactly when
t >= distance and (t - distance) % 2 == 0

For the shared example from (0,0) to (2,1), the distance is 3. Charge 3 works, charge 4 fails because one move remains, and charge 5 works because the two extra moves form one round trip.

Python Solution

a, b = map(int, input().split())
c, d = map(int, input().split())
t = int(input())

distance = abs(a - c) + abs(b - d)
extra = t - distance

if extra >= 0 and extra % 2 == 0:
    print("Y")
else:
    print("N")

C++ Solution

#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
    int a, b, c, d, t;
    cin >> a >> b >> c >> d >> t;

    int distance = abs(a - c) + abs(b - d);
    int extra = t - distance;

    cout << (extra >= 0 && extra % 2 == 0 ? "Y\n" : "N\n");
    return 0;
}

Why the Algorithm Is Correct

Any route must make at least |a-c| net horizontal moves and |b-d| net vertical moves, so t >= distance is necessary. Every move flips the parity of x+y, so a route ending at the destination must have the same parity as the Manhattan distance; therefore the leftover must be even.

Conversely, take a shortest route. If the leftover is a non-negative even number, split it into pairs. Spend each pair by moving to any neighbouring intersection and immediately returning. The grid is infinite and revisits are allowed, so this construction always works. The two tests are therefore both necessary and sufficient.

Complexity Analysis

The algorithm performs a fixed number of arithmetic and logical operations, independent of the coordinate values.

Common Mistakes

  1. Checking distance but not parity.
  2. Using Euclidean distance instead of Manhattan distance.
  3. Checking whether t itself is even instead of comparing its parity with the distance.
  4. Mishandling zero charge when the start equals the destination.
  5. Simulating paths or using BFS on an obstacle-free infinite grid.

Review Questions

  1. What is the distance from (1,1) to (4,3) ? Does t=7 work?
  2. Why can two extra moves always be spent, while one cannot?
  3. If the start equals the destination, which values of t work?
  4. Would the same proof remain sufficient if the grid contained blocked intersections?
  5. Create one test that fails the distance condition and another that fails only the parity condition.