POJ 1185 炮兵阵地(状态压缩DP)

题目链接

写了久。DEBUG很久。

  1 #include <iostream>

  2 #include <cstring>

  3 #include <cstdio>

  4 #include <cstdlib>

  5 #include <map>

  6 #include <queue>

  7 #include <vector>

  8 #include <algorithm>

  9 using namespace std;

 10 int dp1[101][101],dp2[101][101];

 11 int que[101];

 12 int mat[101];

 13 int sum[101];

 14 char str[101][101];

 15 int m;

 16 int getsum(int x)

 17 {

 18     int i,ans = 0;

 19     for(i = 0; i < m; i ++)

 20     {

 21         if(x&(1<<i))

 22             ans ++;

 23     }

 24     return ans;

 25 }

 26 int main()

 27 {

 28     int n,i,j,k,u,num,ans;

 29 

 30     while(scanf("%d%d",&n,&m)!=EOF)

 31     {

 32 

 33         memset(dp1,-1,sizeof(dp1));

 34         memset(dp2,-1,sizeof(dp2));

 35         memset(sum,0,sizeof(sum));

 36         memset(mat,0,sizeof(mat));

 37         num = 1;

 38         for(i = 1; i <= n; i ++)

 39             scanf("%s",str[i]);

 40         for(i = 1; i <= n; i ++)

 41         {

 42             for(j = 0; j < m; j ++)

 43             {

 44                 if(str[i][j] == 'H')

 45                     mat[i] = mat[i] + (1<<j);

 46             }

 47         }

 48         for(i = 0; i < 1<<m; i ++)

 49         {

 50             if(i&(i<<1))

 51                 continue;

 52             if(i&(i>>1))

 53                 continue;

 54             if(i&(i<<2))

 55                 continue;

 56             if(i&(i>>2))

 57                 continue;

 58             sum[num] = getsum(i);

 59             que[num++] = i;

 60         }

 61         for(i = 1; i < num; i ++)

 62         {

 63             if(que[i]&mat[1])

 64                 continue;

 65             dp1[i][1] = sum[i];

 66         }

 67         for(i = 2; i <= n; i ++)

 68         {

 69             for(j = 1; j < num; j ++)

 70             {

 71                 if(mat[i]&que[j]) continue;

 72                 for(k = 1; k < num; k ++)

 73                 {

 74                     if(que[j]&que[k]) continue;

 75                     for(u = 1; u < num; u ++)

 76                     {

 77                         if(que[j]&que[u]) continue;

 78                         if(dp1[k][u] == -1) continue;

 79                         if(dp2[j][k] < dp1[k][u] + sum[j])

 80                             dp2[j][k] = dp1[k][u] + sum[j];

 81                     }

 82                 }

 83             }

 84             for(j = 1; j < num; j ++)

 85             {

 86                 for(k = 1; k < num; k ++)

 87                 {

 88                     dp1[j][k] = dp2[j][k];

 89                     dp2[j][k] = -1;

 90                 }

 91             }

 92         }

 93         ans = 0;

 94         for(j = 1; j < num; j ++)

 95         {

 96             for(k = 1; k < num; k ++)

 97             {

 98                 ans = max(ans,dp1[j][k]);

 99             }

100         }

101         printf("%d\n",ans);

102     }

103     return 0;

104 }

 

你可能感兴趣的:(poj)