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
- Level: CCC Junior J3; an entry point to proof-based reasoning
- Pattern: Manhattan Distance + Parity + Necessary and Sufficient Conditions
- Prerequisite:
abs(), conditionals, modulo%, Cartesian coordinates - What this teaches: separate the minimum amount of work from whether an exact amount is achievable
- Where it appears again: shortest paths on grids, checkerboard parity, invariants, reachability tests, and edge-case design
What to Study Next
- Related CP Weekly problems: CCC 2017 J5/S3 Nailed It! , CCC 2023 J4/S1 Trianglane , and CCC 2017 S1 Sum Game
- Related foundations: absolute value, logical AND, modulo, and boundary testing
- Related DSA notes: What Does Big O Mean?, What Is a Dry Run?, and Grid Problems: From Simulation to Graph Search
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
- Time:
O(1) - Extra space:
O(1)
The algorithm performs a fixed number of arithmetic and logical operations, independent of the coordinate values.
Common Mistakes
- Checking distance but not parity.
- Using Euclidean distance instead of Manhattan distance.
- Checking whether
titself is even instead of comparing its parity with the distance. - Mishandling zero charge when the start equals the destination.
- Simulating paths or using BFS on an obstacle-free infinite grid.
Review Questions
- What is the distance from
(1,1)to(4,3)? Doest=7work? - Why can two extra moves always be spent, while one cannot?
- If the start equals the destination, which values of
twork? - Would the same proof remain sufficient if the grid contained blocked intersections?
- Create one test that fails the distance condition and another that fails only the parity condition.
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
