【算法】shortest distance
好不容易找到的。
#include<iostream> #include<iomanip> #include<cmath> using namespace std; double a,b,c,x,y,z,ans,l,w,h; double f(double x) { return (double(x*x)); } double min(double x,double y) { if(x>y) return y; else return x; } void count1() { ans=sqrt(double(f(a-x)+f(b-y)+f(c-z))); } void count2() { double ans1,ans2; if(b==0||b==w) { ans1=sqrt(double(min(f(h-z+w+h-c),f(z+w+c))+f(x-a))); ans2=sqrt(double(min(f(l-x+w+l-a),f(x+w+a))+f(c-z))); } else if(a==0||a==l) { ans1=sqrt(double(min(f(w-y+l+w-b),f(y+l+b))+f(c-z))); ans2=sqrt(double(min(f(h-z+l+h-c),f(z+l+c))+f(b-y))); } else { ans1=sqrt(double(min(f(w-y+h+w-b),f(y+h+b))+f(x-a))); ans2=sqrt(double(min(f(l-x+h+l-a),f(x+h+a))+f(b-y))); } ans=min(ans1,ans2); } void count3() { if(a==0||a==l) { if(y==0||y==w) ans=sqrt(double(f(labs(x-a)+labs(b-y))+f(c-z))); else ans=sqrt(double(f(labs(z-c)+labs(x-a))+f(b-y))); } else if(b==0||b==w) { if(x==0||x==l) ans=sqrt(double(f(labs(x-a)+labs(b-y))+f(c-z))); else ans=sqrt(double(f(labs(c-z)+labs(b-y))+f(a-x))); } else { if(x==0||x==l) ans=sqrt(double(f(labs(c-z)+labs(x-a))+f(b-y))); else ans=sqrt(double(f(labs(c-z)+labs(b-y))+f(a-x))); } } int main() { cin>>l>>w>>h; cin>>a>>b>>c>>x>>y>>z; if((a==x&&(a==0||a==l))||(b==y&&(b==0||b==w))||(c==z&&(c==0||c==h))) count1(); else if((a==0&&x==l)||(b==0&&y==w)||(c==0&&z==h)||(a==l&&x==0)||(b==w&&y==0)||(c==h&&z==0)) count2(); else count3(); cout<<setiosflags(ios::fixed)<<setprecision(2)<<ans<<endl; return 0; }