TOYS POJ - 2318
TOYS
题目链接:https://vjudge.net/problem/POJ-2318
题目:




题意:给出左上角和右下角矩形坐标和矩形内隔板的上下位置还有toys的坐标,求出这些隔板所隔的所有区域的toys个数。
思路:令玩具坐标为点a,隔板上下坐标分别为点c,b;判断b->c方向是否是a->b方向的逆时针方向,利用向量来求,即判断玩具是否在隔板的左边,如果是左边的话,对应的区域玩具个数++即可,
每个玩具坐标按此依次从左到右的隔板坐标比较,矩形的右宽应当做最后一个挡板。一开始忘记多组。。。然后忘记在边界上(判断的时候忽略了等于0的情况),最后忘记一个致命问题,,想当然以为y2=0,以为是在坐标轴上。。。。所以求向量就错,关键样例还过了。。看了一个多小时bug。。
ac代码如下:
#include<cmath>
#include<stdio.h>
using namespace std;
const double pi = acos(-1.0);
const double inf = 1e100;
const double eps = 1e-6;
const int maxn=1e5+10;
struct Point{
int x, y;
Point(double x = 0, double y = 0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B){
return Vector(A.x+B.x, A.y+B.y);
}
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;
}
bool ToLeftTest(Point a, Point b, Point c){
return Cross(b - a, c - b) >= 0;
}
int main()
{
// freopen("text","r",stdin);
int n;
while(~scanf("%d",&n)&&n){
int m;
double x1,y1,x2,y2;
Point tt[maxn];
scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);
// cout<<n<<" "<<m<<' '<<x1<<" "<<y1<<' '<<endl;
double b, c;
int pos = 0;
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &c, &b);
tt[pos].x = c, tt[pos].y = y1;
pos++;
tt[pos].x = b, tt[pos].y = y2;
pos++;
}
tt[pos].x = x2, tt[pos].y = y1;
pos++;
tt[pos].x = x2, tt[pos].y = y2;
int t1, t2, box[maxn] = {0};
for (int i = 0; i < m; ++i) {
scanf("%d%d", &t1, &t2);
Point a;
a.x = t1, a.y = t2;
int num = 0;
for (int j = 0; j <= n; ++j) {
if (ToLeftTest(a, tt[j * 2 + 1], tt[j * 2])) {
//cout<<"i="<<i<<","<<ToLeftTest(a,tt[j+1],tt[j])<<endl;
//cout<<"j="<<j<<endl;
num = j;
break;
}
}
box[num]++;
}
for (int i = 0; i <= n; i++) {
//printf("(%d,%d)(%d,%d)\n",tt[i].x,tt[i].y,tt[i+1].x,tt[i+1].y);
printf("%d: %d\n", i, box[i]);
}
printf("\n");
}
return 0;
}

浙公网安备 33010602011771号