带有障碍物的1*2铺格子 class Solution {public: int domino(int n, int m, vectorvectorint broken) { int a[10],f[10][256],ans=0,o[256],i,j,k; memset(a,0,sizeof(a)); for(auto b:broken)a[b[0]]|=1b[1]; memset(f,128,sizeof(f)); f[0][(1m)-1
带有障碍物的1*2铺格子
class Solution { public: int domino(int n, int m, vector<vector<int>>& broken) { int a[10],f[10][256],ans=0,o[256],i,j,k; memset(a,0,sizeof(a)); for(auto b:broken)a[b[0]]|=1<<b[1]; memset(f,128,sizeof(f)); f[0][(1<<m)-1]=0; //pre process the number of 1 in [i] for(i=1;i<1<<m;i++)o[i]=o[i>>1]+(i&1); for(i=0;i<n;i++) { for(j=0;j<1<<m;j++)f[i+1][0]=max(f[i+1][0],f[i][j]); //vertical put //因为每种状态都枚举到了,所以竖着插若干个 if(i) for(j=0;j<1<<m;j++) for(k=0;k<1<<m;k++) if(!(j&k)&&!(a[i-1]&k)&&!(a[i]&k)) f[i+1][k]=max(f[i+1][k],f[i][j]+o[k]); for(j=0;j+1<m;j++) //j col and j+1 col is empty //ping fang yi ge if(!(a[i]>>j&1)&&!(a[i]>>j+1&1)) for(k=0;k<1<<m;k++) if(!(k>>j&1)&&!(k>>j+1&1)) f[i+1][k|1<<j|1<<j+1]=max(f[i+1][k|1<<j|1<<j+1],f[i+1][k]+1); } for(i=0;i<1<<m;i++)ans=max(ans,f[n][i]); return ans; } };