This page is still under construction.

Parts of this page are still being built. What you see may change.

The Cuboid Earth Theory

Time limit1sMemory limit128 MB

Summary
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 ℓx×ℓy×ℓz\ell_x \times \ell_y \times \ell_z. Now every location on the Earth's surface can be described by 3D Cartesian coordinates, and the cuboid occupies the region 0≤x≤ℓx0 \le x \le \ell_x, 0≤y≤ℓy0 \le y \le \ell_y, 0≤z≤ℓz0 \le z \le \ell_z. Jaehyun built his house at the vertex (0,0,0)(0, 0, 0).

Meanwhile, Seungwon's house was forcibly relocated to the coordinates (x,y,z)(x, y, z) 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 (0,0,0)(0, 0, 0) to (x,y,z)(x, y, z).

For example, if ℓx=1\ell_x = 1, ℓy=2\ell_y = 2, ℓz=1\ell_z = 1 and Seungwon is at (1,2,1)(1, 2, 1), the shortest path goes from (0,0,0)(0, 0, 0) through the surface point (1,1,0)(1, 1, 0) to (1,2,1)(1, 2, 1), and its length is 8\sqrt{8}.

Input

The input consists of several test cases. Each test case is given on a single line as six integers ℓx\ell_x, ℓy\ell_y, ℓz\ell_z, xx, yy, zz (1≤ℓx,ℓy,ℓz≤10001 \le \ell_x, \ell_y, \ell_z \le 1000). The coordinates (x,y,z)(x, y, z) are guaranteed to lie on one of the six faces of the cuboid. The last line of the input is ℓx=ℓy=ℓz=x=y=z=0\ell_x = \ell_y = \ell_z = x = y = z = 0, 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.)

Examples3

  1. Example 1

    Input
    1 1 2 1 1 2
    1 1 1 1 1 1
    0 0 0 0 0 0
    
    Expected output
    8
    5
    
  2. Example 2

    Input
    5 3 4 5 3 0
    0 0 0 0 0 0
    
    Expected output
    34
    
  3. Example 3

    Input
    3 1 1 3 1 1
    2 4 4 2 4 4
    0 0 0 0 0 0
    
    Expected output
    13
    52