CF31D 分治

http://codeforces.com/problemset/problem/31/D

D. Chocolate
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bob has a rectangular chocolate bar of the size W × H. He introduced a cartesian coordinate system so that the point (0, 0)corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he gotn + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.

Input

The first line contains 3 integers WH and n (1 ≤ W, H, n ≤ 100) — width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 — coordinates of the endpoints of the i-th break (0 ≤ xi, 1 ≤ xi, 2 ≤ W, 0 ≤ yi, 1 ≤ yi, 2 ≤ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order.

It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.

Output

Output n + 1 numbers — areas of the resulting parts in the increasing order.

Sample test(s)
input
2 2 2
1 0 1 2
0 1 1 1
output
1 1 2 
input
2 2 3
1 0 1 2
0 1 1 1
1 1 2 1
output
1 1 1 1 
input
2 4 2
0 1 2 1
0 3 2 3
output
2 2 4 
/**
CF 31D 分治
题目大意:给你你一个w*h的巧克力,用一些平行坐标轴的线段切开,求出每一块的面积
解题思路:递归分治见代码
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=103;
int cn,x[maxn][maxn][maxn],y[maxn][maxn][maxn],area[maxn*maxn];
int w,h,n,i,x1,y1,x2,y2;
void dfs(int x1,int x2,int y1,int y2)
{
    for(int i=y1+1; i<y2; i++)
    {
        if(y[x1][x2][i])
        {
            dfs(x1,x2,i,y2);
            dfs(x1,x2,y1,i);
            return;
        }
    }
    for(int i=x1+1; i<x2; i++)
    {
        if(x[y1][y2][i])
        {
            dfs(i,x2,y1,y2);
            dfs(x1,i,y1,y2);
            return;
        }
    }
    area[cn++]=(x2-x1)*(y2-y1);
}
int main()
{
    while(~scanf("%d%d%d",&w,&h,&n))
    {
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            if(x1==x2)x[y1][y2][x1]=1;
            if(y1==y2)y[x1][x2][y1]=1;
        }
        cn=0;
        dfs(0,w,0,h);
        sort(area,area+cn);
        for(int i=0; i<cn; i++)
        {
            printf(i==cn-1?"%d\n":"%d ",area[i]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值