zoukankan      html  css  js  c++  java
  • ZOJ Problem Set–1860 Dog & Gopher

    Time Limit: 2 Seconds      Memory Limit: 65536 KB


    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, 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 been retained by the gopher to select a hole through which it can escape, if such a hole exists.

    Input

    The first line of input contains four floating point numbers: the (x,y) coordinates of the gopher followed by the (x,y) coordinates of the dog. Subsequent lines of input each contain two floating point numbers: the (x,y) coordinates of a gopher hole. All distances are in metres, to the nearest mm.

    Input contains multiple test cases. Subsequent test cases are separated with a single blank line.

    Output

    Your output for each test case should consist of a single line. If the gopher can escape the 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, choose the first one. There are not more than 1000 gopher holes and all coordinates are between -10000 and +10000.

    Sample Input
    1.000 1.000 2.000 2.000
    1.500 1.500

    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).


    Source: University of Waterloo Local Contest 1999.09.25

      1: #include<iostream>
    
      2: #include<string>
    
      3: #include<sstream>
    
      4: #include<limits>
    
      5: #include<stdio.h>
    
      6: using namespace std;
    
      7: double distance(double x1, double y1, double x2, double y2)
    
      8: {
    
      9:   return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
    
     10: }
    
     11: int main(void)
    
     12: {
    
     13:   string coordinates;
    
     14:   while(getline(cin, coordinates))
    
     15:   {
    
     16:     if(coordinates == "") continue;
    
     17:     string holePosition;
    
     18:     double x_dog, y_dog, x_gopher, y_gopher;
    
     19:     double x_hole, y_hole;
    
     20:     istringstream is(coordinates);
    
     21:     is>>x_gopher>>y_gopher>>x_dog>>y_dog;
    
     22:     bool catched = true;
    
     23:     double nearestDistance = -1,crtDistance, x_nearest, y_nearest;
    
     24:     while(getline(cin, holePosition))
    
     25:     {
    
     26:       if(holePosition == "") break;
    
     27:       istringstream is0(holePosition);
    
     28:       is0>>x_hole>>y_hole;
    
     29:       crtDistance = distance(x_gopher, y_gopher, x_hole, y_hole);
    
     30:       if(distance(x_dog, y_dog, x_hole, y_hole) > 4*distance(x_gopher, y_gopher, x_hole, y_hole))
    
     31:       {
    
     32:         catched = false;
    
     33:         if(nearestDistance > crtDistance)
    
     34:         {
    
     35:           nearestDistance = crtDistance;
    
     36:           x_nearest = x_hole;
    
     37:           y_nearest = y_hole;
    
     38:         }
    
     39:       }
    
     40:     }
    
     41:     if(!catched)
    
     42:     {
    
     43:       printf("The gopher can escape through the hole at (%.3f,%.3f).\n", x_nearest, y_nearest);
    
     44:     }
    
     45:     else
    
     46:     {
    
     47:       cout<<"The gopher cannot escape."<<endl;
    
     48:     }
    
     49:   }
    
     50:   return 0;
    
     51: }
  • 相关阅读:
    2018年左其盛读过评过的书(持续更新中)
    2星|《用场景营销引爆你的生意》:总共4个推荐案例,3个已经失败
    2018左其盛经管新书差评榜(持续更新中)
    3星|《十大全球CEO亲授企业高速成长的关键战略》:作为CEO,我也非常坦率地表明过家庭优先于工作
    2018左其盛好书榜(持续更新中)
    3星|《你的品牌需要一个讲故事的人》:有理论没案例
    《思考快与慢》前传,两位天才犹太心理学家的传奇人生与学术故事:4星|《思维的发现》
    C#如何在派生类中不显示父类的一些属性以及TypeDescriptor使用
    在XML里的XSD和DTD以及standalone的使用
    数据库操作之简单带参操作
  • 原文地址:https://www.cnblogs.com/malloc/p/2516004.html
Copyright © 2011-2022 走看看