The Cuboid Earth Theory
Time limit1sMemory limit128 MB
Given a cuboid and a surface point, print the square of the shortest path length along the cuboid's surface from a vertex to that point.
- Level
Medium7 of 10
- Topics
- Geometry, Math, Brute force, Shortest path
- Solved
- No attempts yet
Problem
Jaehyun has become convinced that the Earth ought to be shaped like a rectangular box. So he carried out a massive project to reshape the Earth into a rectangular cuboid whose edge lengths are . Now every location on the Earth's surface can be described by 3D Cartesian coordinates, and the cuboid occupies the region , , . Jaehyun built his house at the vertex .
Meanwhile, Seungwon's house was forcibly relocated to the coordinates by this project. Seungwon's position always lies on one of the six faces of the cuboid.
Jaehyun wants to travel to Seungwon. Since both houses are on the Earth's surface, Jaehyun may move only along the surface of the cuboid; he cannot pass through its interior. Find the length of the shortest path along the surface from to .
For example, if , , and Seungwon is at , the shortest path goes from through the surface point to , and its length is .
Input
The input consists of several test cases. Each test case is given on a single line as six integers , , , , , (). The coordinates are guaranteed to lie on one of the six faces of the cuboid. The last line of the input is , and no answer should be printed for it.
Output
For each test case, print on a single line the square of the length of the shortest surface path, as an integer. (The square of the shortest path length is always an integer, so you must print the exact integer value, not an approximation.)