[ZOJ1460]The Partition of a Cake
来源:互联网 发布:mysql 查看存储引擎 编辑:程序博客网 时间:2024/06/05 04:57
ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1460
有一块1000*1000的蛋糕,切几刀以后问切成了几块。
题中说:"The intersections of the cut line and the cake edge are two"即每一刀和蛋糕的边缘交点为两个。描述每一刀的时候就是用这两个交点描述的。(在台湾的一个BBS上看到有人说有的时候交点并不是两个,那么应该切的正好是蛋糕边缘)。
解题思路:
记蛋糕被切成了ans块
初始时候ans==1。
之后每切一条直线,这条直线和以往刀切的直线的不同交点数记为tmp,则ans+=tmp+1。这个规律其实我是画了几个图看出来的,不知道是不是可以证明正确性。
比较关键的是要判断线段的相交,再求交点。
做之前有同学提醒一定要注意极限数据,猜想应该有一些比较诡异的输入,所以写代码的时候对输入的切痕做了两个判断:1)是否是切在蛋糕边缘,是则忽略;2)是否和以前的某一条切痕重合,是则忽略。不知道是不是因此躲过了某些极限数据。
源代码
#include <cstdio>#include <cstdlib>#include <cstring>#include <vector>#include <cmath>#include <algorithm>using namespace std;#define maxn 20 const double EPS=1e-5;typedef struct vect{ int x,y; vect(int a=0, int b=0):x(a),y(b){} vect operator-(vect a) { return vect(x-a.x, y-a.y); } int operator/(vect a) { return x*a.y-y*a.x; } bool operator==(vect a) { return (x==a.x && y==a.y); }}vect;typedef struct dvect{ double x,y; bool operator==(dvect a) { return (fabs(x-a.x)<EPS && fabs(y-a.y)<EPS); }}dvect;typedef struct segment{ vect v1, v2; bool operator==(segment a) { return ((v1==a.v1&&v2==a.v2)||(v1==a.v2&&v2==a.v1)); }}segment;segment seg[maxn];bool check(int i){ //检查是不是和以前的重复 for (int j=0; j<i; j++) if (seg[i]==seg[j]) return false; //检查是不是切蛋糕边缘 if (seg[i].v1.x==seg[i].v2.x && (seg[i].v1.x==0 || seg[i].v1.x==1000)) return false; if (seg[i].v1.y==seg[i].v2.y && (seg[i].v1.y==0 || seg[i].v1.y==1000)) return false; return true;}int min(int x, int y){if (x>y) return y; return x;}int max(int x, int y){if (x>y) return x; return y;}int multiply(vect p1, vect p2, vect p0){ int tmp=(p1-p0)/(p2-p0); return (tmp>0)?1:(tmp==0)?0:-1;}bool cross(segment seg1, segment seg2){ return( (max(seg1.v1.x,seg1.v2.x) >= min(seg2.v1.x,seg2.v2.x))&& //u中最右的点是否在v最左的点的右边 (max(seg2.v1.x,seg2.v2.x) >=min(seg1.v1.x,seg1.v2.x))&& //v中最右的点是否在u最左的点的右边 //判断这两条线段在水平层面上是否可能相交 (max(seg1.v1.y,seg1.v2.y) >=min(seg2.v1.y,seg2.v2.y))&& //u中最上的点是否在v最下的点的上边 (max(seg2.v1.y,seg2.v2.y) >=min(seg1.v1.y,seg1.v2.y)) && //v中最上的点是否在u最下的点的上边 //判断这两条线段在垂直层面上是否可能相交 (multiply(seg1.v1, seg1.v2, seg2.v1)*multiply(seg1.v1, seg1.v2, seg2.v2)<0) && (multiply(seg2.v1, seg2.v2, seg1.v1)*multiply(seg2.v1, seg2.v2, seg1.v2)<0));}dvect crossvect(segment seg1, segment seg2){ vect p1=seg1.v1, p2=seg1.v2; vect q1=seg2.v1, q2=seg2.v2; dvect ret; double tmp1,tmp2; //求x坐标 tmp1 = (q2.x-q1.x)*(p1.y-p2.y)-(p2.x-p1.x)*(q1.y-q2.y); tmp2 = (p1.y-q1.y)*(p2.x-p1.x)*(q2.x-q1.x) + q1.x*(q2.y-q1.y)*(p2.x-p1.x)-p1.x*(p2.y-p1.y)*(q2.x-q1.x); ret.x = tmp2/tmp1; //求y坐标 tmp1 = (p1.x-p2.x)*(q2.y-q1.y)-(p2.y-p1.y)*(q1.x-q2.x); tmp2 = p2.y*(p1.x-p2.x)*(q2.y-q1.y) + (q2.x- p2.x)*(q2.y-q1.y)*(p1.y-p2.y)-q2.y*(q1.x-q2.x)*(p2.y-p1.y); ret.y = tmp2/tmp1; return ret; } int main(){ int n, ans, tmp, vnum; dvect cp; //cross point dvect v[maxn]; while (scanf("%d",&n)==1 && n) { ans=1; for (int i=0; i<n; i++) { scanf("%d%d%d%d", &seg[i].v1.x, &seg[i].v1.y, &seg[i].v2.x, &seg[i].v2.y); if (!check(i)) continue; tmp=0; vnum=0; for (int j=0, k; j<i; j++) if (cross(seg[i], seg[j])){ cp=crossvect(seg[i], seg[j]); for (k=0; k<vnum; k++) if (cp==v[k]) break; if (k==vnum) { tmp++; v[vnum++]=cp; } } ans+=tmp+1; } printf("%d\n", ans); } return 0;}
- [ZOJ1460]The Partition of a Cake
- zoj 1460 The Partition of a Cake
- ZOJ 1460 The Partition of a Cake 线段相交
- 《牛客网leetcode20题》Given a string s, partition s such that every substring of the partition is a palind
- SpokenEnglish--It's a piece of cake
- Sicily 1715. A piece of cake
- How To Change the Partition Column Of A Partitioned Table Using DBMS_Redefinition [ID 846405.1]
- How To Change the Partition Column Of A Partitioned Table Using DBMS_Redefinition [ID 846405.1]
- hdu 3097 The Partition of A Graph uva 11396 Claw Decomposition
- a piece of cake,IOS simulator横竖屏显示问题
- Piece of Cake
- Tip of today-A Piece of Cake 蛋糕我爱吃
- backup a partition data to a file and retore the partition
- How to delete the hidden partition of Win7 OS
- hoj2134 Cuts the cake
- HDU2134:Cuts the cake
- Cuts the cake
- hdu4762 Cut the Cake
- 对ERP系统进行数据安检的必要性
- AlwaysOnTop
- 涨停股第二天操作规则
- Excel 操作概括
- GCC 编译 与sizeof
- [ZOJ1460]The Partition of a Cake
- 自定义标签(1)
- 文本编码格式
- 日语口语1.8 、二年うえの先輩ですね
- 责任链模式(Chain of Responsibility)
- [ZOJ1144][POJ1104] Robbery
- FancyUpload 多文件上传
- MSN Virtual Earth
- 相关面试题 回答