503 - Parallelepiped walk

All about problems in Volume 5. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
User avatar
Luchi
New poster
Posts: 3
Joined: Thu Mar 24, 2005 4:33 pm

503 - Parallelepiped walk

Post by Luchi » Mon Apr 25, 2005 2:11 pm

#include <iostream.h>

int x[3] , x1[3] , x2[3] , a[2] , b[2] , min , sum , saer ;

int main()
{
while(cin>>x[0]>>x[1]>>x[2])
{
cin>>x1[0]>>x1[1]>>x1[2]>>x2[0]>>x2[1]>>x2[2];


if(!x[0]||!x[1]||!x[2])
{min=(x1[0]-x2[0])*(x1[0]-x2[0])+(x1[1]-x2[1])*(x1[1]-x2[1])+(x1[2]-x2[2])*(x1[2]-x2[2]);cout<<min<<'\n';}
else{

if(x1[0]==0) {a[0]=0;a[1]=3;} else if(x1[0]==x[0]) {a[0]=1;a[1]=3;}
else if(x1[1]==0) {a[0]=0;a[1]=2;} else if(x1[1]==x[1]) {a[0]=1;a[1]=2;}
else if(x1[2]==0) {a[0]=0;a[1]=1;} else if(x1[2]==x[2]) {a[0]=1;a[1]=1;}

if(x2[0]==0) {b[0]=0;b[1]=3;} else if(x2[0]==x[0]) {b[0]=1;b[1]=3;}
else if(x2[1]==0) {b[0]=0;b[1]=2;} else if(x2[1]==x[1]) {b[0]=1;b[1]=2;}
else if(x2[2]==0) {b[0]=0;b[1]=1;} else if(x2[2]==x[2]) {b[0]=1;b[1]=1;}

if((a[0]==b[0])&&(a[1]==b[1])) {min=(x1[0]-x2[0])*(x1[0]-x2[0])+(x1[1]-x2[1])*(x1[1]-x2[1])+(x1[2]-x2[2])*(x1[2]-x2[2]);cout<<min<<'\n';}

else {if(b[1]!=a[1])

{
saer=-1*(3-(a[1]+b[1]));


sum=(x1[saer]-x2[saer])*(x1[saer]-x2[saer]);
if(b[0]) if(a[0]) { sum+=(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);}
else {sum+=(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer]);}
else if(a[0]) {sum+=(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);}
else {sum+=(x1[a[1]-saer]+x2[b[1]-saer])*(x1[a[1]-saer]+x2[b[1]-saer]);}
min=sum;


if(a[0]) { sum=(x[b[1]-saer]-x2[b[1]-saer]+x[saer]-x1[saer])*(x[b[1]-saer]-x2[b[1]-saer]+x[saer]-x1[saer]);}
else {sum=(x2[b[1]-saer]+x[saer]-x1[saer])*(x2[b[1]-saer]+x[saer]-x1[saer]);}
if(b[0]) { sum+=(x[a[1]-saer]-x1[a[1]-saer]+x[saer]-x2[saer])*(x[a[1]-saer]-x1[a[1]-saer]+x[saer]-x2[saer]);}
else { sum+=(x1[a[1]-saer]+x[saer]-x2[saer])*(x1[a[1]-saer]+x[saer]-x2[saer]);}
if(min>sum) min=sum;


if(a[0]) { sum=(x[b[1]-saer]-x2[b[1]-saer]+x1[saer])*(x[b[1]-saer]-x2[b[1]-saer]+x1[saer]);}
else {sum=(x2[b[1]-saer]+x1[saer])*(x2[b[1]-saer]+x1[saer]);}
if(b[0]) { sum+=(x[a[1]-saer]-x1[a[1]-saer]+x2[saer])*(x[a[1]-saer]-x1[a[1]-saer]+x2[saer]);}
else { sum+=(x1[a[1]-saer]+x2[saer])*(x1[a[1]-saer]+x2[saer]);}
if(min>sum) min=sum;


sum=(x[b[1]-saer]+x[saer]-x1[saer]+x[saer]-x2[saer])*(x[b[1]-saer]+x[saer]-x1[saer]+x[saer]-x2[saer]);
if(b[0]) if(a[0]) sum+=(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer]);
else sum+=(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);
else if(a[0]) sum+=(x1[a[1]-saer]+x2[b[1]-saer])*(x1[a[1]-saer]+x2[b[1]-saer]);
else sum+=(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);
if(min>sum) min=sum;


sum=(x[b[1]-saer]+x1[saer]+x2[saer])*(x[b[1]-saer]+x1[saer]+x2[saer]);
if(b[0]) if(a[0]) sum+=(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer]);
else sum+=(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);
else if(a[0]) sum+=(x1[a[1]-saer]+x2[b[1]-saer])*(x1[a[1]-saer]+x2[b[1]-saer]);
else sum+=(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);
if(min>sum) min=sum;

sum=a[0];a[0]=b[0];b[0]=sum;sum=a[1];a[1]=b[1];b[1]=sum;sum=x1[0];x1[0]=x2[0];x2[0]=sum;sum=x1[1];x1[1]=x2[1];x2[1]=sum;sum=x1[2];x1[2]=x2[2];x2[2]=sum;

sum=(x[b[1]-saer]+x[saer]-x1[saer]+x[saer]-x2[saer])*(x[b[1]-saer]+x[saer]-x1[saer]+x[saer]-x2[saer]);
if(b[0]) if(a[0]) sum+=(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer]);
else sum+=(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);
else if(a[0]) sum+=(x1[a[1]-saer]+x2[b[1]-saer])*(x1[a[1]-saer]+x2[b[1]-saer]);
else sum+=(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);
if(min>sum) min=sum;


sum=(x[b[1]-saer]+x1[saer]+x2[saer])*(x[b[1]-saer]+x1[saer]+x2[saer]);
if(b[0]) if(a[0]) sum+=(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x2[b[1]-saer]);
else sum+=(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x[a[1]-saer]-x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);
else if(a[0]) sum+=(x1[a[1]-saer]+x2[b[1]-saer])*(x1[a[1]-saer]+x2[b[1]-saer]);
else sum+=(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer])*(x1[a[1]-saer]+x[b[1]-saer]-x2[b[1]-saer]);
if(min>sum) min=sum;


}


else
{

sum=(x[a[1]%2]-x1[a[1]%2]+x[a[1]%2]-x2[a[1]%2]+x[3-a[1]])*(x[a[1]%2]-x1[a[1]%2]+x[a[1]%2]-x2[a[1]%2]+x[3-a[1]]);
sum+=(x1[a[1]-a[1]%2]-x2[a[1]-a[1]%2])*(x1[a[1]-a[1]%2]-x2[a[1]-a[1]%2]);
min=sum;


sum=(x1[a[1]%2]+x2[a[1]%2]+x[3-a[1]])*(x1[a[1]%2]+x2[a[1]%2]+x[3-a[1]]);
sum+=(x1[a[1]-a[1]%2]-x2[a[1]-a[1]%2])*(x1[a[1]-a[1]%2]-x2[a[1]-a[1]%2]);
if(sum<min) min=sum;


sum=(x[a[1]-a[1]%2]-x1[a[1]-a[1]%2]+x[a[1]-a[1]%2]-x2[a[1]-a[1]%2]+x[3-a[1]])*(x[a[1]-a[1]%2]-x1[a[1]-a[1]%2]+x[a[1]-a[1]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x1[a[1]-a[1]%2]+x2[a[1]%2]);
if(sum<min) min=sum;


sum=(x[a[1]-a[1]%2]-x1[a[1]-a[1]%2]+x[a[1]%2]-x2[a[1]%2]+x[3-a[1]])*(x[a[1]-a[1]%2]-x1[a[1]-a[1]%2]+x[a[1]%2]-x2[a[1]%2]+x[3-a[1]]);
sum+=(x[a[1]%2]-x1[a[1]%2]+x[a[1]-a[1]%2]-x2[a[1]-a[1]%2])*(x[a[1]%2]-x1[a[1]%2]+x[a[1]-a[1]%2]-x2[a[1]-a[1]%2]);
if(sum<min) min=sum;


sum=(x1[a[1]-a[1]%2]+x[a[1]%2]-x2[a[1]%2]+x[3-a[1]])*(x1[a[1]-a[1]%2]+x[a[1]%2]-x2[a[1]%2]+x[3-a[1]]);
sum+=(x[a[1]%2]-x1[a[1]%2]+x2[a[1]-a[1]%2])*(x[a[1]%2]-x1[a[1]%2]+x2[a[1]-a[1]%2]);
if(sum<min) min=sum;


sum=(x[a[1]-a[1]%2]-x1[a[1]-a[1]%2]+x2[a[1]%2]+x[3-a[1]])*(x[a[1]-a[1]%2]-x1[a[1]-a[1]%2]+x2[a[1]%2]+x[3-a[1]]);
sum+=(x1[a[1]%2]+x[a[1]-a[1]%2]-x2[a[1]-a[1]%2])*(x1[a[1]%2]+x[a[1]-a[1]%2]-x2[a[1]-a[1]%2]);
if(sum<min) min=sum;


sum=(x1[a[1]-a[1]%2]+x2[a[1]%2]+x[3-a[1]])*(x1[a[1]-a[1]%2]+x2[a[1]%2]+x[3-a[1]]);
sum+=(x1[a[1]%2]+x2[a[1]-a[1]%2])*(x1[a[1]%2]+x2[a[1]-a[1]%2]);
if(sum<min) min=sum;

}

cout<<min<<'\n';
}
}
}

return 0;

}

metaphysis
Experienced poster
Posts: 139
Joined: Wed May 18, 2011 3:04 pm

Re: 503 - Parallelepiped walk

Post by metaphysis » Fri Apr 21, 2017 3:57 am

Test data generator:

Code: Select all

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, char *argv[])
{
    srand(time(NULL));
    
    for (int c = 1; c <= 1000; c++)
    {
        int L = rand() % 1001, W = rand() % 1001, H = rand() % 1001;
        int x1, y1, z1, x2, y2, z2;
        
        int plane1 = rand() % 6;
        switch (plane1)
        {
            // top
            case 0:
            x1 = rand() % (L + 1), y1 = rand() % (W + 1), z1 = H;
            break;
            
            // left
            case 1:
            x1 = rand() % (L + 1), y1 = 0, z1 = rand() % (H + 1);
            break;
            
            // right
            case 2:
            x1 = rand() % (L + 1), y1 = W, z1 = rand() % (H + 1);
            break;
            
            // front
            case 3:
            x1 = L, y1 = rand() % (W + 1), z1 = rand() % (H + 1);
            break;
            
            // back
            case 4:
            x1 = 0, y1 = rand() % (W + 1), z1 = rand() % (H + 1);
            break;
            
            // bottom
            case 5:
            x1 = rand() % (L + 1), y1 = rand() % (W + 1), z1 = 0;
            break;
        }
        
        int plane2 = rand() % 6;
        switch (plane2)
        {
            // top
            case 0:
            x2 = rand() % (L + 1), y2 = rand() % (W + 1), z2 = H;
            break;
            
            // left
            case 1:
            x2 = rand() % (L + 1), y2 = 0, z2 = rand() % (H + 1);
            break;
            
            // right
            case 2:
            x2 = rand() % (L + 1), y2 = W, z2 = rand() % (H + 1);
            break;
            
            // front
            case 3:
            x2 = L, y2 = rand() % (W + 1), z2 = rand() % (H + 1);
            break;
            
            // back
            case 4:
            x2 = 0, y2 = rand() % (W + 1), z2 = rand() % (H + 1);
            break;
            
            // bottom
            case 5:
            x2 = rand() % (L + 1), y2 = rand() % (W + 1), z2 = 0;
            break;
        }
        
        cout << L << ' ' << W << ' ' << H << ' ';
        cout << x1 << ' ' << y1 << ' ' << z1 << ' ';
        cout << x2 << ' ' << y2 << ' ' << z2 << '\n';
    }
    
    return 0;
}

Post Reply

Return to “Volume 5 (500-599)”