HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539

 

郑厂长系列故事——排兵布阵

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3289    Accepted Submission(s): 1144


Problem Description
  郑厂长不是正厂长
  也不是副厂长
  他根本就不是厂长
  事实上
  他是带兵打仗的团长

  一天,郑厂长带着他的军队来到了一个n*m的平原准备布阵。
  根据以往的战斗经验,每个士兵可以攻击到并且只能攻击到与之曼哈顿距离为2的位置以及士兵本身所在的位置。当然,一个士兵不能站在另外一个士兵所能攻击到的位置,同时因为地形的原因平原上也不是每一个位置都可以安排士兵。
  现在,已知n,m 以及平原阵地的具体地形,请你帮助郑厂长计算该阵地,最多能安排多少个士兵。
 

 

Input
输入包含多组测试数据;
每组数据的第一行包含2个整数n和m (n <= 100, m <= 10 ),之间用空格隔开;
接下来的n行,每行m个数,表示n*m的矩形阵地,其中1表示该位置可以安排士兵,0表示该地形不允许安排士兵。
 

 

Output
请为每组数据计算并输出最多能安排的士兵数量,每组数据输出一行。
 

 

Sample Input
6 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 

 

Sample Output
2
 

 

Source

 

 

题解:

POJ1185 炮兵阵地此题的变形。

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const double EPS = 1e-6;
15 const int INF = 2e9;
16 const LL LNF = 9e18;
17 const int MOD = 1e5;
18 const int MAXN = 1e3+10;
19 
20 int row[110], sta[170], cnt[170], dp[110][170][170];
21 int main()
22 {
23     int n, m;
24     while(scanf("%d%d", &n,&m)!=EOF)
25     {
26         for(int i = 1; i<=n; i++)
27         {
28             int val;
29             row[i] = 0;
30             for(int j = 0; j<m; j++)
31             {
32                 scanf("%d", &val);
33                 row[i] += (val==0)*(1<<j);
34             }
35         }
36 
37         int tot = 0;
38         for(int s = 0; s<(1<<m); s++)
39         {
40             if(!(s&(s<<2)))
41             {
42                 sta[++tot] = s;
43                 cnt[tot] = 0;
44                 for(int j = 0; j<m; j++)
45                     if(s&(1<<j))
46                         cnt[tot]++;
47             }
48         }
49 
50         memset(dp, 0, sizeof(dp));
51         for(int i = 1; i<=n; i++)
52         {
53             for(int j = 1; j<=tot; j++)
54             {
55                 int s1 = sta[j];
56                 if(i>2 && (s1&row[i-2])) continue;
57                 for(int k = 1; k<=tot; k++)
58                 {
59                     int s2 = sta[k];
60                     if(i>1 && (s2&row[i-1])) continue;
61                     if(s1&(s2<<1) || (s1&(s2>>1)) ) continue;
62                     for(int t = 1; t<=tot; t++)
63                     {
64                         int s3 = sta[t];
65                         if(s3&row[i]) continue;
66                         if(s2&(s3<<1) || (s2&(s3>>1)) ) continue;
67                         if(s1&s3) continue;
68                         dp[i][k][t] = max(dp[i][k][t], dp[i-1][j][k]+cnt[t]);
69                     }
70                 }
71             }
72         }
73 
74         int ans = 0;
75         for(int i = 1; i<=tot; i++)
76             for(int j = 1; j<=tot; j++)
77                 ans = max(ans, dp[n][i][j]);
78         printf("%d\n", ans);
79     }
80 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8509361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值