zoukankan      html  css  js  c++  java
  • UESTCoj 1874 Wally World (计算几何)

    http://acm.uestc.edu.cn/problem.php?pid=1874&cid=217

    Wally World

    Time Limit: 1000 ms Memory Limit: 65535 kB Solved: 26 Tried: 135

    Description

    Two star-crossed lovers want to meet. The two lovers are standing at distinct points in the plane (but then again, aren't we all?). They can travel freely except that there is a single wall which cannot be crossed. The wall is a line segment which is parallel to either the x or y axis.
    Each lover can move 1 unit in 1 second. How long will it take them to be together if they both choose the best path?

    Input

    Input for each test case will consist of two lines each containing four integers. The rst two integers will specify the x and y coordinates of the rst lover; the next two integers will specify the x and y coordinates of the second lover. The next four integers will specify the start and end points of the wall. Furthermore, in all cases both lovers will not be on the (in nite) line containing the wall | that is, the wall extended in both directions. All coordinates will be positive and less than or equal to 10000 and neither lover will start on the wall. The input will be terminated by a line containing four zeroes.

    Output

    For each test case, output the minimum time in seconds for the two lovers to meet. Print the answer to exactly 3 decimal places, using the output format shown in the example.

    Sample Input

    5 2 7 2
    1 1 1 100
    1 2 3 2
    2 1 2 100
    0 0 0 0

    Sample Output

    Case 1: 1.000
    Case 2: 1.414

    思路:

    求两点的最短距离,两点中间有一堵墙,需要判断,是否能够直接到达,能则就是两点距离,若不能,则要绕过墙的最短距离,然后除以2

    输入:

    x1 y1 x2 y2 //两恋人的坐标

    x3 y3 x4 y4 //这堵墙的两端点坐标

    分析:

    求出(x1,y1),(x2,y2) 所表示的直线y=func(x),若x3=x4,将x3代入y=func(x)求的y,然后判断,y是否在区间[y1,y2],[y3,y4]之内,x3是否在区间[x1,x2]之内,只有同时满足这三个区间,才能算上线段相交了,否则不相交;

    另外,需要考虑的有,直线的斜率是否存在的问题,当斜率不在,则需要进行特判,这一步很重要。

    下面给出AC代码:

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <iostream>
      4 #include <math.h>
      5 using namespace std;
      6 #define INF 9999999999
      7 
      8 //判断x是不是在a,b之间
      9 int isOK(double x,int a,int b)
     10 {
     11     int temp;
     12     if(a>b)
     13     {
     14         temp=a,a=b,b=temp;
     15     }
     16     if(x<=b&&x>=a)
     17     {
     18         return 1;
     19     }
     20     return 0;
     21 }
     22 
     23 //求两点的距离
     24 double distance(int x1,int y1,int x2,int y2)
     25 {
     26     return sqrt((double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
     27 }
     28 
     29 //返回较小的数
     30 double mind(double a,double b)
     31 {
     32     return a<b?a:b;
     33 }
     34 
     35 //处理函数
     36 double deal(double x,int a,int b,double y,int a1,int b1,double xx,int a2,int b2,int x1,int x2,int x3,int x4,int y1,int y2,int y3,int y4)
     37 {
     38     double res;
     39     if(isOK(x,a,b)&&isOK(y,a1,b1)&&isOK(xx,a2,b2))
     40     {
     41         double d1=distance(x1,y1,x3,y3)+distance(x2,y2,x3,y3);
     42         double d2=distance(x1,y1,x4,y4)+distance(x2,y2,x4,y4);
     43         res=mind(d1,d2);
     44     }
     45     else
     46     {
     47         res=distance(x1,y1,x2,y2);
     48     }
     49     return res;
     50 }
     51 
     52 int main()
     53 {
     54     int x1,x2,x3,x4,y1,y2,y3,y4;
     55     int cnt=1;
     56     while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2)&&(x1+x2+y1+y2))
     57     {
     58         scanf("%d%d%d%d",&x3,&y3,&x4,&y4);
     59         double k,b;
     60         if(x1==x2)  //当斜率不存在时
     61         {
     62             k=INF;
     63             b=0;
     64         }
     65         else
     66         {
     67             k=((double)(y1-y2))/(x1-x2);
     68             b=y1-k*x1;
     69         }
     70         double y,x;
     71         double res;
     72         if(x3==x4)
     73         {
     74             if(x1==x2)  //当斜率不存在,并且另外一条线段斜率也不存在,则两线段平行,结果就是两点的距离
     75             {
     76                 res=distance(x1,y1,x2,y2);
     77                 printf("Case %d: %.3lf\n",cnt++,res/2.0);
     78                 continue;
     79             }
     80             y=k*x3+b;
     81             res=deal(y,y1,y2,x3,x1,x2,y,y3,y4,x1,x2,x3,x4,y1,y2,y3,y4);
     82         }
     83         else if(y3==y4)
     84         {
     85             if(x1==x2)  //斜率不存在,但是两直线垂直
     86             {
     87                 x=x1;
     88             }
     89             else if(fabs(k)<1e-6)
     90             {
     91                 x=b;
     92             }
     93             else
     94             {
     95                 x=((double)(y3-b))/k;
     96             }
     97             res=deal(x,x1,x2,y3,y1,y2,x,x3,x4,x1,x2,x3,x4,y1,y2,y3,y4);
     98         }
     99         printf("Case %d: %.3lf\n",cnt++,res/2.0);
    100     }
    101 
    102     return 0;
    103 }
  • 相关阅读:
    [转载]每天要说无数次的话,原来英语这么说
    [转载]What I Have Lived For 一生何求?
    WIN 7 和fedora双系统引导问题
    [转载]网站建设中一些容易被忽视的问题
    linux进程控制-wait()
    shell(2)
    电脑英语缩略词
    shell(1)
    [转载]无论成败 但求做最好的自己
    [转载]C++出错提示英汉对照表
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3113904.html
Copyright © 2011-2022 走看看