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;
}
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;
}
相关文章推荐
- uva 10310 - Dog and Gopher
- UVa 10310 Dog and Gopher(long long)
- UVa 10310 Dog and Gopher(double)
- UVA 10310 - Dog and Gopher
- UVa 10310 - Dog and Gopher
- UVa Problem 10310 Dog and Gopher (狗拿地鼠)
- HDU1200 POJ2039 ZOJ2208 UVALive3084 To and Fro【密码+水题】
- (挑战编程_13_1)Dog and Gopher
- UVa 10610 - Gopher and Hawks
- UVA 10491 Cows and Cars——概率水题
- UVALive 7035 Built with Qinghuai and Ari Factor(水题)
- UVALive2287 POJ1047 HDU1313 ZOJ1073 Round and Round We Go【大数+数学计算+水题】
- UVA10300 Ecological Premium【Ad Hoc+水题】
- POJ3438 ZOJ2886 UVALive3822 Look and Say【数列+水题】
- Dog and Gopher
- UVA10018 Reverse and Add【回文数+水题】
- Codeforces #269 (Div. 2) B. MUH and Important Things(水题:麻烦)
- UVA1394 And Then There Was One
- 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
- uva 10246 Asterix and Obelix(最短路问题拓展 dijkstra)