Leonardo, like many other Italian scientists and artists of his age, was extremely interested in city planning and urban design. He aimed to model an ideal city: comfortable, spacious and rational in its usage of resources, far away from the narrow, claustrophobic cities of the Middle Ages.
The ideal city
The city is made of
- For any two empty cells, there exists at least one sequence of adjacent empty cells connecting them.
- For any two non-empty cells, there exists at least one sequence of adjacent non-empty cells connecting them.
Example 1
None of the configurations of blocks below represent an ideal city: the first two on the left do not satisfy the first condition, the third one does not satisfy the second condition, and the fourth one does not satisfy either of the conditions.
Distance
When traversing the city, a hop indicates going from one block to an adjacent one.
Empty cells cannot be traversed. Let
Example 2
The configuration below represents an ideal city made of
Statement
Your task is to, given an ideal city, write a program to compute the sum of all pairwise distances between blocks
Specifically, you have to implement a routine DistanceSum(N, X, Y)
that, given
In Example 2, there are
Subtask 1 [11 points]
You may assume that
Subtask 2 [21 points]
You may assume that
Subtask 3 [23 points]
You may assume that
Additionally, the following two conditions hold: given any two non-empty cells
Subtask 4 [45 points]
You may assume that
Implementation Details
The program you submit must implement the subprogram described above using the following signature.
int DistanceSum(int N, int X[], int Y[]);
This subprogram must behave as described above. Of course you are free to implement other subprograms for its internal use. Your submissions must not interact in any way with standard input/output, nor with any other file.
Sample Grader
The sample grader will expect input in the following format:
- line
: - lines
:
Attachment Package
The sample grader along with the example test case is available here.
Comments