[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
Sample Input
Sample Output
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
#include<iostream>
#include<string.h>
using namespace std;
class Rect{
public:
int x1,y1,x2,y2;
void set(int a,int b,int c,int d){
x1=a,y1=b,x2=c,y2=d;
}
};//线段类
bool ok(Rect &a,Rect &b){
return b.y1<=a.y1 && a.y1<=b.y2 && a.x1<=b.x1 && b.x1<=a.x2;
}//判断线段相交
int M;
int s;
Rect rectH[],rectS[];//水平和竖直线段集
int main(){
cin>>M;
while(M--){
cin>>s;
int H=,S=;
for(int i=;i<s;i++){
int x,y,x1,y1;
cin>>x>>y>>x1>>y1;
if(x==x1){
if(y>y1)rectS[S++].set(x1,y1,x,y);
else rectS[S++].set(x,y,x1,y1);
}else{
if(x>x1)rectH[H++].set(x1,y1,x,y);
else rectH[H++].set(x,y,x1,y1);
}//要注意从上到下,从左到右
} int tot=;
for(int i=;i<H-;i++){
for(int j=i+;j<H;j++){//枚举2条横的,统计满足相交的竖着的线段的条数count
int count=;
for(int k=;k<S;k++){
if(ok(rectH[i],rectS[k]) && ok(rectH[j],rectS[k]))
count++;
}
tot+=(count-)*count/;//计算此情况能组成多少
}
}
cout<<tot<<'\n';
}return ;
}
[ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)的更多相关文章
- Counting Rectangles
Counting Rectangles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1043 Accepted: 546 De ...
- Project Euler 85 :Counting rectangles 数长方形
Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 co ...
- UVA - 10574 Counting Rectangles
Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...
- UVA 10574 - Counting Rectangles(枚举+计数)
10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...
- Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和
D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...
- Codeforces 372 B. Counting Rectangles is Fun
$ >Codeforces \space 372 B. Counting Rectangles is Fun<$ 题目大意 : 给出一个 \(n \times m\) 的 \(01\) ...
- Codeforces 372B Counting Rectangles is Fun:dp套dp
题目链接:http://codeforces.com/problemset/problem/372/B 题意: 给你一个n*m的01矩阵(1 <= n,m <= 40). 然后有t组询问( ...
- [ACM_模拟][ACM_暴力] Lazier Salesgirl [暴力 懒销售睡觉]
Description Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
随机推荐
- python面向对象个人总结
基础概念:面向对象其实就是类与对象的使用. 类是模板,对象是实例.模板创建实例,实例去类里面去执行类的方法.类的例子: class Foo: def Bar(self): ...
- 使用JdbcTemplate简化JDBC操作 实现数据库操作
使用Spring JDBC框架方遍简单的完成JDBC操作,满足性能的需求且灵活性高. Spring JDBC框架由4个部分组成,即core.datasource.object.support. org ...
- gradle教程 [原创](eclipse/ADT下 非插件 非Android Studio/AS)纯手打 第一篇:安装配置gradle
一个bug 一个脚印的叫你们用gradle. 1介于网络上的很多资料都是老的 不适用与现在的新版本gradle 尤其是有些gradle方法改名了老的用不了 2介于网上都是粘贴复制并且零碎我很蛋疼啊,走 ...
- 调用xml文件的bean
AcTest.class package com.zyz.db; import com.zyz.dao.PersonDao; import org.springframework.context.Ap ...
- Spring MVC开发环境的搭建和实例
一.安装jdk 二.安装tomcat 三.安装maven 新增环境变量MAVEN-HOME,并在path变量中添加bin路径 四.安装IntelliJ IDEA 五.创建maven web项目选择jd ...
- C# 从excel里面复制的1万6千多条记录粘贴到FCKeditor里面,点保存的时候,保存不了,页面没有反应
客户那边添加公告,是直接从excel里面复制的,有1万6千多条记录,excel文件有6M多. 编辑器用的FCKeditor,也能粘贴上,就是点保存的时候,执行了一段时间就没有反映了,保存不了. 想着可 ...
- canvas事件
canvas的事件只能监听在canvas标签上,而不嗯那个想其他dom元素那样,所以针对canvas里面的图形监听事件是比较麻烦的. 通过isPointInPath(x,y); 这个方法用于判断某一 ...
- USB协议(1)
今天开始学习USB协议,精挑细选,我决定使用<圈圈教你玩USB>这本书,并且参考网友翻译的<USB2.0中文协议>. 这两本书都可以在ishare.sina.com.cn 即新 ...
- CardView的简单介绍
CardView是Android5.0中的一个全新控件,本质上而言,CardView是一个增加了圆角和阴影效果的FrameLayout,没错它就是一个FrameLayout,一个布局.CardView ...
- mysql and 和 or 的 优先级和 查询问题
1. select * from trade where id=1 and cid=1 or pid=2 ; 2. select * from trade where cid=1 or (pid=2 ...