Say Cheese

Once upon a time, in a giant piece of cheese, there lived a cheese mite named Amelia Cheese Mite.
Amelia should have been truly happy because she was surrounded by more delicious cheese than she
could ever eat. Nevertheless, she felt that something was missing from her life.
One morning, her dreams about cheese were interrupted by a noise she had never heard before. But
she immediately realized what it was - the sound of a male cheese mite, gnawing in the same piece of
cheese! (Determining the gender of a cheese mite just by the sound of its gnawing is by no means easy,
but all cheese mites can do it. That’s because their parents could.)
Nothing could stop Amelia now. She had to meet that other mite as soon as possible. Therefore
she had to find the fastest way to get to the other mite. Amelia can gnaw through one millimeter of
cheese in ten seconds. But it turns out that the direct way to the other mite might not be the fastest
one. The cheese that Amelia lives in is full of holes. These holes, which are bubbles of air trapped
in the cheese, are spherical for the most part. But occasionally these spherical holes overlap, creating
compound holes of all kinds of shapes. Passing through a hole in the cheese takes Amelia essentially
zero time, since she can fly from one end to the other instantly. So it might be useful to travel through
holes to get to the other mite quickly.
For this problem, you have to write a program that, given the locations of both mites and the holes
in the cheese, determines the minimal time it takes Amelia to reach the other mite. For the purposes of
this problem, you can assume that the cheese is infinitely large. This is because the cheese is so large
that it never pays for Amelia to leave the cheese to reach the other mite (especially since cheese-mite
eaters might eat her). You can also assume that the other mite is eagerly anticipating Amelia’s arrival
and will not move while Amelia is underway.
Input
The input file contains descriptions of several cheese mite test cases. Each test case starts with a line
containing a single integer n (0 ≤ n ≤ 100), the number of holes in the cheese. This is followed by
n lines containing four integers xi
, yi
, zi
, ri each. These describe the centers (xi
, yi
, zi) and radii ri
(ri > 0) of the holes. All values here (and in the following) are given in millimeters.
The description concludes with two lines containing three integers each. The first line contains the
values xA, yA, zA, giving Amelia’s position in the cheese, the second line containing xO, yO, zO, gives
the position of the other mite.
The input file is terminated by a line containing the number ‘-1’.
Output
For each test case, print one line of output, following the format of the sample output. First print the
number of the test case (starting with 1). Then print the minimum time in seconds it takes Amelia
to reach the other mite, rounded to the closest integer. The input will be such that the rounding is
unambiguous.
Sample Input
1
20 20 20 1
0 0 0
0 0 10
1
5 0 0 4
0 0 0
10 0 0
-1
Sample Output
Cheese 1: Travel time = 100 sec
Cheese 2: Travel time = 20 sec
一个奶酪,有洞,老鼠在没洞的地方走要10s一单位,在洞内行走不费时间(瞬间移动),问到目标点消耗的最短时间。
一开始并没有思路,然后发现xyz是没有限制的,说明坐标是直接计算的,然后发现洞的个数是有限的,于是就想到,把起始点和终点再加上洞的球心,就构成了图,至于距离就很好解决了。
小tips,注意球相交的情况,坑了一发……

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
struct Node{
    ll x, y, z, r;
}node[105];
int leap[105];
double d[105][105], a[105];
int main()
{
    ll i, j, m, n, ans, t, kase = 1;
    while (scanf("%lld", &n) != EOF)
    {
        if (n == -1)break;
        for (i = 1; i <= n; i++)
            cin >> node[i].x >> node[i].y >> node[i].z >> node[i].r;
        cin >> node[0].x >> node[0].y >> node[0].z;
        cin >> node[n + 1].x >> node[n + 1].y >> node[n + 1].z;
        node[0].r = node[n + 1].r = 0;
        for (i = 0; i <= 102;i++)
        for (j = 0; j <= 102; j++)
            d[i][j] = inf;
        for (i = 0; i <= n + 1;i++)
        for (j = 0; j <= n + 1; j++)
        {
            double tempx, tempy, tempz;
            tempx = (node[i].x - node[j].x)*(node[i].x - node[j].x);
            tempy = (node[i].y - node[j].y)*(node[i].y - node[j].y);
            tempz = (node[i].z - node[j].z)*(node[i].z - node[j].z);
            d[i][j] = d[j][i] = max(sqrt(tempx + tempy + tempz) - node[i].r - node[j].r, 0.0);
        }
        ll  k;
        for (k = 0; k <= n + 1;k++)
        for (i = 0; i <= n + 1;i++)
        for (j = 0; j <= n + 1; j++)
            d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

        double ans1;
        ans1 = d[0][n + 1] * 10.0;
        ans = (int)(ans1 + 0.5);
        cout << "Cheese " << kase << ": Travel time = ";
        kase++;
        cout << ans;
        cout << " sec" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值