计算几何 ( 判断线段相交 )——土地划分( FZU 1015 )
2016-07-24 17:06
253 查看
题目链接:
http://acm.fzu.edu.cn/problem.php?pid=1015
分析&&题解:
需要注意不少细节,首先需要判断两条线段是否重合,然后考虑起点和终点重合的情况(结果需要减1),然后还是继续套模板。
判断线段相交 (包括端点)
AC代码:
http://acm.fzu.edu.cn/problem.php?pid=1015
分析&&题解:
需要注意不少细节,首先需要判断两条线段是否重合,然后考虑起点和终点重合的情况(结果需要减1),然后还是继续套模板。
判断线段相交 (包括端点)
AC代码:
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> using namespace std; int W,H,L; struct point { double x; double y; }Point[66]; struct line { point s; point e; }Line[100]; double multiply(point sp,point ep,point op) { return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y)); } bool intersect(line u,line v) { return( (max(u.s.x, u.e.x)>=min(v.s.x, v.e.x))&& //排斥实验 (max(v.s.x, v.e.x)>=min(u.s.x, u.e.x))&& (max(u.s.y, u.e.y)>=min(v.s.y, v.e.y))&& (max(v.s.y, v.e.y)>=min(u.s.y, u.e.y))&& (multiply(v.s, u.e, u.s)*multiply(u.e, v.e, u.s)>=0)&& //跨立实验 (multiply(u.s, v.e, v.s)*multiply(v.e, u.e, v.s)>=0)); } int main() { while(scanf("%d%d", &W, &H) && (W+H) ) { cin >> L; point s,g; scanf("%lf%lf", &s.x, &s.y); for(int i=0; i<L; i++) { //l+1个点 scanf("%lf%lf",&g.x,&g.y); Line[i].s=s; Line[i].e=g; s=g; } int cnt =1; for(int i=0;i<L;i++) { for(int j=i+1;j<L;j++) { if (Line[i].e.x==Line[j].s.x && Line[i].e.y==Line[j].s.y || Line[i].s.x==Line[j].e.x && Line[i].s.y==Line[j].e.y) continue; if( intersect(Line[i], Line[j])) { cnt++; //cout << i << " : "<< j << endl; } } } int ans =cnt+L+1; if(Point[0].x == Point[L].x && Point[0].y == Point[L].y) ans--; cout << ans<< endl; } }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法