<p>Given a <code>m&nbsp;* n</code>&nbsp;matrix <code>seats</code>&nbsp;&nbsp;that represent seats distributions&nbsp;in a classroom.&nbsp;If a seat&nbsp;is&nbsp;broken, it is denoted by <code>&#39;#&#39;</code> character otherwise it is denoted by a <code>&#39;.&#39;</code> character.</p>

<p>Students can see the answers of those sitting next to the left, right, upper left and upper right, but he cannot see the answers of the student sitting&nbsp;directly in front or behind him. Return the <strong>maximum </strong>number of students that can take the exam together&nbsp;without any cheating being possible.</p>

<p>Students must be placed in seats in good condition.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img height="200" src="https://assets.leetcode.com/uploads/2020/01/29/image.png" width="339" />
<pre>
<strong>Input:</strong> seats = [[&quot;#&quot;,&quot;.&quot;,&quot;#&quot;,&quot;#&quot;,&quot;.&quot;,&quot;#&quot;],
&nbsp;               [&quot;.&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;.&quot;],
&nbsp;               [&quot;#&quot;,&quot;.&quot;,&quot;#&quot;,&quot;#&quot;,&quot;.&quot;,&quot;#&quot;]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teacher can place 4 students in available seats so they don&#39;t cheat on the exam. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> seats = [[&quot;.&quot;,&quot;#&quot;],
&nbsp;               [&quot;#&quot;,&quot;#&quot;],
&nbsp;               [&quot;#&quot;,&quot;.&quot;],
&nbsp;               [&quot;#&quot;,&quot;#&quot;],
&nbsp;               [&quot;.&quot;,&quot;#&quot;]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Place all students in available seats. 

</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> seats = [[&quot;#&quot;,&quot;.&quot;,&quot;<strong>.</strong>&quot;,&quot;.&quot;,&quot;#&quot;],
&nbsp;               [&quot;<strong>.</strong>&quot;,&quot;#&quot;,&quot;<strong>.</strong>&quot;,&quot;#&quot;,&quot;<strong>.</strong>&quot;],
&nbsp;               [&quot;<strong>.</strong>&quot;,&quot;.&quot;,&quot;#&quot;,&quot;.&quot;,&quot;<strong>.</strong>&quot;],
&nbsp;               [&quot;<strong>.</strong>&quot;,&quot;#&quot;,&quot;<strong>.</strong>&quot;,&quot;#&quot;,&quot;<strong>.</strong>&quot;],
&nbsp;               [&quot;#&quot;,&quot;.&quot;,&quot;<strong>.</strong>&quot;,&quot;.&quot;,&quot;#&quot;]]
<strong>Output:</strong> 10
<strong>Explanation:</strong> Place students in available seats in column 1, 3 and 5.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>seats</code>&nbsp;contains only characters&nbsp;<code>&#39;.&#39;<font face="sans-serif, Arial, Verdana, Trebuchet MS">&nbsp;and</font></code><code>&#39;#&#39;.</code></li>
	<li><code>m ==&nbsp;seats.length</code></li>
	<li><code>n ==&nbsp;seats[i].length</code></li>
	<li><code>1 &lt;= m &lt;= 8</code></li>
	<li><code>1 &lt;= n &lt;= 8</code></li>
</ul>

class Solution:
    def maxStudents(self, seats: List[List[str]]) -> int:

        def preComputeSeats(n):
            '''
            Givne the size of a column, return h[imask] = [(jmask0,diff0),(jmask1,diff1),...)]
            where imask represents the initial state of a row,
                jmask represents the valid state after some seats are taken according to the given 'imask', and 
                diff represents the indices of the seats that are taken.
            e.g:   imask  = 10001
                    jmask0 = 10001, diff0 = []   # take no seats
                    jmask1 = 11001, diff1 = [1]  # take seat on position 1
                    jmask2 = 10101, diff2 = [2]  # take seat on position 2
                    jmask3 = 10011, diff3 = [3]  # take seat on position 3
                    jmask4 = 11011, diff4 = [1,3]# take seats on position 1 and 3
            '''
            h = {}
            for i in range(2**n):
                s=f'{i:0{n}b}'
                h[i]=[]
                for j in range(2**n):
                    t=f'{j:0{n}b}'
                    diff,superset = [],True
                    for k in range(n):
                        if s[k]==t[k]:
                            continue
                        elif s[k]=='1' and t[k]=='0':
                            superset = False
                            break
                        else: #s[k]==0 and t[k]==1:
                            diff.append(k)
                    if superset:
                        match = True
                        for k in range(1,len(diff)):
                            if diff[k]-diff[k-1] <= 1:
                                match = False
                                break
                        if match:
                            h[i].append((j,diff))
            return h


        m = len(seats)
        n = len(seats[0])

        h = preComputeSeats(n)
        
        rowSeats = []
        for i in range(m):
            mask = 0
            for j in range(n):
                mask = mask<<1
                if seats[i][j]=='#':
                    mask += 1
            rowSeats.append(mask)
        
        rowSeats.append(2**n-1) # add a dummy row=1111..1
        
        dp = [[0 for _ in range(2**n)] for _ in range(m+1)]        
        # dp[i][imask] indicates the maximum number of seats that can be taken from rows 0 to i, given a state 'imask' on row i.
        
        for i in range(m+1):
            for imask in range(2**n):
                if imask & rowSeats[i] == rowSeats[i]: # imask is a superset of row[i]
                    #-- compute dp[i][imask]
                    maxV = 0
                    jmask = rowSeats[i-1]
                    for imaskx,diff in h[imask]:
                        # compute the state of the previous row i-1 according the seats taken in row i (== the seats in diff)
                        jmaskx = jmask
                        for j in diff:
                            if j-1 >=0:
                                jmaskx = jmaskx | (1<<n-j)
                            if j+1 <= n-1:
                                jmaskx = jmaskx | (1<<n-j-2)
                        
                        if dp[i-1][jmaskx] + len(diff) > maxV:
                            maxV = dp[i-1][jmaskx] + len(diff)
                    dp[i][imask] = maxV

        return max(dp[m])

17100