题目在这里
http://code.google.com/codejam/contest/dashboard?c=32005#
这题先把问题转化一下, 相当于:
在平面上有一个矩形, 现在知道有一些点于这个矩形的关系。 请根据这些信息, 判断能否确定另一些点与这个矩形的关系
代码如下:
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Point
{
int x, y;
};
enum RectType{ RT_NULL = 0, RT_FINITE = 1 };
class Rect
{
public:
Rect():
mType(RT_NULL),
top(0),
left(0),
right(0),
bottom(0)
{
}
Rect Merge(const Point &a) const
{
Rect r = *this;
if (mType == RT_NULL)
{
r.mType = RT_FINITE;
r.top = r.bottom = a.y;
r.left = r.right = a.x;
return r;
}
if (top < a.y) r.top = a.y;
if (bottom > a.y) r.bottom = a.y;
if (left > a.x) r.left = a.x;
if (right < a.x) r.right = a.x;
return r;
}
bool IsInside(const Point &a) const
{
if (mType == RT_FINITE)
return a.x >= left && a.x <= right
&& a.y >= bottom && a.y <= top;
return false;
}
private:
int top, bottom, left, right;
RectType mType;
};
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int nNbCases, nNbKnown, nNbUnknown;
Point pt;
cin >> nNbCases;
for (int t = 1; t <= nNbCases; ++t)
{
cin >> nNbKnown;
Rect rect;
string sBird;
vector<Point> ptList;
for (int i =0; i<nNbKnown; ++i)
{
cin >> pt.x >> pt.y >> sBird;
if (sBird == "BIRD")
{
rect = rect.Merge(pt);
}
else
{
cin >> sBird;
ptList.push_back(pt);
}
}
cin >> nNbUnknown;
cout << "Case #" << t <<":/n";
for (int i=0; i<nNbUnknown; ++i)
{
cin >> pt.x >> pt.y;
if (rect.IsInside(pt))
{
cout << "BIRD/n";
}
else
{
bool bBird = true;
for (vector<Point>::iterator it = ptList.begin(); it != ptList.end(); ++it)
{
if ( rect.Merge(pt).IsInside(*it) )
{
bBird = false;
break;
}
}
if ( bBird )
cout << "UNKNOWN/n";
else cout << "NOT BIRD/n";
}
}
}
return 0;
}