PKU 1444 Parallelepiped walk

    技术2023-03-29  16

     

    Parallelepiped walk

    Description

    Two points A(x1, y1, z1) and B(x2, y2, z2) are placed on the surface of parallelepiped P = {(x, y, z): 0 <= x <= L, 0 <= y <= W, 0 <= z <= H} with L*W*H dimensions (see figure). These two points can be linked with various curves lying on the surface of P. You are to find out the square of the shortest curve length.  AB两个点在一个长宽高分别为LWH的平行六面体的表面上,如图.

    让你找出,用一根线连接AB两点,使线最短。线只能平行六面体的在表面上。 Parallelepiped dimensions L, W, H and coordinates of the points are integers, 0 <= L,W,H <= 1000. 

    其中0 <= L,W,H <= 1000

     

    Input

    Input contains (in indicated order): L, W, H, x1, y1, z1, x2, y2, z2. The numbers are separated with spaces and end-of-line characters.

    按照L,W,H,x1,y1,z1,x2,y2,z2的顺序输入,之间用空格隔开。

    Output

    Output should contain the square of the shortest curve length between points A and B on the surface of P.

    输出AB之间最短的线的“平方”。

    Sample Input

    5 5 2

    3 1 2

    3 5 0

    Sample Output

    36

     

    第一步:

             A的旋转到底面。

    1、  判断A是否在下底面或者上底面,如果不在上下底面,通过交换坐标使其在上底面或者下底面。

    2、  如果在上底面,可以通过对对称中心(l/2,w/2,h/2,做对称变换,最终使A在下底面上。

    第二步:

             walk(i,j,x1,y1,x2,y2,z2,l,w,h);

             i+1 表示底面向右下翻,长方体顺时针旋转;

             i-1 表示底面向左下翻,长方体逆时针旋转;

             j+1 表示底面向里下翻,长方体向外旋转;

             j-1 表示底面向外下翻,长方体向里旋转;

             可以证明一下结论:

    1、  沿每个方向最多翻转两次就可以得到正确答案;

    2、  即使两个点都旋转到底面时,他们直接的连线不完全在面上。那么他们之间的距离一定不是最短。

     

    注意事项:

            

     

             沿不同的面展开AB 之间的最短距离是不一样的。如果在顶点情况更复杂。

     #include<iostream> #include<algorithm> #include<stdio.h> using namespace std; const int oo=1<<30; int ans; void walk(int i,int j,int x1,int y1,int x2,int y2,int z2,int l,int w,int h) { if(z2==0) { int t=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); ans=min(ans,t); } else { if(i==0||i==1) walk( i+1, j, x1, y1-w, x2 ,z2, w-y2, l, h, w); if(i==0||i==-1) walk( i-1, j, x1, y1+h, x2, h-z2, y2, l, h, w); if(j==0||j==1) walk( i,j+1, x1+h, y1, h-z2, y2, x2, h, w, l); if(j==0||j==-1) walk( i, j-1, x1-l,y1,z2,y2,l-x2,h,w,l); } }  

     

    最新回复(0)