Chocolate (Codeforces 31D)
Tags: Implement, dfs and similar

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 got n + 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 W, H 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 Inputs:
2 2 2
1 0 1 2
0 1 1 1

2 2 3
1 0 1 2
0 1 1 1
1 1 2 1

2 4 2
0 1 2 1
0 3 2 3

Sample Outputs:
1 1 2

1 1 1 1

2 2 4

Solution (Python):
(w, h, n) = map(int, input().split())
N = 101
x = [[0] * N for _ in range(N)]
y = [[0] * N for _ in range(N)]
(x[h][0], y[0][w], p) = (2, 2, [])
for (a, b, c, d) in [[*map(int, input().split())] for i in range(n)]:
    (m, n) = sorted([(a, b), (c, d)])
    x[m[1]][m[0]] = 1
    y[m[1]][m[0]] = 1
    if m[0] == n[0]:
        x[n[1]][n[0]] = 2
    if m[1] == n[1]:
        y[n[1]][n[0]] = 2

def v(a, b):
    (s, t) = (1, 1)
    while y[b][a + s] == 0:
        s += 1
    if y[b][a + s] == 1:
        y[b][a + s] = x[b][a + s] = 2
        v(a + s, b)
    while x[b + t][a] == 0:
        t += 1
    if x[b + t][a] == 1:
        x[b + t][a] = y[b + t][a] = 2
        v(a, b + t)
    p.append(s * t)
v(0, 0)
print(*sorted(p))

Solution (Python):
from sys import stdin
input = stdin.readline
row = [[0] * 110 for i in range(110)]
col = [[0] * 110 for i in range(110)]
vis = [[False] * 110 for i in range(110)]
ans = []

def get():
    return map(int, input().split())
(W, H, N) = get()
for _ in range(N):
    (x1, y1, x2, y2) = get()
    if x1 == x2:
        for i in range(y1 + 1, y2 + 1):
            row[x1][i] = 1
    elif y1 == y2:
        for i in range(x1 + 1, x2 + 1):
            col[i][y1] = 1
for i in range(1, W + 1):
    for j in range(1, H + 1):
        if not vis[i][j]:
            (t1, t2, t3, t4) = (i, j, 1, 1)
            while col[t1][t2] == 0 and t2 < H:
                t2 += 1
                t4 += 1
            while row[t1][t2] == 0 and t1 < W:
                t1 += 1
                t3 += 1
            for u in range(i, t1 + 1):
                for v in range(j, t2 + 1):
                    vis[u][v] = True
            ans.append(t3 * t4)
ans.sort()
for i in ans:
    print(i, end=' ')

Solution (Python):
(w, h, n) = map(int, input().split())
N = 101
x = [[0] * N for _ in range(N)]
y = [[0] * N for _ in range(N)]
(x[h][0], y[0][w], p) = (2, 2, [])
for (a, b, c, d) in [[*map(int, input().split())] for i in range(n)]:
    (m, n) = sorted([(a, b), (c, d)])
    x[m[1]][m[0]] = 1
    y[m[1]][m[0]] = 1
    if m[0] == n[0]:
        x[n[1]][n[0]] = 2
    if m[1] == n[1]:
        y[n[1]][n[0]] = 2

def v(a, b):
    (s, t) = (1, 1)
    while y[b][a + s] == 0:
        s += 1
    if y[b][a + s] == 1:
        y[b][a + s] = x[b][a + s] = 2
        v(a + s, b)
    while x[b + t][a] == 0:
        t += 1
    if x[b + t][a] == 1:
        x[b + t][a] = y[b + t][a] = 2
        v(a, b + t)
    p.append(s * t)
v(0, 0)
print(*sorted(p))