这两道道题的大意是,给定一个矩形,然后给你n条互不相交的从矩形上边的边连到下面的边的直线,将这个矩形分成n+1部分,现在有m个点,问你每个区域各有多少个点,区别仅在于输出方式不同。
于是这是道水到爆的计算几何基础题,只要把这些边排个序,然后用叉乘判断是否在这个区域里就可以了。
附上我的AC代码:
POJ 2318:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
class Point
{
public:
double x, y;
public:
Point(double x = 0, double y = 0) : x(x), y(y) { }
};
typedef Point Vector;
Vector operator + (const Vector &a, const Vector &b)
{
return Vector(a.x+b.x, a.y+b.y);
}
Vector operator - (const Point &a, const Point &b)
{
return Vector(a.x-b.x, a.y-b.y);
}
Vector operator * (const Vector &a, const double &b)
{
return Vector(a.x*b, a.y*b);
}
Vector operator * (const double &a, const Vector &b)
{
return b*a;
}
Vector operator / (const Vector &a, const double &b)
{
return Vector(a.x/b, a.y/b);
}
Vector operator / (const double &a, const Vector &b)
{
return b/a;
}
const double eps = 1e-10;
int dcmp(const double &x)
{
if (fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}
bool operator == (const Point &a, const Point &b)
{
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
bool operator < (const Point &a, const Point &b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
double Dot(const Vector &a, const Vector &b)
{
return a.x*b.x + a.y*b.y;
}
double Cross(const Vector &a, const Vector &b)
{
return a.x*b.y - a.y*b.x;
}
int n, m;
double xx1, yy1, xx2, yy2;
Point p[5500];
Vector v[5500];
int so[5500];
int ans[5500];
bool lcmp(int i, int j)
{
return p[i] < p[j];
}
void check()
{
double x, y;
scanf("%lf %lf", &x, &y);
Point tmp = Point(x, y);
for (int i = 0; i <= n; i++) {
if (Cross(tmp-p[so[i]], v[so[i]]) * Cross(tmp-p[so[i+1]], v[so[i+1]]) < 0) {
ans[i]++;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("toys.in", "r", stdin);
#endif
double x, y;
while (true) {
bool flag = false;
scanf("%d %d %lf %lf %lf %lf", &n, &m, &xx1, &yy1, &xx2, &yy2);
if (xx1 > xx2)
swap(xx1, xx2);
if (yy1 > yy2)
swap(yy1, yy2), flag = true;
if (n == 0)
break;
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= n; i++) {
scanf("%lf %lf", &x, &y);
if (flag) {
p[i] = Point(y, yy1);
v[i] = Point(x, yy2) - p[i];
} else {
p[i] = Point(x, yy1);
v[i] = Point(y, yy2) - p[i];
}
}
p[0].x = xx1, p[0].y = yy1;
v[0].x = 0, v[0].y = yy2-yy1;
p[n+1].x = xx2, p[n+1].y = yy1;
v[n+1].x = 0, v[n+1].y = yy2-yy1;
for (int i = 0; i <= n+1; i++)
so[i] = i;
sort(so+1, so+n+1, lcmp);
for (int i = 0; i < m; i++) {
check();
}
for (int i = 0; i <= n; i++)
printf("%d: %d\n", i, ans[i]);
printf("\n");
}
return 0;
}
POJ 2398:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
class Point
{
public:
double x, y;
public:
Point(double x = 0, double y = 0) : x(x), y(y) { }
};
typedef Point Vector;
Vector operator + (const Vector &a, const Vector &b)
{
return Vector(a.x+b.x, a.y+b.y);
}
Vector operator - (const Point &a, const Point &b)
{
return Vector(a.x-b.x, a.y-b.y);
}
Vector operator * (const Vector &a, const double &b)
{
return Vector(a.x*b, a.y*b);
}
Vector operator * (const double &a, const Vector &b)
{
return b*a;
}
Vector operator / (const Vector &a, const double &b)
{
return Vector(a.x/b, a.y/b);
}
Vector operator / (const double &a, const Vector &b)
{
return b/a;
}
const double eps = 1e-10;
int dcmp(const double &x)
{
if (fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}
bool operator == (const Point &a, const Point &b)
{
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
bool operator < (const Point &a, const Point &b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
double Dot(const Vector &a, const Vector &b)
{
return a.x*b.x + a.y*b.y;
}
double Cross(const Vector &a, const Vector &b)
{
return a.x*b.y - a.y*b.x;
}
int n, m;
double xx1, yy1, xx2, yy2;
Point p[5500];
Vector v[5500];
int so[5500];
int ans[5500];
int ans2[5500];
bool lcmp(int i, int j)
{
return p[i] < p[j];
}
void check()
{
double x, y;
scanf("%lf %lf", &x, &y);
Point tmp = Point(x, y);
for (int i = 0; i <= n; i++) {
if (Cross(tmp-p[so[i]], v[so[i]]) * Cross(tmp-p[so[i+1]], v[so[i+1]]) < 0) {
ans[i]++;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("toys.in", "r", stdin);
#endif
double x, y;
while (true) {
bool flag = false;
scanf("%d %d %lf %lf %lf %lf", &n, &m, &xx1, &yy1, &xx2, &yy2);
if (xx1 > xx2)
swap(xx1, xx2);
if (yy1 > yy2)
swap(yy1, yy2), flag = true;
if (n == 0)
break;
memset(ans, 0, sizeof(ans));
memset(ans2, 0, sizeof(ans2));
for (int i = 1; i <= n; i++) {
scanf("%lf %lf", &x, &y);
if (flag) {
p[i] = Point(y, yy1);
v[i] = Point(x, yy2) - p[i];
} else {
p[i] = Point(x, yy1);
v[i] = Point(y, yy2) - p[i];
}
}
p[0].x = xx1, p[0].y = yy1;
v[0].x = 0, v[0].y = yy2-yy1;
p[n+1].x = xx2, p[n+1].y = yy1;
v[n+1].x = 0, v[n+1].y = yy2-yy1;
for (int i = 0; i <= n+1; i++)
so[i] = i;
sort(so+1, so+n+1, lcmp);
for (int i = 0; i < m; i++) {
check();
}
for (int i = 0; i <= n; i++) {
ans2[ans[i]]++;
}
printf("Box\n");
for (int i = 1; i <= m; i++)
if (ans2[i])
printf("%d: %d\n", i, ans2[i]);
}
return 0;
}