暴力枚举-数长方形(hdu5258)

数长方形

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 152    Accepted Submission(s): 87

Problem Description
小度熊喜欢玩木棒。一天他在玩木棒的时候,发现一些木棒会形成长方形。小度熊可能是处女座吧,他只会将木棒横竖摆放,这样会形成很多长方形。现在给你一些横竖摆放的木棒,请你帮小度熊数一数形成了多少个长方形。

为了简化题目,一个木棒的端点不会在另一个木棒上,也就是说,木棒的端点不会在长方形上。
 
Input
第一行一个整数T,表示T组数据,不超过100组。

每组数据中,第一行是n,代表有多少个木棒,n不会超过25。接下来n行,每行4个整数x1,y1,x2,y2,代表木棒的坐标,绝对值不超过1000。

所有的木棒都是横竖摆放的,也就是说x1=x2或者y1=y2,没有长为0的木棒。
 
Output
对于每组测试数据,先输出一行

Case #i:

然后输出一个整数,代表有多少个长方形。
 
Sample Input
2
4
3 0 3 3
4 0 4 3
2 1 5 1
2 2 5 2
4
3 0 3 3
4 0 4 3
2 1 5 1
2 2 -5 2
 
Sample Output
Case #1:
1
Case #2:
0
分析:分别把竖条和横条存在col和row数组中,然后两两枚举竖条col[i]和col[j],然后枚举判断row[k]是否横跨col[i]和col[j],若是的话s++,枚举一边row[k]后,此时的矩形个数sum+=s*(s-1)/2;
程序:
 
  1 #include"stdio.h"
  2 #include"string.h"
  3 #include"stdlib.h"
  4 #include"algorithm"
  5 #include"queue"
  6 #include"math.h"
  7 #include"iostream"
  8 #include"vector"
  9 #define M 100009
 10 #define inf 0x3f3f3f3f
 11 #define eps 1e-9
 12 #define PI acos(-1.0)
 13 #include"map"
 14 #include"vector"
 15 #include"set"
 16 #include"string"
 17 #include"stack"
 18 #define LL __int64
 19 using namespace std;
 20 struct node
 21 {
 22     int x,y;
 23     node(){}
 24     node(int x,int y)
 25     {
 26         this->x=x;
 27         this->y=y;
 28     }
 29 };
 30 struct line
 31 {
 32     node s,e;
 33 }col[M],row[M];
 34 int cmp(line a,line b)
 35 {
 36     return a.s.x<b.s.x;
 37 }
 38 int ok(line a,line b,line c)
 39 {
 40     if(c.s.x<=a.s.x
 41        &&c.e.x>=b.s.x
 42        &&c.s.y<=min(a.s.y,b.s.y)
 43        &&c.s.y>=max(a.e.y,b.e.y))
 44        return 1;
 45     return 0;
 46 }
 47 int main()
 48 {
 49     int T,kk=1;
 50     cin>>T;
 51     while(T--)
 52     {
 53         int n;
 54         scanf("%d",&n);
 55         int x1,x2,y1,y2;
 56         int col_k=0;
 57         int row_k=0;
 58         for(int i=1;i<=n;i++)
 59         {
 60             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
 61             if(x1==x2)
 62             {
 63                 ++col_k;
 64                 col[col_k].s=node(x1,max(y1,y2));
 65                 col[col_k].e=node(x1,min(y1,y2));
 66             }
 67             else if(y1==y2)
 68             {
 69                 ++row_k;
 70                 row[row_k].s=node(min(x1,x2),y1);
 71                 row[row_k].e=node(max(x1,x2),y1);
 72             }
 73         }
 74         sort(col+1,col+col_k+1,cmp);
 75         int sum=0;
 76         for(int i=1;i<=col_k;i++)
 77         {
 78             for(int j=i+1;j<=col_k;j++)
 79             {
 80                 int s=0;
 81                 for(int k=1;k<=row_k;k++)
 82                 {
 83                     if(ok(col[i],col[j],row[k]))
 84                         s++;
 85                 }
 86                 s--;
 87                 if(s>=1)
 88                 {
 89                     s=s*(s+1)/2;
 90                     sum+=s;
 91                 }
 92             }
 93         }
 94         printf("Case #%d:\n",kk++);
 95         printf("%d\n",sum);
 96     }
 97     return 0;
 98 }
 99 /*
100 23
101 6
102 1 0 1 5
103 5 0 5 5
104 0 4 6 4
105 0 2 6 2
106 0 1 6 1
107 3 0 3 3
108 
109 2
110 0 0 0 5
111 6 0 6 6
112 
113 7
114 0 1 100 1
115 0 3 100 3
116 0 95 100 95
117 0 97 100 97
118 3 0 3 10
119 3 90 3 100
120 90 0 90 110
121 
122 10
123 0 1 100 1
124 0 2 100 2
125 0 3 100 3
126 0 4 100 4
127 0 5 100 5
128 10 0 10 110
129 11 0 11 110
130 12 0 12 110
131 13 0 13 110
132 15 0 15 110
133 
134 */
View Code

 

posted @ 2015-08-07 21:27  一样菜  阅读(461)  评论(0编辑  收藏  举报