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 ...
随机推荐
- 技海拾贝 - Android
1. 前台Service - 介绍: http://blog.csdn.net/think_soft/article/details/7299438 - 代码实例: http://blog.csdn ...
- 对象关联(associated objects)
category与associative作为objective-c的扩展机制的两个特性,category即类型,可以通过它来扩展方法:associative,可以通过它来扩展属性:在iOS开发中,可能 ...
- 新浪微博AppKey大集合(share)
本文转自:http://blog.sina.com.cn/s/blog_9e1ea13a01017y3n.html ------------------------------------------ ...
- LintCode Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- echarts图表标签(axisLabel)折行
在项目中遇到了一个echarts图表标签过长需折行的需求,so引出这篇总结,啦啦啦有帮助就多多支持啦,撒花撒花撒花~~~~ 在此不对echarts其他用法做解释,详细见echarts文档(点击前往) ...
- Java篇-File类之常用操作
/** * */ package com.io.file; import java.io.File; import java.io.IOException; /** * <pre> * & ...
- [非原创]eclipse中一些常见svn图标的含义
项目视图 The Package Explorer view - 已忽略版本控制的文件.可以通过Window → Preferences → Team → Ignored Resources.来忽 ...
- 深入剖析ConcurrentHashMap(1)
转载自并发编程网 – ifeve.com本文链接地址: 深入剖析ConcurrentHashMap(1) ConcurrentHashMap是Java5中新增加的一个线程安全的Map集合,可以用来替代 ...
- Quartus II 与 Modelsim 联调【转】
Quartus II 9.0版本的时候软件还有自带的仿真工具,现在安装的是11.0以上版本,才发现 Quartus II 11.0以上取消了软件自带的波形仿真工具,因此需要波形仿真就要调用专业的仿真工 ...
- php7 编译安装 apache
http://blog.csdn.net/21aspnet/article/details/47708763 根据此教程的步骤但是碰到了若干问题 1. 执行./configure的时候报错 大部分可 ...