google codejam 2008 APAC local onsites , Problem A What are Birds?

题目在这里

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;

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值