Dynamic Programming21 sections · 916 units
Open in Course

Counting Tilings - Transition Formula

Combining paths

For each valid transition from cur_maskcur\_mask to next_masknext\_mask, add dp[c][cur_mask]dp[c][cur\_mask] to dp[c+1][next_mask]dp[c+1][next\_mask]. This sums all ways to reach the new profile. Precompute transitions: for each cur_maskcur\_mask, run the recursive placement function to find all reachable next_masknext\_mask values.

Store these in trans[cur_mask]trans[cur\_mask]. Then the DP loop becomes: for each cc, for each cur_maskcur\_mask, for each next_masknext\_mask in trans[cur_mask]trans[cur\_mask], update. This separation makes code cleaner and faster. You compute transitions once and reuse them for all columns.