ZOJ 1460 The Partition of a Cake 线段相交
来源:互联网 发布:o2o软件 编辑:程序博客网 时间:2024/06/06 00:38
原题见ZOJ 1460
(zoj网站经常挂,还是链到NJOJ吧)
一个正方形蛋糕边长为1000,并给了四个点位置固定。
现在上面切n条线,请问可以被切成几块。
思路
先考虑直线的切割。若在原图中已经有若干条直线,再增加一条直线,和原图有k个交点,则平面块数将增加k+1.
当交点发生重叠时,则应该增加的块数(边长为0的一块东西)不计入。
现考虑在正方形中的切割,则只需在原本增加的块数中减去2块,即向无穷远处延伸的平面不用计入。
代码
核心解释: int jo(seg C, pt &ans){}
一个线段和另一个线段的相交情况。
先判断跨接是否成立,再判断两者是否平行。判断交点是否为端点。排除所有特殊情况后,再按照一般情况解出交点。交点必须在线段内。
附代码:
/*-------------------------------------------- * File Name: ZOJ 1460 * Author: Danliwoo * Mail: Danliwoo@outlook.com * Created Time: 2016-04-03 22:22:38--------------------------------------------*/#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <cmath>using namespace std;#define N 100#define eps 1e-8int delta(double x){ return fabs(x) < eps ? 0 : (x > 0 ? 1 : -1);}struct pt{ double x, y; pt(){} pt(double x, double y): x(x), y(y){} void sc(){ scanf("%lf%lf", &x, &y); } bool eq(pt a){ return !delta(x-a.x) && !delta(y-a.y); } double operator * (const pt &b){ return x*b.y-y*b.x; } pt operator - (const pt &b){ return pt(x-b.x, y-b.y); }}p[N], q[N*N];struct seg{ pt a, b; seg(){} seg(pt a, pt b) : a(a), b(b){} int jo(seg C, pt &ans){ pt c = C.a, d = C.b; if(max(a.x,b.x) < min(c.x,d.x)|| max(a.y,b.y) < min(c.y,d.y)|| max(c.x,d.x) < min(a.x,b.x)|| max(c.y,d.y) < min(a.y,b.y)) return 0; int x = delta((b-a)*(c-a)), y = delta((b-a)*(d-a)); if(x == 0 && y == 0) return -1; if(x == 0) ans = c; else if(y == 0) ans = d; else{ ans.x =-(c*d*(a.x-b.x)-a*b*(c.x-d.x))/((a-b)*(c-d)); ans.y =(a*b*(c.y-d.y)-c*d*(a.y-b.y))/((a-b)*(c-d)); } if(delta(ans.x-max(a.x,b.x)) > 0 || delta(ans.x-min(a.x,b.x))<0) return 0; return 1; }}s[N];bool cmp(pt a, pt b){ return delta(a.x-b.x) ? a.x < b.x : a.y < b.y;}int main(){ int n; s[0] = seg(pt(0,0), pt(0,1000)); s[1] = seg(pt(0,1000), pt(1000,1000)); s[2] = seg(pt(1000,1000), pt(1000,0)); s[3] = seg(pt(1000,0), pt(0,0)); while(scanf("%d", &n), n){ for(int i = 4;i < n+4;i++){ p[2*i].sc(); p[2*i+1].sc(); s[i] = seg(p[2*i], p[2*i+1]); } int ans = 1, flag; n += 4; for(int i = 4;i < n;i++){ int cnt = 0, qn = 0; for(int j = 0;j < i;j++){ pt a; flag = s[i].jo(s[j], a); if(flag == -1) break; else if(flag == 1) q[qn++] = a; } if(flag == -1) continue; ans += qn-1; sort(q, q+qn, cmp); for(int j = 1;j < qn;j++) if(q[j].eq(q[j-1])) ans--; } printf("%d\n", ans); } return 0;}
0 0
- ZOJ 1460 The Partition of a Cake 线段相交
- zoj 1460 The Partition of a Cake
- [ZOJ1460]The Partition of a Cake
- zoj 2325 线段相交
- zoj 2551 判线段相交
- zoj 3511 Cake Robbery(线段树)
- ZOJ 3511 Cake Robbery(线段树)
- ZOJ 2706 Thermal Death of the Universe 线段树
- ZOJ 2706 Thermal Death of the Universe(线段树)
- ZOJ 2706 Thermal Death of the Universe (线段树)
- ZOJ 1648 Circuit Board(判断线段相交)
- zoj 1010 Area【线段相交问题】
- zoj 1158 判断2线段完全相交
- ZOJ 1648 Circuit Board(线段相交判定)
- zoj 2870 Segments 线段与直线相交
- Cake ZOJ
- Cake ZOJ
- 《牛客网leetcode20题》Given a string s, partition s such that every substring of the partition is a palind
- 自考那些事儿(六):计算机网络原理(总述篇)
- 数据结构与算法分析(读书笔记):1.引论
- 页面布局学习(一)
- 笔记---main函数及其他问题
- opencv笔记02-main函数使用
- ZOJ 1460 The Partition of a Cake 线段相交
- android设计模式应用--解释器模式
- x86汇编--(四)新的世界 字符转换技巧
- 排序算法之插入排序
- 阿拉伯数字与中文数字的转换----------相互转化
- cocos2d-x中的引用计数原理
- 第一章、标识符与关键字
- 螺旋矩阵(c语言实现)
- fadeIn,fadeOut函数 图片轮播