poj2977

题意:给定一个长方体,和长方体表面上的一个点。求该点在长方体表面上移动到长方体一个(确定的)顶点的最短距离。

分析:陷阱就在于可能需要经过三个面,而不是两个面,如果一个点在顶面,那么可能需要经过顶面、后面和左面,也可能经过顶面、右面和前面。

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
using namespace std;

int main()
{
//freopen("t.txt", "r", stdin);
int lx, ly, lz, x, y, z;
while (scanf("%d%d%d%d%d%d", &lx, &ly, &lz, &x, &y, &z), lx | ly | lz | x
| y | z)
{
int ans;
if (x == 0 || y == 0 || z == 0)
{
printf(
"%d\n", x * x + y * y + z * z);
continue;
}
if (z == lz && y == ly && x == lx)
{
printf(
"%d\n", min((y + z) * (y + z) + x * x, min((x + z) * (x + z)
+ y * y, (y + x) * (y + x) + z * z)));
continue;
}
if (z == lz)
{
ans
= min((y + lz) * (y + lz) + x * x, (x + lz) * (x + lz) + y * y);
ans
= min(ans, (ly + x) * (ly + x) + (ly - y + lz) * (ly - y + lz));
ans
= min(ans, (y + lx) * (y + lx) + (lx - x + lz) * (lx - x + lz));
printf(
"%d\n", ans);
continue;
}
if (y == ly)
{
ans
= min((z + ly) * (z + ly) + x * x, (x + ly) * (x + ly) + z * z);
ans
= min(ans, (lz + x) * (lz + x) + (lz - z + y) * (lz - z + y));
ans
= min(ans, (lx + z) * (lx + z) + (lx - x + y) * (lx - x + y));
printf(
"%d\n", ans);
continue;
}
if (x == lx)
{
ans
= min((y + lx) * (y + lx) + z * z, (z + lx) * (z + lx) + y * y);
ans
= min(ans, (ly + z) * (ly + z) + (ly - y + x) * (ly - y + x));
ans
= min(ans, (lz + y) * (lz + y) + (lz - z + x) * (lz - z + x));
printf(
"%d\n", ans);
continue;
}
}
return 0;
}

你可能感兴趣的:(poj)