NOIP 2002 矩形覆盖 解题报告

   暴力搜就可以了,枚举每一个点放进第j个矩形里面,然后保存最小值就可以了,代码如下:

#include <stdio.h>
#include <stdlib.h>
#define oo 0xFFFFFFF
struct dot{
	int x, y;
}dots[50];
struct ret{
	struct dot l, r;
}rec[4];
int n, k;
int ans = oo;

int check_(int i, int j)
{
	if(rec[i].l.x == oo || rec[i].l.y == oo || rec[i].r.x == -oo || rec[i].r.y == -oo){
		return 0;
	}
	if(rec[j].l.x == oo || rec[j].l.y == oo || rec[j].r.x == -oo || rec[j].r.y == -oo){
		return 0;
	}
	if(rec[i].l.x > rec[j].r.x || rec[i].l.y > rec[j].r.y){
		return 0;
	}
	if(rec[j].l.x > rec[i].r.x || rec[j].l.y > rec[i].r.y){
		return 0;
	}
	return 1;
}

int check(void)
{
	int i, j;
	for(i = 0; i < k; i++){
		for(j = i + 1; j < k; j++){
			if(check_(i, j)){
				return 0;
			}
		}
	}
	return 1;
}

int calsqr(void)
{
	int l = 0;
	int i;
	for(i = 0; i < k; i++){
		if(rec[i].l.x != oo){
			l += (rec[i].r.x - rec[i].l.x) * (rec[i].r.y - rec[i].l.y);
		}
	}
	return l;
}

void srch(int now)
{
	int i;
	struct ret tmp;
	if(now == n){
		ans = calsqr();
		return;
	}
	for(i = 0; i < k; i++){
		tmp = rec[i];
		if(rec[i].l.x > dots[now].x){
			rec[i].l.x = dots[now].x;
		}
		if(rec[i].l.y > dots[now].y){
			rec[i].l.y = dots[now].y;
		}
		if(rec[i].r.x < dots[now].x){
			rec[i].r.x = dots[now].x;
		}
		if(rec[i].r.y < dots[now].y){
			rec[i].r.y = dots[now].y;
		}
		if(check() && calsqr() < ans){
			srch(now + 1);
		}
		rec[i] = tmp;
	}
}

int main(int argc, char **argv)
{
	int i;
	scanf("%d%d", &n, &k);
	for(i = 0; i < n; i++){
		scanf("%d%d", &dots[i].x, &dots[i].y);
	}
	for(i = 0; i < k; i++){
		rec[i].l.x = rec[i].l.y = oo;
		rec[i].r.x = rec[i].r.y = -oo;
	}
	srch(0);
	printf("%d\n", ans);
	return 0;
}

  

posted @ 2011-07-29 09:49  zqynux  阅读(955)  评论(0编辑  收藏  举报