132. Another Chocolate Maniac time limit per test: 0.25 sec. Bob really LOVES chocolate. He thinks he never gets enough. Imagine his joy when his parents told him that they would buy him many rectangular chocolate pieces for his birthday. A piece of chocolate is a 2x1 or 1x2 rectangle. Bob's parents also bought him a nice birthday cake, which can be imagined as a matrix having M rows and N columns. Some positions on the cake are occupied by candles, the others are empty. Bob's parents asked their son to place as many chocolate pieces as he can on the empty squares on the cake, in such a manner that no two chocolate pieces overlap. However, he would like to keep the chocolate pieces to himself. That's why, he wants to place only a minimal amount of them on the cake and keep the rest. In order not to make Mon and Dad suspicious, Bob wants to place the chocolate pieces in such a way, that no other piece may be placed on the cake (that is, there won't exist any two adjacent empty squares). Find the minimal number of pieces which need to be placed on the cake, so that they do not overlap and no extra piece may be added. Input The first line of the input contains 2 integers: M (1<=M<=70) and N (1<=N<=7). Next, M lines will follow, each of them containing N characters, describing the cake. The character on row i and columnj of the cake may be either a '*' (ASCII code 42), representing a candle, or a '.' (ASCII code 46), representing an empty square. Output You should output one integer: the minimal amount of pieces of chocolate which need to be placed on the cake. Sample Input 5 5 .*..* *.... ..**. **.*. .**.. Sample Output 4 |
思路 :dp[ i ][ s1 ][ s2 ] 表示第 i -1 行狀態爲s2,第 i 行狀態爲s2的方案數。然後就是dfs枚舉狀態。
dfs(p , s1 , s2 , s3 , cnt )(假設當前枚舉的是第 i 行)p爲當前枚舉的列號 ,s1爲i-2行的狀態 ,
s2爲i-1的狀態,s3爲i行的狀態,cnt爲放置的數目。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int B[8]= {1,2,4,8,16,32,64,128};
const int inf=1<<30;
const int maxn=1<<8;
const int N=75;
int dp[2][maxn][maxn],a[N],cur,len,n,m,tmp,now;
char ch[10];
void input()
{
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%s",ch);
for(int j=0; j<m; j++)
{
a[i]<<=1;
if(ch[j]=='*') a[i]+=1;
}
}
}
void initial()
{
cur=0;
len=1<<m;
for(int i=0; i<2; i++)
for(int j=0; j<maxn; j++)
for(int k=0; k<maxn; k++)
dp[i][j][k]=inf;
dp[cur][len-1][a[1]]=0;
}
void dfs(int p,int s1,int s2,int s3,int cnt)
{
if(p>0 && !(s1 & B[p-1]) && !(s2 & B[p-1])) return ;
if(p>1 && !(s2 & B[p-1]) && !(s2 & B[p-2])) return ;
if(p>=m)
{
dp[cur][s2][s3]=min(dp[cur][s2][s3],dp[cur^1][tmp][now]+cnt);
return ;
}
dfs(p+1,s1,s2,s3,cnt);
if(p<m-1 && !(s2 & B[p]) && !(s2 & B[p+1])) dfs(p+1,s1,s2|B[p]|B[p+1],s3,cnt+1);
if(!(s2 & B[p]) && !(s3 & B[p])) dfs(p+1,s1,s2|B[p],s3|B[p],cnt+1);
}
void solve()
{
for(int i=1; i<=n; i++)
{
cur^=1;
for(int j=0; j<len; j++)
for(int k=0; k<len; k++)
if(dp[cur^1][j][k]<inf)
{
tmp=j;
now=k;
dfs(0,j,k,a[i+1],0);
}
for(int j=0; j<len; j++)
for(int k=0; k<len; k++)
dp[cur^1][j][k]=inf;
}
int ans=inf;
for(int i=0; i<len; i++) ans=min(ans,dp[cur][i][0]);
printf("%d\n",ans);
}
int main()
{
input();
initial();
solve();
return 0;
}