http://acm.nyist.net/JudgeOnline/problem.php?pid=142

第一道解析几何问题,比较纠结,主要是几个解析几何的基本操作,包括求两线段的叉积,判断左右方向,判断是否相交, 计算交点等等,主要的思路就是穷举所有的折点的组合,取任意两个折点组成一条线,看看能不能跟所有的管道的上下折点构成的线段相交,如果所有都相交则说明光线能通过,否则求出所有光线中照的最远的那个(穷举的过程中设置一个记录变量) 这里就需要计算两个线段的交点(如果不能穿过管道,就要计算管道壁和光线的交点)。

#include<stdio.h>
#include<math.h>
struct point
{
double x, y;
};
double det(double x1, double y1, double x2, double y2) //计算叉积
{
return x1 * y2 - x2 * y1;
}
double cross(point a, point b, point p) //判断左右方向
{
return det(b.x - a.x, b.y - a.y, p.x - a.x, p.y - a.y);
}
double check(point a1, point a2, point b1, point b2) //判断是否相交
{
return cross(a1, a2, b1) * cross(a1, a2, b2);
}
double getarea(point a, point b, point c)
{
double ab = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
double ac = sqrt((a.x - c.x) * (a.x - c.x) + (a.y - c.y) * (a.y - c.y));
double bc = sqrt((c.x - b.x) * (c.x - b.x) + (c.y - b.y) * (c.y - b.y));
double q = (ab + ac + bc) / 2;
return sqrt(q * (q - ab) * (q - ac) * (q - bc));
}
double calculate(point a1, point a2, point b1, point b2)//计算x坐标值
{
double s1 = getarea(a1, a2, b1);
double s2 = getarea(a1, a2, b2);
return (s1 * b2.x + s2 * b1.x) / (s1 + s2);
}
int main()
{
int n;
while(scanf("%d", &n), n != 0)
{
int i;
point pip[25][2];
for(i = 0; i < n; i++)
{
scanf("%lf %lf", &pip[i][0].x, &pip[i][0].y);
pip[i][1].x = pip[i][0].x;
pip[i][1].y = pip[i][0].y - 1;
}
int j;
double max_x = -0x7fffffff;
bool flag = false;
point a, b;
for(i = 0; i < n && flag == false; i++)
{
int index1, index2;
int temp = 0;
for(temp = 0; temp < 2; temp ++)
{
a = pip[i][temp];
for(index1 = i + 1 ; index1 < n && flag == false; index1 ++)
{
for(index2 = 0; index2 < 2 && flag == false; index2 ++)
{
b = pip[index1][index2];
if( check(a, b, pip[0][0], pip[0][1]) - 0 < 1e-6 )
{
for(j = 1; j < n; j++)
{
if( check(a, b, pip[j][0], pip[j][1]) - 0 > 1e-6)
{
double x;
if(cross(a, b, pip[j][0]) > 0)
x = calculate(a, b, pip[j - 1][1], pip[j][1]);
else
x = calculate(a, b, pip[j - 1][0], pip[j][0]);
if(x > max_x)
max_x = x;
break;
}
}
if(j == n)
flag = true;
}
}
}
}
}
if(flag == true)
printf("Through all the pipe.\n");
else
printf("%.2lf\n", max_x);
}
return 0;
}

  

nyoj 142, poj 1039 ,hdu 1454 管道问题的更多相关文章

  1. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  2. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  3. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  4. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  5. poj 1039 Pipe(叉乘。。。)

    题目:http://poj.org/problem?id=1039 题意:有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从 ...

  6. POJ - 1039 Pipe(计算几何)

    http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入 ...

  7. POJ 1039 Pipe【经典线段与直线相交】

    链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  8. poj和hdu部分基础算法分类及难度排序

    最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...

  9. POJ 1308&&HDU 1272 并查集判断图

      HDU 1272 I - 小希的迷宫 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. ARP 扫描主机学习笔记

    1.通用套接字地址结构与具体套接字地址结构之间可相互转化 1)通用转具体,某些函数将结果存储在通用套接字地址结构中,这时将通用转换为具体,具体通过访问成员名可以方便的得到结果. 2)具体转通用,为了消 ...

  2. svm损失函数

    作者:杜客链接:https://zhuanlan.zhihu.com/p/20945670来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. SVM的损失函数定义如下: 举 ...

  3. WebView 载入本地的html

    1.可以是用loadData,这种方法需要先将html文件读取出来,以字符串传入loadData,可以展示页面,但是不会引用css.js等文件. 2.使用loadUrl,不过需要注意,这里因为是使用本 ...

  4. python 后台爆破工具

    sys:使用sys模块获得脚本的参数 queue模块,创建一个“队列”对象 time 模块     Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数). fin ...

  5. 防止sql注入

    sqlmap 较专业的sql注入工具YII2  activeform   注意传过来的modle的rules规则 <?php$form=\yii\widgets\ActiveForm::begi ...

  6. Silverlight RadChart :创建十字定位&圈选

    //图像加载 void Chart_Loaded(object sender, RoutedEventArgs e) { var plotAreaPanel = this.radChart.Defau ...

  7. LeetCode-Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. public ...

  8. 成员资格、授权 – ASP.NET MVC 4 系列

           ASP.NET MVC 不像 ASP.NET WEB FORMS 那样提供了很多自动保护机制来保护页面不受恶意用户的攻击,更明确的说,后者是致力于使应用程序免受攻击: 服务器组件对显示的 ...

  9. Android学习十二:自定义控件学习

    自定义控件: 1.定义控件的属性,atts.xml 2.代码实现自定义控件的控制 3.引用该控件 首先定义相关的属性 <?xml version="1.0" encoding ...

  10. JS正则截取两个字符串之间的字符串

    match方法 var str = "iid0000ffr"; var substr = str.match(/id(\S*)ff/); console.log(substr) 返 ...