The Column
Задача CONSTRUCTION
Statement
Builders were putting up a column shaped like a rectangular box
{(x, y, z) | 0 <= x <= A, 0 <= y <= B, 0 <= z <= C}. To make the job
easier they need to stretch a rope of minimum length along the surface of
the column from the point (x1, y1, z1) to the point (x2, y2, z2) — both
points lying on the box's surface. The rope may run over any point of the
column's surface. Help the builders find the square of the length of rope
they need.
Input / Output
You read, in order, 9 integers separated by spaces: A B C x1 y1 z1 x2 y2 z2
(each of A, B, C does not exceed 100).
You output a single number: the square of the shortest surface path's length.
Constraints
- each of A, B, C ≤ 100 (positive integers)
- both endpoints lie on the surface of the box
[0, A] × [0, B] × [0, C]
Example
Input: 3 4 4 1 2 4 3 2 1
Output: 25
Solution idea
This is the classic "shortest path on the surface of a box" problem. First work out which face (or faces, if a point sits on an edge) each endpoint lies on. If both points share a face, the answer is just the ordinary 2D squared distance within that face. Otherwise, "unfold" the box: for every sequence of faces that connects the face of the first point to the face of the second point across shared edges, lay those faces out flat into a single plane (rotating each face about the shared edge with its neighbor, like opening a cardboard box), which turns the surface path into a straight line in 2D. Compute the straight-line squared distance for each valid unfolding — a rectangular box only has a handful of distinct face-adjacency paths worth trying — and keep the minimum. Because the box's opposite faces never need to be unfolded through more than two intermediate faces, checking all of these candidate unfoldings is enough to guarantee the true shortest path is found.