[ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)

简介:


Description

We are given a figure consisting of only horizontal and vertical line segments. Our goal is to count the number of all different rectangles formed by these segments. As an example, the number of rectangles in the Figures 1 and 2 are 5 and 0 respectively.

There are many intersection points in the figure. An intersection point is a point shared by at least two segments. The input line segments are such that each intersection point comes from the intersection of exactly one horizontal segment and one vertical segment.

Input

The first line of the file contains a single number M, which is the number of test cases in the file (1 <= M <= 10), and the rest of the file consists of the data of the test cases. Each test case begins with a line containing s (1 <= s <= 100), the number of line segments in the figure. It follows by s lines, each containing x and y coordinates of two end points of a segment respectively. The coordinates are integers in the range of 0 to 1000.

Output

The output for each test case is the number of all different rectangles in the figure described by the test case. The output for each test case must be written on a separate line.

Sample Input

2
6
0 0 0 20
0 10 25 10
20 10 20 20
0 0 10 0
10 0 10 20
0 20 20 20
3
5 0 5 20
15 5 15 25
0 10 25 10

Sample Output

5
0

 

The above input file contains two test cases corresponding to Figures 1 and 2 respectively.

 

题目大意:给一些水平或竖直的线段,求能组成的矩形的个数。

解题思路:因为题目给的只有垂直和水平的线段,且总线段不超过100.所以我们可以暴力。

  1、任选两根水平的线段,若无水平线段可选,结束。否则,转2

  2、从所有的垂直线段里,找到和这两根水平线段相交的线段,假设有tmp条。转3

  3、对于1步选的两条水平线段,因为有tmp跟垂直线段与其相交,根据推算,可以得知,其能组成的矩形就是(tmp - 1)*tmp / 2 个,将其加进总和里即可。转1

 

复制代码
 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4 class Rect{
 5 public:
 6     int x1,y1,x2,y2;
 7     void set(int a,int b,int c,int d){
 8         x1=a,y1=b,x2=c,y2=d;
 9     }
10 };//线段类
11 bool ok(Rect &a,Rect &b){
12     return b.y1<=a.y1 && a.y1<=b.y2 && a.x1<=b.x1 && b.x1<=a.x2;
13 }//判断线段相交
14 int M;
15 int s;
16 Rect rectH[1004],rectS[1004];//水平和竖直线段集
17 int main(){
18     cin>>M;
19     while(M--){
20         cin>>s;
21         int H=0,S=0;
22         for(int i=0;i<s;i++){
23             int x,y,x1,y1;
24             cin>>x>>y>>x1>>y1;
25             if(x==x1){
26                 if(y>y1)rectS[S++].set(x1,y1,x,y);
27                 else rectS[S++].set(x,y,x1,y1);
28             }else{
29                 if(x>x1)rectH[H++].set(x1,y1,x,y);
30                 else rectH[H++].set(x,y,x1,y1);
31             }//要注意从上到下,从左到右
32         }
33 
34         int tot=0;
35         for(int i=0;i<H-1;i++){
36             for(int j=i+1;j<H;j++){//枚举2条横的,统计满足相交的竖着的线段的条数count
37                 int count=0;
38                 for(int k=0;k<S;k++){
39                     if(ok(rectH[i],rectS[k]) && ok(rectH[j],rectS[k]))
40                         count++;
41                 }
42                 tot+=(count-1)*count/2;//计算此情况能组成多少
43             }
44         }
45         cout<<tot<<'\n';
46     }return 0;
47 }
复制代码

 



本文转自beautifulzzzz博客园博客,原文链接:http://www.cnblogs.com/zjutlitao/p/3572395.html,如需转载请自行联系原作者

相关文章
|
6月前
|
算法
LeetCode算法题---最长回文子串、N 字形变换(四)
LeetCode算法题---最长回文子串、N 字形变换(四)
41 0
|
6月前
【每日一题Day165】LC1039多边形三角剖分的最低得分 | 区间dp
【每日一题Day165】LC1039多边形三角剖分的最低得分 | 区间dp
57 0
每日三题-最大正方形 、完全平方数 、目标和
每日三题-最大正方形 、完全平方数 、目标和
87 2
每日三题-最大正方形 、完全平方数 、目标和
洛谷P3194 [HNOI2008]水平可见直线(计算几何+单调栈)
洛谷P3194 [HNOI2008]水平可见直线(计算几何+单调栈)
108 0
洛谷P3194 [HNOI2008]水平可见直线(计算几何+单调栈)
|
存储
【每日一题Day106】LC1129 颜色交替的最短路径 | BFS
返回长度为 n 的数组 answer,其中 answer[X] 是从节点 0 到节点 X 的红色边和蓝色边交替出现的最短路径的长度。如果不存在这样的路径,那么 answer[x] = -1。
143 0
PTA——7-31 三角形判断
PTA——7-31 三角形判断
343 0
PTA——7-31 三角形判断
|
存储 算法
算法题每日一练---第41天:三角形最小路径和
给定一个三角形 triangle ,找出自顶向下的最小路径和。
97 0
算法题每日一练---第41天:三角形最小路径和
【PTA】7-3 判断上三角矩阵 (15分)
【PTA】7-3 判断上三角矩阵 (15分)
2173 0