• 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
    ", ans);
    79     }
    80 }
    View Code
  • 相关阅读:
    Developer 转型记:一个开发平台的“魔力”
    实践录丨如何在鲲鹏服务器OpenEuler操作系统中快速部署OpenGauss数据库
    一图看懂华为云DevCloud如何应对敏捷开发的测试挑战
    华为云GaussDB(DWS)内存知识点,你知道吗?
    在人工智能时代追逐的“后浪”
    【华为云技术分享】DLI跨源|当DLI遇见MongoDB
    授人以渔:stm32资料查询技巧
    云小课 | IPv4枯了,IPv6来了
    揭秘淘宝平台广告策略,拆解最佳投放实践
    520了,用32做个简单的小程序
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8509361.html
Copyright © 2020-2023  润新知