Problem:


Square Carpets

Mr. Frugal bought a new house.
He feels deeply in love with his new house
because it has a comfortable living room
in which he can put himself completely at ease.
He thinks his new house is a really good buy.

But, to his disappointment, 
the floor of its living room has some scratches on it.

The floor has a rectangle shape, covered with square panels.
He wants to replace all the scratched panels with flawless panels, 
but he cannot afford to do so.
Then, he decides to cover all the scratched panels 
with carpets.

The features of the carpets he can use are as follows.

 Carpets are square-shaped.

 Carpets may overlap each other.

 Carpets cannot be folded.

 Different sizes of carpets are available.
Lengths of sides of carpets are multiples of that of the panels.

The carpets must cover all the scratched panels,
but must not cover any of the flawless ones.

For example, 
if the scratched panels are as shown in Figure 1, 
at least 6 carpets are needed.

  

  Figure 1: Example Covering

As carpets cost the same irrespective of their sizes, 
Mr. Frugal would like to use as few number of carpets as possible.

Your job is to write a program which tells the minimum number 
of the carpets to cover all the scratched panels.

Input

The input consists of multiple data sets.

As in the following, the end of the input is indicated by a line
containing two zeros.

DataSet1

DataSet2

...

DataSetn

0 0

Each data set (DataSeti) represents the
state of a floor. The format of a data set is as follows.

W H

P11 P12 P13 ... P1W

P21 P22 P23 ... P2W

...

PH1 PH2
PH3 ... PHW

The positive integers W and H are the numbers of panels
on the living room
in the x- and y- direction, respectively.
The values of W and H are no more than 10.
The integer Pyx represents the state of the panel.
The value of Pyx means,

0: flawless panel (must not be covered),

1: scratched panel (must be covered). 

Output

For each data set,
your program should output a line containing one integer which 
represents the minimum number of the carpets to cover 
all of the scratched panels.

Sample Input

4 3
0 1 1 1
1 1 1 1
1 1 1 1
8 5
0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1
0 1 1 1 0 1 1 1
8 8
0 1 1 0 0 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 0 0 1 1 0
0 1 1 0 0 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 0 0 1 1 0
10 10
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
0 0

Output for the Sample Input

2
6
14
29



Sample input:
4 3
0 1 1 1
1 1 1 1
1 1 1 1
8 5
0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1
0 1 1 1 0 1 1 1
8 8
0 1 1 0 0 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 0 0 1 1 0
0 1 1 0 0 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 1 1 0 0 1 1 0
10 10
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
0 0


Sample output:
2
6
14
29

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
write = sys.stdout.write

def solve():
    W, H = map(int, readline().split())
    if W == 0:
        return False
    MP = [list(map(int, input().split())) for i in range(H)]
    C = [[0]*W for i in range(H)]
    for j in range(W):
        cur = 0
        for i in range(H-1, -1, -1):
            if MP[i][j]:
                cur += 1
            else:
                cur = 0
            C[i][j] = cur
    D = [[0]*W for i in range(H)]
    E = [[0]*W for i in range(H)]
    for i in range(H):
        st = []
        for j in range(W):
            c = C[i][j]
            last = j
            while st and c <= st[-1][0]:
                p, k = st.pop()
                last = k
                d = min(j - k, p)
                for e in range(k, j-d+1):
                    D[i][e] = max(D[i][e], d)
            st.append((c, last))
        while st:
            p, k = st.pop()
            d = min(W - k, p)
            if d:
                for e in range(k, W-d+1):
                    D[i][e] = max(D[i][e], d)
        st.append((W, 0))

    S = []
    Z = [0]*(W*H)
    ALL = 0
    for i in range(H):
        for j in range(W):
            if MP[i][j]:
                ALL |= 1 << (i*W + j)
            if E[i][j] < D[i][j]:
                S.append(i*W + j)
                Z[i*W + j] = D[i][j]
            E[i][j] = e = max(E[i][j], D[i][j])
            if e > 1:
                E[i+1][j] = max(E[i+1][j], e-1)
                E[i][j+1] = max(E[i][j+1], e-1)
                E[i+e-1][j+e-1] = max(E[i+e-1][j+e-1], 1)

    SN = len(S)
    L = max(W, H)
    T = [0]*(L+1)
    for d in range(1, L+1):
        v = 0
        for i in range(d):
            for j in range(d):
                v |= 1 << (i*W + j)
        T[d] = v
    BS = [0]*SN; CS = [0]*SN
    for i in range(SN):
        s = S[i]
        BS[i] = T[Z[s]] << s
        CS[i] = 1 << s

    memo = [{} for i in range(SN)]
    def dfs(i, state):
        if i == SN:
            if state == ALL:
                return 0
            return W*H
        if state in memo[i]:
            return memo[i][state]
        r = W*H
        if state & CS[i]:
            r = min(r, dfs(i+1, state))
        if state & BS[i] != BS[i]:
            r = min(r, dfs(i+1, state | BS[i]) + 1)
        memo[i][state] = r
        return r
    write("%d\n" % dfs(0, 0))
    return True
while solve():
    ...

Submission status: Accepted

Solution code:
def solve():
    from itertools import combinations
    from sys import stdin
    file_input = stdin
    
    while True:
        W, H = map(int, file_input.readline().split())
        if W == 0:
            break
        
        carpets = []
        state = 0
        
        # Largest Square algorithm
        prev = [0] * (W + 1)
        for i in range(1, H + 1):
            f_line = file_input.readline().split()
            cur = [0] * (W + 1)
            for j, tpl in enumerate(zip(f_line, prev, prev[1:]), start=1):
                p, p1, p2 = tpl
                if p == '1':
                    side = min(p1, p2, cur[j - 1]) + 1
                    cur[j] = side
                    
                    # making carpet bit sequence
                    line_bit = (1 << side) - 1
                    line_bit <<= (j - side)
                    c_b = line_bit
                    for k in range(side - 1):
                        c_b <<= W
                        c_b += line_bit
                    c_b <<= (W * (i - side))
                    carpets.append(c_b)
                else:
                    b = 1 << (j - 1)
                    b <<= (W * (i - 1))
                    state += b
            prev = cur
        
        flag = -1
        ans = 0
        while flag != state:
            flag = state
            
            c_check = dict(zip(carpets, [False] * len(carpets)))
            for c1, c2 in combinations(carpets, 2):
                if c_check[c1] or c_check[c2]:
                    continue
                overlap = c1 & c2
                if overlap == c2:
                    c_check[c2] = True
                elif overlap == c1:
                    c_check[c1] = True
            carpets = []
            for k, v in c_check.items():
                if not v:
                    carpets.append(k)
            
            for i in range(W * H):
                b = 1 << i
                if b & state:
                    continue
                t_carpets = []
                for c in carpets:
                    if b & c:
                        t_carpets.append(c)
                if len(t_carpets) == 1:
                    c = t_carpets[0]
                    state |= c
                    ans += 1
                    carpets.remove(c)
            
            carpets = list(set(c^(c & state) for c in carpets))
            
        goal = (1 << (W*H)) - 1
        if state == goal:
            print(ans)
            continue
        
        for i in range(1, len(carpets) + 1):
            for t_carpets in combinations(carpets, i):
                t_state = state
                for c in t_carpets:
                    t_state |= c
                if t_state == goal:
                    print(ans + i)
                    break
                else:
                    continue
                break
            else:
                continue
            break

solve()