sgu132:Another Chocolate Maniac

来源:互联网 发布:java超市管理系统 编辑:程序博客网 时间:2024/05/01 00:56
132. Another Chocolate Maniac




time limit per test: 0.25 sec. 
memory limit per test: 4096 KB


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 column j 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


131和132连一起两道状压dp,sgu也真是够了......
又是一道棋盘覆盖模型,只不过有两个特点,一个是有障碍点,这好说,把它当成初始状态即可;另一个是不需要填满,因此相应的状态就容易推一些。
考虑不需要填满,而且要满足无后效性,我们保存连续两行的状态。
类似于131的,从第一列开始:
(根据状态j、k,来推得状态‘k、s, 即由i-1、i行来推得i、i+1行)
假设我们推到了第cur列,我们考虑只在i中方方块。
(1)横着放,满足横着放的地方是空的即可
(2)竖着放,满足竖着放的地方是空的即可,且这种状态同时会影响到i+1行
(3)不放

然而一种状态成立的条件即为没有连续空着的两个格子,在推的时候判断退出即可。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXS = 1<<7, MAXM = 75, MAXN = 7, INF = 1<<30;int n = 0, m = 0;int f[2][MAXS][MAXS] = {0};int ini[MAXM] = {0};int now = 0;int binary[MAXN+1] = {1};int full = 0;int srcj = 0, srck = 0;void dfs(int cur, int j, int k, int s, int add){  if(cur > 0 && !(j&binary[cur-1]) && !(k&binary[cur-1])) return ;  if(cur > 1 && !(k&binary[cur-1]) && !(k&binary[cur-2])) return ;    if(cur == n)  {    f[now][k][s] = min(f[now][k][s], f[now^1][srcj][srck]+add);return ;  }    dfs(cur+1, j, k, s, add);  if(cur < n-1 && !(k&binary[cur]) && !(k&binary[cur+1]))    dfs(cur+1, j, k|binary[cur]|binary[cur+1], s, add+1);  if(!(k&binary[cur]) && !(s&binary[cur]))    dfs(cur+1, j, k|binary[cur], s|binary[cur], add+1);}int main(){  char c = '\0';  scanf("%d%d\n", &m, &n);  for(int i = 1; i <= m; ++i)  {    for(int j = 1; j <= n; ++j){  c = getchar();  ini[i] = ini[i]<<1|(c=='*');    }    scanf("\n");   }  for(int i = 1; i <= n; ++i) binary[i] = binary[i-1]<<1;  full = binary[n]-1;    for(int i = 0; i <= full; ++i)    for(int j = 0; j <= full; ++j)      f[1][i][j] = INF;  f[1][full][ini[1]] = 0;  for(int i = 1; i <= m; ++i)  {    for(int j = 0; j <= full; ++j)  for(int k = 0; k <= full; ++k)    f[now][j][k] = INF;for(int j = 0; j <= full; ++j)  for(int k = 0; k <= full; ++k)    if(f[now^1][j][k] != INF)    {      srcj = j, srck = k;  dfs(0, j, k, ini[i+1], 0);    }now ^= 1;  }  int ans = INF;  for(int i = 0; i <= full; ++i)    ans = min(ans, f[now^1][i][0]);  printf("%d\n", ans);  return 0;}


0 0
原创粉丝点击