POJ 计算几何专项训练(1) 【2318】【2398】【3304】【2653】【1556】【1066】...
POJ 2318 TOYS
題意是在一個(gè)大矩形里有n條分割線把矩形分割成n+1部分、再給出一些玩具的坐標(biāo)、要求統(tǒng)計(jì)每個(gè)部分內(nèi)有多少個(gè)玩具、
具體做法就是二分求解出當(dāng)前玩具右方的第一條線、這可以用叉積判斷、
Code:
varres:array [0..5002] of longint;p:array [0..5002] of record x1,x2,y1,y2:double;end;xx,yy,ldx,ldy,rux,ruy:double;n,m,i,l,r,mid:longint; function crossp(x1,y1,x2,y2:double):double;begincrossp:=x1*y2-x2*y1;end; function ok(now:longint):boolean;beginif crossp(p[now].x1-xx,p[now].y1-yy,p[now].x2-xx,p[now].y2-yy)>0 then exit(true) else exit(false);end; beginread(n);while n<>0 dobeginreadln(m,ldx,ruy,rux,ldy);for i:=1 to n dobeginreadln(p[i].x2,p[i].x1);p[i].y1:=ldy;p[i].y2:=ruy;end;p[0].x1:=ldx;p[0].x2:=ldx;p[0].y1:=ldy;p[0].y2:=rux;p[n+1].x1:=rux;p[n+1].x2:=rux;p[n+1].y1:=ldy;p[n+1].y2:=ruy;for i:=1 to m dobeginreadln(xx,yy);l:=1;r:=n+1;while l<r-1 dobeginmid:=(l+r) div 2;if ok(mid) then r:=mid else l:=mid;end;while (l>1) and ok(l-1) do dec(l);while not ok(l) do inc(l);inc(res[l-1]);end;for i:=0 to n dowriteln(i,': ',res[i]);writeln;fillchar(res,sizeof(res),0);read(n);end; end.
POJ 2398 TOY Storage
本題是上題的姊妹題,區(qū)別在于本題要求統(tǒng)計(jì)的是包含X個(gè)玩具的部分有多少、
P.S 上題的X坐標(biāo)是有序給出的,而本題要先排序、
Code:
varres,app:array [0..1002] of longint;p:array [0..1002] of record x1,x2,y1,y2:double;end;xx,yy,ldx,ldy,rux,ruy:double;n,m,i,l,r,mid:longint; function crossp(x1,y1,x2,y2:double):double;begincrossp:=x1*y2-x2*y1;end; function ok(now:longint):boolean;beginif crossp(p[now].x1-xx,p[now].y1-yy,p[now].x2-xx,p[now].y2-yy)>0 then exit(true) else exit(false);end; procedure sort(l,r:longint);vari,j:longint;cx:double;begini:=l;j:=r;cx:=p[random(r-l+1)+l].x1;repeatwhile p[i].x1<cx do inc(i);while cx<p[j].x1 do dec(j);if i<=j thenbeginp[1002]:=p[i];p[i]:=p[j];p[j]:=p[1002];inc(i);dec(j);end;until i>j;if i<r then sort(i,r);if j>l then sort(l,j);end; beginread(n);while n<>0 dobeginreadln(m,ldx,ruy,rux,ldy);for i:=1 to n dobeginreadln(p[i].x2,p[i].x1);p[i].y1:=ldy;p[i].y2:=ruy;end;sort(1,n);p[n+1].x1:=rux;p[n+1].x2:=rux;p[n+1].y1:=ldy;p[n+1].y2:=ruy;for i:=1 to m dobeginreadln(xx,yy);l:=1;r:=n+1;while l<r-1 dobeginmid:=(l+r) div 2;if ok(mid) then r:=mid else l:=mid;end;while (l>1) and ok(l-1) do dec(l);while not ok(l) do inc(l);inc(res[l-1]);end;for i:=0 to n doinc(app[res[i]]);writeln('Box');for i:=1 to n doif app[i]<>0 thenwriteln(i,': ',app[i]);fillchar(app,sizeof(app),0);fillchar(res,sizeof(res),0);read(n);end; end.
POJ 3304 Segments
本題要求判斷是否存在一條直線通過給出的所有直線、
不難想到如果該直線不過任何一條直線的端點(diǎn),一定可以讓它旋轉(zhuǎn)一定角度與某一個(gè)端點(diǎn)相碰;
而如果該直線只與某一個(gè)相碰,必然可以旋轉(zhuǎn)另一端點(diǎn)與另一個(gè)端點(diǎn)相碰、
于是,我們N^2枚舉出直線,然后判斷是否與每一條線段相交即可、
Code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath>#define EPS 1e-8 #define sqr(x) ((x)*(x))using namespace std;double x[501],y[501]; int n,vv;double dis(double aa,double bb,double cc,double dd){return sqrt((aa-bb)*(aa-bb)+(cc-dd)*(cc-dd)); }double crossp(double aa,double bb,double cc,double dd){return aa*dd-bb*cc; }int judge(double x1,double y1,double x2,double y2){if (dis(x1,x2,y1,y2)<EPS) return 0;for (int j=1;j<=n*2;j++)if (j&1)if (crossp(x[j]-x1,y[j]-y1,x2-x1,y2-y1)*crossp(x2-x1,y2-y1,x[j+1]-x1,y[j+1]-y1)<0) return 0;return 1; }int main(){scanf("%d",&vv);while (vv--){scanf("%d",&n);for (int i=1;i<=n*2;i++)scanf("%lf%lf",&x[i],&y[i]);if (n==1){printf("Yes!\n");continue;}int succ=0;for (int i=1;i<=n*2 && !succ;i++)for (int j=i+1;j<=n*2 && !succ;j++)if (judge(x[i],y[i],x[j],y[j])) succ++;if (succ) printf("Yes!\n"); else printf("No!\n");} }
POJ 1269 Pick-up Sticks
本題來自于一個(gè)經(jīng)典的……游戲?
題意主要就是每次放一條線段上去、如果后放的與先放的有交點(diǎn)就覆蓋掉先放的、
于是我們可以維護(hù)一個(gè)隨便什么鏈表、然后維護(hù)當(dāng)前在頂上的線段編號(hào)、因?yàn)轭}目保證TOP線段不超過1000條、所以是可以無壓力通過的、、
Code:
#include <cstring> #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector>#define EPS 1e-9 #define sqr(x) ((x)*(x)) #define INF 1e9 #define pb push_backusing namespace std;struct segment{double sx,sy,ex,ey; }ss[100010];int n,top[1010],tt;double crossp(double x1,double y1,double x2,double y2){return x1*y2-x2*y1; }int cross(segment a,segment b){if (max(a.sx,a.ex)<min(b.sx,b.ex) || max(a.sy,a.ey)<min(b.sy,b.ey) \|| min(a.sx,a.ex)>max(b.sx,b.ex) || min(a.sy,a.ey)>max(b.sy,b.ey)) return 0;if (crossp(a.sx-b.sx,a.sy-b.sy,b.ex-b.sx,b.ey-b.sy)*crossp(b.ex-b.sx,b.ey-b.sy,a.ex-b.sx,a.ey-b.sy)>0 && \crossp(b.sx-a.sx,b.sy-a.sy,a.ex-a.sx,a.ey-a.sy)*crossp(a.ex-a.sx,a.ey-a.sy,b.ex-a.sx,b.ey-a.sy)>0) return 1;else return 0; }int main(){scanf("%d",&n);while (n){for (int i=1;i<=n;i++)scanf("%lf%lf%lf%lf",&ss[i].sx,&ss[i].sy,&ss[i].ex,&ss[i].ey);top[tt=1]=1;for (int i=2;i<=n;i++){for (int j=1;j<=tt;j++)if (cross(ss[top[j]],ss[i])) top[j]=-1;int cur=0;for (int j=1;j<=tt;j++)if (top[j]>0) top[++cur]=top[j];tt=cur;top[++tt]=i;}printf("Top sticks:");for (int i=1;i<tt;i++)printf(" %d,",top[i]);printf(" %d.\n",top[tt]);scanf("%d",&n);}return 0; }
POJ 1556 The doors
一看圖就知道是什么題了、
把所有出現(xiàn)的點(diǎn)全部建成一張圖,然后求解最短路就可以了。
Code:
#include <cstring> #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector>#define EPS 1e-9 #define sqr(x) ((x)*(x)) #define INF 1e9 #define pb push_backusing namespace std;struct segment{double sx,sy,ex,ey; }ss[100010];int n,top[1010],tt;double crossp(double x1,double y1,double x2,double y2){return x1*y2-x2*y1; }int cross(segment a,segment b){if (max(a.sx,a.ex)<min(b.sx,b.ex) || max(a.sy,a.ey)<min(b.sy,b.ey) \|| min(a.sx,a.ex)>max(b.sx,b.ex) || min(a.sy,a.ey)>max(b.sy,b.ey)) return 0;if (crossp(a.sx-b.sx,a.sy-b.sy,b.ex-b.sx,b.ey-b.sy)*crossp(b.ex-b.sx,b.ey-b.sy,a.ex-b.sx,a.ey-b.sy)>0 && \crossp(b.sx-a.sx,b.sy-a.sy,a.ex-a.sx,a.ey-a.sy)*crossp(a.ex-a.sx,a.ey-a.sy,b.ex-a.sx,b.ey-a.sy)>0) return 1;else return 0; }int main(){scanf("%d",&n);while (n){for (int i=1;i<=n;i++)scanf("%lf%lf%lf%lf",&ss[i].sx,&ss[i].sy,&ss[i].ex,&ss[i].ey);top[tt=1]=1;for (int i=2;i<=n;i++){for (int j=1;j<=tt;j++)if (cross(ss[top[j]],ss[i])) top[j]=-1;int cur=0;for (int j=1;j<=tt;j++)if (top[j]>0) top[++cur]=top[j];tt=cur;top[++tt]=i;}printf("Top sticks:");for (int i=1;i<tt;i++)printf(" %d,",top[i]);printf(" %d.\n",top[tt]);scanf("%d",&n);}return 0; }
POJ 1066 Treasure Hunt
本題大概是說、在(0,0)(100,100)的矩形內(nèi)有若干線段把矩形分割成若干區(qū)域,然后給出一個(gè)寶藏的所在地(X,Y)求一條從邊界進(jìn)入到達(dá)該點(diǎn)的路徑,使得該路徑通過的線段最少、
本題與3304有一定相似之處,不難發(fā)現(xiàn)只要枚舉所有線段的端點(diǎn)到(X,Y)組成的線段與其它線段的交點(diǎn)數(shù)就可以了、
Code:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring>using namespace std;struct segment{double beginx,beginy,endx,endy; }ss[1010];int n; double nx,ny; segment temp;double crossp(double x1,double y1,double x2,double y2){return x1*y2-x2*y1; }int cross(segment a,segment b){if (min(a.beginx,a.endx)>max(b.beginx,b.endx) ||\min(b.beginx,b.endx)>max(a.beginx,a.endx) ||\min(a.beginy,a.endy)>max(b.beginy,b.endy) ||\min(b.beginy,b.endy)>max(a.beginy,a.endy)) return 0;if (crossp(a.beginx-b.beginx,a.beginy-b.beginy,b.endx-b.beginx,b.endy-b.beginy)\*crossp(b.endx-b.beginx,b.endy-b.beginy,a.endx-b.beginx,a.endy-b.beginy)>0 &&\crossp(b.beginx-a.beginx,b.beginy-a.beginy,a.endx-a.beginx,a.endy-a.beginy)\*crossp(a.endx-a.beginx,a.endy-a.beginy,b.endx-a.beginx,b.endy-a.beginy)>0) return 1;return 0; }int main(){scanf("%d",&n);for (int i=1;i<=n;i++)scanf("%lf%lf%lf%lf",&ss[i].beginx,&ss[i].beginy,&ss[i].endx,&ss[i].endy);scanf("%lf%lf",&nx,&ny);temp.endx=nx;temp.endy=ny;int ans=99999999;for (int i=1;i<=n;i++){int cnt=0;temp.beginx=ss[i].beginx;temp.beginy=ss[i].beginy;for (int j=1;j<=n;j++)if (cross(ss[j],temp)) cnt++;if (cnt<ans) ans=cnt;cnt=0;temp.beginx=ss[i].endx;temp.beginy=ss[i].endy;for (int j=1;j<=n;j++)if (cross(ss[j],temp)) cnt++;if (cnt<ans) ans=cnt;}if (ans==99999999) ans=0;printf("Number of doors = %d\n",ans+1);return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/JS-Shining/archive/2013/01/18/2866182.html
總結(jié)
以上是生活随笔為你收集整理的POJ 计算几何专项训练(1) 【2318】【2398】【3304】【2653】【1556】【1066】...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: myeclipse生成getset注释
- 下一篇: 详细讲解JAVA中的IO流