nyoj 142, poj 1039 ,hdu 1454 管道问题
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 管道问题的更多相关文章
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- 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 ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- poj 1039 Pipe(叉乘。。。)
题目:http://poj.org/problem?id=1039 题意:有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从 ...
- POJ - 1039 Pipe(计算几何)
http://poj.org/problem?id=1039 题意 有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从左边入 ...
- POJ 1039 Pipe【经典线段与直线相交】
链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- poj和hdu部分基础算法分类及难度排序
最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...
- POJ 1308&&HDU 1272 并查集判断图
HDU 1272 I - 小希的迷宫 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- mybatis:Invalid bound statement (not found)
[常规解决办法] 如果出现: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 一般的原因 ...
- Xcode 6制作动态及静态Framework
技术交流新QQ群:414971585 有没有写SDK或者要将一些常用的工具类做成Framework的经历? 你或许自己写脚本完成了这项工作,相信也有很多的人使用 iOS-Universal-Frame ...
- 【转载】Python的包管理工具Pip
接触了Ruby,发现它有个包管理工具RubyGem很好用,并且有很完备的文档系统http://rdoc.info 发现Python下也有同样的工具,包括easy_install和Pip.不过,我没有细 ...
- Java数据类型、变量、运算符、语句。
数据类型:整型 int long short byte小数 double float 字符 char 转义字符:\'(单引号字符) \\(反斜杠字符) \n(换行) \r(回车) \t(水平制表符,相 ...
- 启动odoo-10.0成功,但是访问时出错
启动odoo-10.0显示成功 2017-01-05 06:49:48,211 532 INFO ? odoo: Odoo version 10.02017-01-05 06:49:48,211 53 ...
- AngularJS + Java---前台网页与后台数据库传递数据 基本结构
第一个关于这两种语言的项目,以下只是我自己的理解,欢迎指教:) 基本对应关系 1. controller .jsp(.html) ng-controller="controllerTest ...
- Linear Algebra lecture 2 note
Lecture2 Elimination Inverses Permutation 消元法介绍(elimination): 有方程组 提取系数,形成矩阵为: 消元的思想跟解方程组中先消除未知数的思路一 ...
- string.join(iterable)
str.join(iterable) Return a string which is concatenation the of the strings in the iterable iterab ...
- codeMirror的简单使用,js比较文本差异(标注出增删改)
最近项目需要使用比较文本的差异的功能,在同事的推荐下,使用js脚本来比较,所以codeMirror变成了选择. 当然codeMirror中有其他功能,比较文本差异的只是其中一个功能,本人不在此做介绍, ...
- HTML的文本格式化
文本格式化:<html> <body> <b>This text is bold</b> <br /> <strong>This ...