您的位置:首页 > 运维架构

UVA10310 Dog and Gopher【Ad Hoc+水题】

2018-02-12 07:09 609 查看
A large field has a gopher and a dog. The dog wants to eat the gopher, while the gopher wants to run to safety through one of several gopher holes dug in the surface of the field.
  Neither the dog nor the gopher is a math major; however, neither is entirely stupid. The gopher decides on a particular gopher hole and heads for that hole in a straight line at a fixed speed. The dog, which is very good at reading body language, anticipates which hole the gopher has chosen, and heads at double the speed of the gopher to the hole, where it intends to gobble up the gopher. If the dog reaches the hole first, the gopher gets gobbled; otherwise, the gopher escapes.
You have to select a hole for the gopher through which it can escape, if such a hole exists.
Input
The input file contains several sets of input. The first line of each set contains one integer and four floating point numbers. The integer n denotes how many holes are in the set and the four floating point numbers denote the (x, y) coordinates of the gopher followed by the (x, y) coordinates of the dog. Subsequent n lines of input each contain two floating point numbers: the (x, y) coordinates of a gopher hole. All distances are in meters; to the nearest mm. Input is terminated by end of file. There is a blank line between two consecutive sets.
Output
Your should output a single line for each set of input. For each set, if the gopher can escape the output line should read ‘The gopher can escape through the hole at (x, y).’ identifying the appropriate hole to the nearest mm. Otherwise the output line should read ‘The gopher cannot escape.’ If the gopher may escape through more than one hole, report the one that appears first in the input. There are not more than 1000 gopher holes in a set of input and all coordinates are between -10000 and +10000.
Sample Input
1 1.000 1.000 2.000 2.000
1.500 1.500
2 2.000 2.000 1.000 1.000
1.500 1.500
2.500 2.500
Sample Output
The gopher cannot escape.
The gopher can escape through the hole at (2.500,2.500).

问题链接UVA10310 Dog and Gopher
问题简述
  一只狗和一只地鼠,地鼠有n个洞。给出狗和地鼠初始的位置即坐标位置,给出n个洞的坐标位置,问地鼠能否安全逃到一个洞中。

问题分析

  模拟计算即可。
  需要注意,计算距离时不要使用开方,否则会发生精度丢失。不开方数的进行比较其结果也是一样的。
程序说明:(略)
题记:(略)
参考链接:(略)

AC的C++语言程序如下:/* UVA10310 Dog and Gopher */

#include <bits/stdc++.h>

using namespace std;

struct _point {
double x, y;
};

const int N = 1000;
_point p
;

double dist(_point a, _point b )
{
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

int main()
{
int n;
while(~scanf("%d", &n)) {
_point gopher, dog;

scanf("%lf%lf%lf%lf", &gopher.x, &gopher.y, &dog.x, &dog.y);

for(int i=0; i<n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);

int ans = -1;
for(int i=0; i<n; i++)
if(4.0 * dist(p[i], gopher) <= dist(p[i], dog)) {
ans = i;
break;
}

if(ans == -1)
printf("The gopher cannot escape.\n");
else
printf("The gopher can escape through the hole at (%.3f,%.3f).\n", p[ans].x, p[ans].y);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: