题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4539

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

Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 65535/32768 K (Java/Others)
#### 问题描述
> 郑厂长不是正厂长
> 也不是副厂长
> 他根本就不是厂长
> 事实上
> 他是带兵打仗的团长
> 一天,郑厂长带着他的军队来到了一个n*m的平原准备布阵。
> 根据以往的战斗经验,每个士兵可以攻击到并且只能攻击到与之曼哈顿距离为2的位置以及士兵本身所在的位置。当然,一个士兵不能站在另外一个士兵所能攻击到的位置,同时因为地形的原因平原上也不是每一个位置都可以安排士兵。
> 现在,已知n,m 以及平原阵地的具体地形,请你帮助郑厂长计算该阵地,最多能安排多少个士兵。

输入

输入包含多组测试数据;

每组数据的第一行包含2个整数n和m (n <= 100, m <= 10 ),之间用空格隔开;

接下来的n行,每行m个数,表示n*m的矩形阵地,其中1表示该位置可以安排士兵,0表示该地形不允许安排士兵。

输出

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

样例输入

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

样例输出

2

题意

每个点会攻击离它哈密顿距离为2的所有点,以及它自己所在的点,现在给你一个n*m的棋盘,有些点上不能放棋子,问最多能放多少个棋子,且任意两个棋子都不会互相攻击。

题解

dp[cur][i][j]表示第cur-1行状态为i,第cur行状态为j,能装下的最大不冲突棋子数。

做法和[port]差不多

  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<ctime>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<string>
  10. #include<bitset>
  11. #include<cstdlib>
  12. #include<cstring>
  13. #include<iostream>
  14. #include<algorithm>
  15. #include<functional>
  16. using namespace std;
  17. #define X first
  18. #define Y second
  19. #define mkp make_pair
  20. #define lson (o<<1)
  21. #define rson ((o<<1)|1)
  22. #define mid (l+(r-l)/2)
  23. #define sz() size()
  24. #define pb(v) push_back(v)
  25. #define all(o) (o).begin(),(o).end()
  26. #define clr(a,v) memset(a,v,sizeof(a))
  27. #define bug(a) cout<<#a<<" = "<<a<<endl
  28. #define rep(i,a,b) for(int i=a;i<(b);i++)
  29. #define scf scanf
  30. #define prf printf
  31. typedef long long LL;
  32. typedef vector<int> VI;
  33. typedef pair<int,int> PII;
  34. typedef vector<pair<int,int> > VPII;
  35. const int INF=0x3f3f3f3f;
  36. const LL INFL=10000000000000000LL;
  37. const double eps=1e-9;
  38. const double PI = acos(-1.0);
  39. //start----------------------------------------------------------------------
  40. const int maxn=111;
  41. const int maxm=11;
  42. const int maxs=400;
  43. LL dp[2][maxs][maxs];
  44. int n,m;
  45. ///处理出有效状态
  46. VI sta;
  47. vector<LL> sumv;
  48. void pre(){
  49. for(int i=0;i<(1<<10);i++){
  50. int cnt=0;
  51. bool su=true;
  52. for(int j=0;j<10;j++){
  53. if(!(i&(1<<j))) continue;
  54. cnt++;
  55. if(j-2>=0&&(i&(1<<(j-2)))){ su=false; break; }
  56. }
  57. if(!su) continue;
  58. sta.pb(i);
  59. sumv.pb(cnt);
  60. }
  61. }
  62. int tot;
  63. int arr[maxn][maxm];
  64. bool ok(int x,int i){
  65. for(int j=0;j<m;j++){
  66. if(!(x&(1<<j))) continue;
  67. if(arr[i][j]==0) return false;
  68. }
  69. return true;
  70. }
  71. bool ok2(int pp,int p,int u){
  72. for(int j=0;j<m;j++){
  73. if(!(u&(1<<j))) continue;
  74. if(j&&(p&(1<<(j-1)))) return false;
  75. if(j<m-1&&(p&(1<<(j+1)))) return false;
  76. if(pp>=0&&(pp&(1<<j))) return false;
  77. }
  78. return true;
  79. }
  80. void dealone(){
  81. LL ans=0;
  82. for(int i=0;i<tot;i++){
  83. if(ok(sta[i],0)) ans=max(ans,sumv[i]);
  84. }
  85. prf("%lld\n",ans);
  86. }
  87. int main() {
  88. pre();
  89. while(scf("%d%d",&n,&m)==2) {
  90. tot=upper_bound(all(sta),(1<<m)-1)-sta.begin();
  91. // bug(tot);
  92. rep(i,0,n) rep(j,0,m){
  93. scf("%d",&arr[i][j]);
  94. }
  95. if(n==1){ dealone(); continue; }
  96. int cur=0;
  97. clr(dp[cur],0);
  98. for(int i=0;i<tot;i++){
  99. if(!ok(sta[i],0)) continue;
  100. for(int j=0;j<tot;j++){
  101. if(!ok(sta[j],1)) continue;
  102. if(!ok2(-1,sta[i],sta[j])) continue;
  103. dp[cur][i][j]=sumv[i]+sumv[j];
  104. }
  105. }
  106. // bug(dp[cur][17][4]);
  107. for(int t=2;t<n;t++){
  108. cur^=1;
  109. clr(dp[cur],0);
  110. for(int k=0;k<tot;k++){
  111. if(!ok(sta[k],t)) continue;
  112. for(int j=0;j<tot;j++){
  113. if(!ok(sta[j],t-1)) continue;
  114. if(!ok2(-1,sta[j],sta[k])) continue;
  115. for(int i=0;i<tot;i++){
  116. if(!ok(sta[i],t-2)) continue;
  117. if(!ok2(sta[i],sta[j],sta[k])) continue;
  118. dp[cur][j][k]=max(dp[cur][j][k],dp[cur^1][i][j]+sumv[k]);
  119. }
  120. }
  121. }
  122. }
  123. LL ans=0;
  124. for(int i=0;i<tot;i++){
  125. for(int j=0;j<tot;j++){
  126. ans=max(ans,dp[cur][i][j]);
  127. }
  128. }
  129. prf("%lld\n",ans);
  130. }
  131. return 0;
  132. }
  133. //end-----------------------------------------------------------------------

HDU 4539 郑厂长系列故事——排兵布阵 状压dp的更多相关文章

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...

  2. hdu_4539_郑厂长系列故事——排兵布阵(状压DP|最大团)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 题意:中文,不解释 题解:将每一行的状态压缩,然后进行DP,也可以用最大团做.这里我用的DP # ...

  3. HDU 4539郑厂长系列故事――排兵布阵(状压DP)

    HDU 4539  郑厂长系列故事――排兵布阵 基础的状压DP,首先记录先每一行可取的所哟状态(一行里互不冲突的大概160个状态), 直接套了一个4重循环居然没超时我就呵呵了 //#pragma co ...

  4. HDU 4539 郑厂长系列故事——排兵布阵

    http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Others) ...

  5. POJ 1185 - 炮兵阵地 & HDU 4539 - 郑厂长系列故事——排兵布阵 - [状压DP]

    印象中这道题好像我曾经肝过,但是没肝出来,现在肝出来了也挺开心的 题目链接:http://poj.org/problem?id=1185 Time Limit: 2000MS Memory Limit ...

  6. HDU 4539 郑厂长系列故事――排兵布阵(曼哈顿距离)

    这虽然是中文题,然而没看懂,不懂的地方,就是在曼哈顿距离这块,网上搜索了一下,写了个程序,是测试曼哈顿距离的. 曼哈顿距离:两点(x1,y1)(x2,y2)的曼哈顿距离为|x1-x2|+|y1-y2| ...

  7. HDU 4529 郑厂长系列故事——N骑士问题 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4529 郑厂长系列故事--N骑士问题 Time Limit: 6000/3000 MS (Java/O ...

  8. 郑厂长系列故事——排兵布阵 hdu4539(状态压缩DP)

    郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  9. HDU-4539郑厂长系列故事——排兵布阵(状态压缩,动态规划)

    郑厂长系列故事--排兵布阵 Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total ...

随机推荐

  1. C++程序设计--实验二

    第二次实验主要内容是函数重载,快速排序及其模板实现,简单的user类实现. 实验结论: 一.函数重载编程练习 /*编写重载函数add(),实现对int型,double型和Complex型数据的加法.在 ...

  2. x01.calc: 编程语言

    想写终极程序,大都去写操作系统或编程语言了.编程语言可以极其复杂如C,也可以极简,只处理加减乘除如 calc. 1. 词法分析 %{ #include <stdio.h> #include ...

  3. PCIE_DMA实例一:xapp1052详细使用说明

    一:前言 很多和我一样初学pcie的硬件工程师都会遇到这样一个问题,看了不少pcie相关的资料,还是搞不清这玩意儿到底该怎么用.于是我们打开ISE的core_generator工具,生成了一个pcie ...

  4. springboot快速入门(四)——数据库操作

    一.Spring-Data-Jpa的引入 先给出官方文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ IBM ...

  5. WPF 扩大,回弹效果

    原文:WPF 扩大,回弹效果 <Window x:Class="Fish.AccountBook.View.Test.PanelWindow" xmlns="htt ...

  6. 5 数据结构、栈、队列、链表、list、dict、迷宫问题

    1.什么是数据结构 2.栈:后进先出 1.什么是栈 栈(Stack)是一个数据集合,可以理解为只能在一端进行插入或删除操作的列表.   2.栈的Python实现 stack = [] stack.ap ...

  7. Failed to chmod /Users/fei/Library/Developer/CoreSimulator/Devices/DB5AC3C0错误的解决办法

    当XCode遇到此问题的时候,可通过重启模拟器和XCode来解决,拿走不谢

  8. ubuntu 图形化界面 gui 桌面版 root登录 sorry,that didn't work.please try again! 抱歉,认证失败。请重试

    出现这种问题,用下面的方法就行了 https://jingyan.baidu.com/article/bad08e1e224b2709c85121f1.html 而且我发现,因为我用的是英文版的ubu ...

  9. jquery 直接访问图片路径

    jQuery("#img").attr("src",function(){return this.src+"?"});

  10. mysql索引原理及查询速度优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...