Another chocolate maniac Time limit per test:0.25 sec. Memory limit per test:4096 KB Bob reallyLOVESChocolate. He thinks he never gets enough. Imagine his joy when he parents told him that they would buy him many rectangular chocolate pieces for his birthday. A piece of chocolate is a2x1Or1x2Rectangle. Bob's parents also bought him a nice birthday cake, which can is imagined as a matrix havingMRows andNColumns. Some positions on the cake is occupied by candles, the others is 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 Mann Er that no 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 don't to make Mon and Dad suspicious, Bob wants-to-place the chocolate pieces in such a-, that's no other piece MA Y be placed on the cake (that's, there won ' t exist any, adjacent empty squares). Find the minimal number of pieces which need to being placed on the cake, so that they does not overlap and no extra piece be added. Input The first line of the input contains 2 integers: M (1<=m<=70) and N (1<=n <=7). Next, M lines would follow, each of them containing N characters, describing the cake. The character on row I and columnJ of the cake is either a ' * ' (ASCII code representing a candle, or a '. ' (ASCII code ), representing an empty square. Output You should output one integer:the minimal amount of pieces of chocolate which need to being placed on the cake. Sample Input 5 5.*. **......**.**.*.. **..
Sample Output 4
|
|
idea: dp[I [S1] [S2] indicates that the i-1 row status is S2, and the number of scenarios where line I status is S2. Then there is the DFS enumeration state.
DFS (p, S1, S2, S3, CNT) (assuming the current enumeration is line I) p is the column number of the current enumeration, S1 is the state of the I-2 row,
S2 is the state of I-1, S3 is the state of the I row, CNT is the number of places to put.
#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;}
UVa Another chocolate maniac (pressure DP)