【几何】-POJ-2318-Toys
来源:互联网 发布:软件开发编程 编辑:程序博客网 时间:2024/06/06 14:09
题目链接:http://poj.org/problem?id=2318
题目描述:见链接,用隔板隔开一个长方形箱子,往里面随便放玩具,给出箱子结构和玩具坐标,统计各个隔板空间中有几个玩具
解题思路:
看数据,o(n^2)暴力即可。这是用叉乘判断位置关系的简单应用。
先把隔板们处理成向量形式存好,隔板端点存好。
对每个玩具从左到右遍历一边隔板,当发现玩具在 i 隔板左侧时,说明玩具在 (i - 1)的隔板空间中
AC代码:
#include <iostream>#include <cstdio>#include <cstring>//#include <cmath>using namespace std;struct Point{ double x,y; Point(double x=0, double y=0):x(x),y(y) {} void show() { cout<<"("<<x<<", "<<y<<")"<<endl; }};typedef Point Vector;Vector operator - (Point A, Point B) { return Vector(A.x-B.x, A.y-B.y); }double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }double fabs(int x){ return x < 0 ? -x : x;}const double eps = 1e-10;int dcmp(double x){ if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;}int n,m,x1,y1,x2,y2;Vector edge[5100];Point p1[5100],p2[5100],toy[5100];int ans[5100];int main(){ freopen("input.txt","r",stdin); int i,j,U,L,tx,ty; while(cin>>n,n!=0) { memset(ans,0,sizeof(ans)); cin>>m>>x1>>y1>>x2>>y2; memset(p1,0,sizeof(p1)); memset(p2,0,sizeof(p2)); memset(toy,0,sizeof(toy)); for(i=1;i<=n;i++) { cin>>U>>L; p1[i]=Point(U,y1); p2[i]=Point(L,y2); } p1[0]=Point(x1,y1); p1[n+1]=Point(x2,y1); p2[0]=Point(x1,y2); p2[n+1]=Point(x2,y2); for(i=0;i<m;i++) { cin>>tx>>ty; toy[i]=Point(tx,ty); } memset(edge,0,sizeof(edge)); for(i=0;i<=n+1;i++) { edge[i]=p1[i]-p2[i]; //edge[i].show(); } for(i=0;i<m;i++) { if(toy[i].x==x1) { ans[0]++;continue; } if(toy[i].x==x2) { ans[n]++;continue; } for(j=1;j<=n+1;j++) { Vector temp=toy[i]-p2[j]; int k=dcmp(Cross(edge[j],temp)); if(k>0) { ans[j-1]++; break; } } } for(i=0;i<=n;i++) printf("%d: %d\n",i,ans[i]); cout<<endl; } return 0;}AC截图:
0 0
- POJ 2318 TOYS(几何)
- 【几何】-POJ-2318-Toys
- poj-2318-TOYS-计算几何
- POJ 2318 TOYS(计算几何)
- poj 2318 TOYS (计算几何)
- 计算几何--POJ--2318--TOYS
- POJ 2318 TOYS <计算几何>
- poj 2318 TOYS(计算几何)
- poj 2318 TOYS(计算几何:求叉积)
- Poj 2318 toys (计算几何,叉积)
- POJ 2318 TOYS(计算几何)
- POJ 2318 - TOYS【计算几何 - 叉积】
- POJ 2318 TOYS 几何题 基础
- POJ 2318 TOYS 计算几何 线段相交
- POJ 2318 TOYS(计算几何入门)
- poj 2318 TOYS(计算几何)
- 【POJ 2318】TOYS(计算几何)
- POJ 2318 TOYS(计算几何)
- Request.Cookies用法分析
- 【Android】任务和返回栈(tasks and back stack)
- cdh5 源码编译,导入eclipse
- 关于 NSURL 的解析和编码
- 特殊SQL
- 【几何】-POJ-2318-Toys
- InnoDB: Error: io_setup() failed with EAGAIN after 5 attempts
- 一个简单的通讯录
- win7 + IIS + MYSQL +PHP 配置
- svn 命令行操作
- js判断是否位数字
- 霍夫变换概述霍夫变换(Hough Transform)是图像处理中的一种特征提取技术
- 使用struts2 建立Helloworld
- 斐波那契数列