Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dog and Gopher
09-18-2009, 06:59 PM
Post: #1
Dog and Gopher
A large field has a dog and a gopher. 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. The dog heads at double the speed of the gopher to the hole. If the dog reaches the hole first, the gopher gets gobbled up; otherwise, the gopher escapes. You have been retained by the gopher to select a hole through which it can escape, if such a hole exists.
Input
The first line contains one integer and four floating point numbers. The integer n denotes how many holes are that gopher can escape through. The four floating point numbers denote the (x, y) coordinates of the gopher followed by the (x, y) coordinates of the dog. The 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 millimeter.
Output
Print a single line. If the gopher can escape, the output line should read, “The gopher can escape through the hole at (x, y).” while identifying the appropriate hole to the nearest millimeter. Otherwise, the output line should read, “The gopher cannot escape.” If the gopher can escape through more than one hole, report the one that appears first in the input. There are at most 1,000 gopher holes in a set
of input and all coordinates range between –10,000 and +10,000.
Sample Input
1 1.000 1.000 2.000 2.000
1.500 1.500
Sample Output
The gopher cannot escape.
Sample Input
2 2.000 2.000 1.000 1.000
1.500 1.500
2.500 2.500
Sample Output
The gopher can escape through the hole at (2.500,2.500).
Solution
If euclidian distance between two points, gopher and dog is in order 2*g=d then gopher can not escape. I used records to record each point, like Tpoint in Delphi and record, like struct in C++ !
So here is the code in TPW :
Code:
program dog_and_gopher;
uses wincrt;
type
  tpoint=record
    x,y:real;
  end;
var
n,i:longint;
x1,y1,x2,y2:real;
solution_found:boolean;
a:array[1..100] of tpoint;
solution:tpoint;
function euclidian_distance(x1,y1,x2,y2:real):real;
begin
  euclidian_distance:=sqrt(sqr(x1-x2)+sqr(y1-y2));
end;
begin
readln(n,x1,y1,x2,y2);
for i:=1 to n do
   readln(a[i].x,a[i].y);
solution_found:=false;
for i:=1 to n do
   if not solution_found then
     if 2*euclidian_distance(x1,y1,a[i].x,a[i].y)<=
       euclidian_distance(x2,y2,a[i].x,a[i].y)  then
         begin
          solution_found:=true;
          with solution do
            begin
             x:=a[i].x;
             y:=a[i].y;
            end;
         end;
if not solution_found then
  writeln('The gopher cannot escape.')
else
  writeln('The gopher can escape through the hole at (',solution.x:8:3,',',solution.y:8:3,').');
end.

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: