问题描述】
w*h的格子画了n条或垂直或水平宽度为1的直线,求出这些格子被划分成了多少个4连块(上、下、左、右连通)。
【输入格式】
第一行包含两个整数:w和h,表示矩阵的列数和行数(行列编号都从1开始)。
第二行包含一个整数n,表示有n条直线。
接下来的n行,每行包含四个整数:x1,y1,x2,y2,表示一条直线的列号和行号。
【输出格式】
一个整数,表示区域数量。
【输入样例】
10 10
5
1 4 6 4
1 8 10 8
4 1 4 10
9 1 9 5
10 6 10 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
【输出样例】
6
- 1
【数据范围】
1<=w,h<=1000000 , 1<=n<=500
【来源】
挑战程序设计竞赛(第2版)164页
//#include <bits/stdc++.h>
#include <map>
#include <set>
#include<iostream>
#include <stdio.h>
#include<cstring>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#define LL long long int
using namespace std;
const int MM=1000001;
const int MMM=6006; //压缩以后不超过6000
int N,W,H; //压缩以前的宽度与高度
int X1[MM],Y1[MM], X2[MM],Y2[MM];
int g[MMM][MMM];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0}; //四个方向方向
int neww,newh;//压缩以后的宽度和高度
int compress(int *x1, int *x2 ,int w){
vector<int> xs;
//N个三合板
for(int i=1;i<=N; i++){
for(int d=-1;d<=1;d++){ //需要存前、后、自己、一共三个点
if( x1[i]+d>0 && x1[i]+d<=w) xs.push_back(x1[i]+d);
if( x2[i]+d>0 && x2[i]+d<=w) xs.push_back(x2[i]+d);
}
}
for(int i=0;i<xs.size();i++){
cout << xs[i] << " ";
}
cout << endl;
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()) , xs.end());
for(int i=1;i<=N;i++){
x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin() ;
x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin() ;
}
for(int i=0;i<xs.size();i++){
cout << xs[i] << " ";
}
cout << endl;
return neww = xs.size();
}
int bfs(){
int ans=0;
for(int i=1;i<=neww;i++) {
for(int j=1;j<=newh;j++){
if( g[i][j] ==1 ) continue;
ans++;
queue< pair<int ,int> > q;
q.push( make_pair(i,j));
while( !q.empty()){
int hx = q.front().first; int hy= q.front().second;
q.pop();
for(int i=0;i<4;i++){
int xx = hx + dx[i];int yy = hy + dy[i];
if( xx >=0 && xx<neww && yy>=0 && yy<newh && g[yy][xx]==0){
q.push( make_pair(xx, yy) );
g[yy][xx]=1;
}
}
}
}
}
return ans;
}
int main()
{
cin >> W >> H;
cin >> N;
for(int i=1;i<=N;i++){
cin >> X1[i];
cin >> Y1[i];
cin >> X2[i];
cin >> Y2[i];
}
neww = compress(X1, X2, W);
newh = compress(Y1, Y2, H);
memset( g, 0 , sizeof(g) );
for(int i=1;i<=N;i++){
for(int y=Y1[i]; y<=Y2[i];y++){
for(int x=X1[i] ; x<=X2[i]; x++){
g[y][x] = 1;
}
}
}
for(int y=1; y<=newh;y++){
for(int x=1 ; x<=neww; x++){
cout << g[y][x]<< ' ';
}
cout << endl;
}
//cout << bfs();
return 0;
}
书中的的数据离散化之后不是这个小图。
aoj 0531 Paint Color 题目链接: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0531
涂色:(日文题目,自己翻译成了中文)为了宣传信息竞赛,要在长方形的三合板上喷油漆来制作招牌。三合板上不需要涂色的部分预先贴好了护板。被护板隔开的区域要涂上不同的颜色,比如上图就应该涂上5种颜色。
请编写一个程序计算涂色数量,输入数据中,保证看板不会被护板全部遮住,并且护板的边一定是水平或垂直的。
输入:
第一个数是宽w(1 ≤ w ≤ 1000000),第二个数是高h(1 ≤ h ≤ 1000000)。
第二行是护板的数量n(1 ≤ n ≤ 1000),接着n行是每个护板的左下角坐标 (x1 , y1 )和右上角坐标 (x2 , y2 ),用空格隔开: x1 , y1 , x2 , y2 (0 ≤ x1< x2 ≤ w, 0 ≤ y1 < y2 ≤ h 都是整数)
招牌的坐标系如下,左下角是 (0, 0) ,右上角是(w, h) , 测试集中的30%都满足w ≤ 100, h ≤ 100, n ≤ 100。
输出:
一个整数,代表涂色数量。
输入输出的例子
input.txt
15 6 10 1 4 5 6 2 1 4 5 1 0 5 1 6 1 7 5 7 5 9 6 7 0 9 2 9 1 10 5 11 0 14 1 12 1 13 5 11 5 14 6 0 0output.txt
5
网上资料: http://www.hankcs.com/program/algorithm/aoj-0531-paint-color.html