问题描述:输入一组矩形,这些矩形的边与坐标轴平行,要求合并重叠的矩形并输出合并后的结果。
解法一:暴搜
面试的时候虽然感觉可以先排序再搜索,但没想到具体的做法,所以就直接暴搜了。
import java.util.*;
class Main {
public static void main(String[] args){
List<AABox> input = new LinkedList();
boolean merged = true;
while(merged){
merged = false;
for(int i = 0; i < input.size(); i++){
for(int j = i + 1; j < input.size(); i++){
AABox box1 = input.get(i), box2 = input.get(j);
if(box1.IsOverlap(box2)){
input.set(i, box1.Merge(box2));
input.remove(j);
j--;
merged = true;
}
}
}
}
System.out.println(intput);
}
}
这一做法在最坏情况下,时间复杂度是 O(n^3)。最坏情况如下图所示。
解法二:排序+遍历
在上述极端情况下的时间复杂度为 O(n)。
(本来排序之后想用BFS的,后来发现实际情况比较简单,没必要BFS)
1. 排序
定义结构体Position和对象Box,Box代表矩形,Position用来包装 x 或者 y 值。分别维护2个Position链表,其中1个链表的元素是包装 x 值的Position,另1个则是 y 值。x 值的有序链表如下图所示。
struct Position{
public:
int val;
Position* prev, *next;
Box* box;
Position(int val){
this->val = val;
this->prev = NULL;
this->next = NULL;
this->box = NULL;
}
void setBox(Box * box){
this->box = box;
}
};
class Box{
public:
Position *x0, *x1, *y0, *y1;
bool deleted;
Box(Position* in_x0, Position* in_y0, Position* in_x1, Position* in_y1) :
x0(in_x0), y0(in_y0), x1(in_x1), y1(in_y1)
{
deleted = false;
x0->setBox(this);
y0->setBox(this);
x1->setBox(this);
y1->setBox(this);
}
// bool isOverlap(Box other);
// Box merge(Box other);
};
2.判断是否重叠
举例:在上图的链表中,遍历 box1.x0 和 box1.x1 之间的节点,找到 节点2,并根据节点2的box指针找到所属的box2;比较box1和box2的y值,以判断box1和box2是否重合。
Position *ps = box1->x0->next;
while (ps->box != box1) {
Box *box2 = ps->box;
// Is overlap?
if (box1->y0->val < box2->y0->val && box2->y0->val < box1->y1->val ||
box1->y0->val < box2->y1->val && box2->y1->val < box1->y1->val ||
box2->y0->val < box1->y0->val && box1->y1->val < box2->y1->val) {
//merge
3.合并
更新box1,将box2标记为删除状态,同时更新2个Position有序链表。
举例:上图中 box1.x1 = 3, box2.x1 = 5,所以将box1.x1的指针修改为指向节点5,并删除节点3
//merge
if (box1->x0->val > box2->x0->val) {
deleteNode(box1->x0);
box1->x0 = box2->x0;
box2->x0->box = box1;
} else
deleteNode(box2->x0);
if (box1->x1->val < box2->x1->val) {
deleteNode(box1->x1);
box1->x1 = box2->x1;
box2->x1->box = box1;
} else
deleteNode(box2->x1);
if (box1->y0->val > box2->y0->val) {
deleteNode(box1->y0);
box1->y0 = box2->y0;
box2->y0->box = box1;
} else
deleteNode(box2->y0);
if (box1->y1->val < box2->y1->val) {
deleteNode(box1->y1);
box1->y1 = box2->y1;
box2->y1->box = box1;
} else
deleteNode(box2->y1);
//merge
完整代码
#include <iostream>
#include <list>
#include <queue>
#include <algorithm>
using namespace std;
class Box;
struct Position{
public:
int val;
Position* prev, *next;
Box* box;
Position(int val){
this->val = val;
this->prev = NULL;
this->next = NULL;
this->box = NULL;
}
void setBox(Box * box){
this->box = box;
}
};
class Box{
public:
Position *x0, *x1, *y0, *y1;
bool deleted;
Box(Position* in_x0, Position* in_y0, Position* in_x1, Position* in_y1) :
x0(in_x0), y0(in_y0), x1(in_x1), y1(in_y1)
{
deleted = false;
x0->setBox(this);
y0->setBox(this);
x1->setBox(this);
y1->setBox(this);
}
// bool isOverlap(Box other);
// Box merge(Box other);
};
bool cmp(int* p1, int* p2){
return *p1 < *p2;
}
void insertSort(Position & h, Position & t, Position & n){
Position * i = &t;
while (&h != i && ((*i).val > n.val || &t == i)){
i = (*i).prev;
}
n.next = (*i).next;
n.prev = i;
(*i).next->prev = &n;
(*i).next = &n;
}
void deleteNode(Position * n) {
n->prev->next = n->next;
n->next->prev = n->prev;
delete n;
}
int main() {
list<Box*> in;
Position xHead(0), xTail(0), yHead(0), yTail(0);
xHead.next = &xTail;
xTail.prev = &xHead;
yHead.next = &yTail;
yTail.prev = &yHead;
int n;
cin >> n;
while (n--) {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
Position* px0 = new Position(x0);
Position* py0 = new Position(y0);
Position* px1 = new Position(x1);
Position* py1 = new Position(y1);
insertSort(xHead, xTail,*px0);
insertSort(xHead, xTail,*px1);
insertSort(yHead, yTail,*py0);
insertSort(yHead, yTail,*py1);
Box* box = new Box(px0, py0, px1, py1);
in.push_back(box);
}
/*input, such as
3
0 0 2 2
1 1 4 4
4 4 5 5
*/
list<Box*>::iterator it;
for(it = in.begin(); it != in.end(); it++){
if((*it)->deleted)
continue;
/* //BFS
queue<Box*> q;
q.push(*it);
while (!q.empty()){
Box* box1 = q.front();
q.pop();*/
Box * box1 = *it;
while(!box1->deleted &&
box1->x0->next->box != box1 && box1->y0->next->box != box1) {
Position *ps = box1->x0->next;
while (ps->box != box1) {
Box *box2 = ps->box;
// Is overlap?
if (box1->y0->val < box2->y0->val && box2->y0->val < box1->y1->val ||
box1->y0->val < box2->y1->val && box2->y1->val < box1->y1->val ||
box2->y0->val < box1->y0->val && box1->y1->val < box2->y1->val) {
//merge
if (box1->x0->val > box2->x0->val) {
deleteNode(box1->x0);
box1->x0 = box2->x0;
box2->x0->box = box1;
} else
deleteNode(box2->x0);
if (box1->x1->val < box2->x1->val) {
deleteNode(box1->x1);
box1->x1 = box2->x1;
box2->x1->box = box1;
} else
deleteNode(box2->x1);
if (box1->y0->val > box2->y0->val) {
deleteNode(box1->y0);
box1->y0 = box2->y0;
box2->y0->box = box1;
} else
deleteNode(box2->y0);
if (box1->y1->val < box2->y1->val) {
deleteNode(box1->y1);
box1->y1 = box2->y1;
box2->y1->box = box1;
} else
deleteNode(box2->y1);
//merge
//delete box2
box2->deleted = true;
}
ps = ps->next;
}
}
}
for(it = in.begin(); it != in.end(); it++) {
if((*it)->deleted)
it = in.erase(it);
}
for(it = in.begin(); it != in.end(); it++){
cout << (*it)->x0->val << " " << (*it)->y0->val << " " << (*it)->x1->val << " " << (*it)->y1->val << endl;
}
}
解法三:并查集
有人用并查集解决此问题(链接),但个人感觉没有必要使用并查集。