转自:http://blog.csdn.net/a497406594/article/details/38442893

Kill the Monsters


Time Limit: 7 Seconds Memory Limit: 32768 KB

In order to celebrate the 8th anniversary of ZOJ, watashi introduces a strange game to other ZJU ACM team members. The board of the game consists of 20X20 grids.

There're some monsters in the grid and 40 buttons along the board. Each row and column is controlled by a single button, while each button can be pushed ONLY ONCE.

  • If you push a row-controlling button, the health point of all the monsters in the corresponding row will decrease 1.
  • If you push a column-controlling button, the health point of all the monsters in the corresponding column will decrease 1.

The health point of each monster is 2 initially, and if the monster's health point is 0, we say that the monster has been killed. The goal of the game is to kill as many monsters as possible and you cannot push more than N buttons.

Though Watashi plays the game day and night, he cannot always achieve the perfect goal. Please help him to find the best solution of some specific boards.

Input

The first line of each test case contains the numbers N ( 1 <= N <= 40 ).

Then there're 20 lines, each with 20 characters. The jth character in the ith line correspond to the state in grid (i, j). '.' for the empty grid, '#' for a monster.

The input will contain no more than 10 test cases.

The last test case is followed by one zero.

Output

For each test case output one number, the maximum number of dead monsters.

Sample Input

  1. 3
  2. ....................
  3. ....................
  4. ..###...............
  5. ....................
  6. ....................
  7. ....................
  8. ....................
  9. ....................
  10. ....................
  11. ....................
  12. ....................
  13. ....................
  14. ....................
  15. ....................
  16. ....................
  17. ....................
  18. ....................
  19. ....................
  20. ....................
  21. ....................
  22. 10
  23. ####################
  24. ####################
  25. ####################
  26. ####################
  27. ####################
  28. ####################
  29. ####################
  30. ####################
  31. ####################
  32. ####################
  33. ####################
  34. ####################
  35. ####################
  36. ####################
  37. ####################
  38. ####################
  39. ####################
  40. ####################
  41. ####################
  42. ####################
  43. 0

Sample Output

  1. 2
  2. 25

题意:每次给出一张20*20的图,#表示拥有两滴血的怪物,有两种减怪物血的方式,一种是整列减1滴血,另一种是整行减1滴血,且每行每列只能减一次。提供n次,问最多能杀掉多少怪物。

思路:主要还是围绕着消除哪些行跟哪些列,并使行+列=n,这样很容易就相当枚举其中一个,行不容易处理,反而列可以用状压dp进行处理。用1表示选取了当前列。接下来只要选取所在列下,对能每行能消除的个数进行排序,选取最多能消除的前几行。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<queue>
  8. #include<map>
  9. #include<vector>
  10.  
  11. #define N 25
  12. #define M 15
  13. #define mod 1000000007
  14. #define mod2 100000000
  15. #define ll long long
  16. #define maxi(a,b) (a)>(b)? (a) : (b)
  17. #define mini(a,b) (a)<(b)? (a) : (b)
  18.  
  19. using namespace std;
  20.  
  21. int n;
  22. char s[N][N];
  23. int have[N];
  24. int ma;
  25. int tot;
  26. int cou[N];
  27. int cc[(<<)+];
  28. int now;
  29.  
  30. void ini1()
  31. {
  32. memset(cc,,sizeof(cc));
  33. for(int i=;i<(<<);i++)
  34. {
  35. int cnt=;
  36. for(int j=;j<;j++)
  37. if(i&(<<j))
  38. cnt++;
  39. cc[i]=cnt;
  40. }
  41. }
  42.  
  43. void ini()
  44. {
  45. int i,j;
  46. ma=;
  47. memset(have,,sizeof(have));
  48. for(i=;i<;i++){
  49. scanf("%s",s[i]);
  50. }
  51. for( i=;i<;i++)
  52. for( j=;j<;j++)
  53. if(s[i][j]=='#')
  54. have[i]+=(<<j);
  55.  
  56. }
  57.  
  58. bool cmp(int i,int j)
  59. {
  60. return i>j;
  61. }
  62.  
  63. void solve()
  64. {
  65. int o,i,j,temp;
  66. for(o=;o<tot;o++)
  67. {
  68. memset(cou,,sizeof(cou));
  69. if(cc[o]>n) continue;
  70. // printf(" o=%d now=%d\n",o,now);
  71. for(i=;i<;i++){
  72. cou[i]=cc[ o &have[i] ];
  73. }
  74. sort(cou,cou+,cmp);
  75. //for(j=0;j<20;j++) printf(" j=%d cou=%d")
  76. temp=;
  77. for(j=;j<n-cc[o] && j<;j++){ //注意这里j<20
  78. temp+=cou[j];
  79. }
  80. ma=max(ma,temp);
  81. }
  82. }
  83.  
  84. int main()
  85. {
  86. // freopen("data.in","r",stdin);
  87. // freopen("data.out","w",stdout);
  88. //scanf("%d",&T);
  89. // for(int cnt=1;cnt<=T;cnt++)
  90. // while(T--)
  91. tot=<<;
  92. ini1();
  93. while(scanf("%d",&n)!=EOF)
  94. {
  95. if(n==) break;
  96. //printf(" %d\n",n);
  97. ini();
  98. solve();
  99. printf("%d\n",ma);
  100. //cout<<tot<<endl;
  101. }
  102.  
  103. return ;
  104. }

ZOJ 3306 状压dp的更多相关文章

  1. Problem Arrangement ZOJ - 3777(状压dp + 期望)

    ZOJ - 3777 就是一个入门状压dp期望 dp[i][j] 当前状态为i,分数为j时的情况数然后看代码 有注释 #include <iostream> #include <cs ...

  2. ZOJ - 3777(状压dp)

    The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...

  3. Most Powerful(ZOJ 3471状压dp)

    题意:n个原子,两两相撞其中一个消失,产生能量,给出任意两原子相撞能产生的能量,求能产生的最大能量. 分析:dp[i]表示情况为i时产生的最大能量 /*#include <map> #in ...

  4. Survival(ZOJ 2297状压dp)

    题意:有n个怪,已知杀死第i个怪耗费的血和杀死怪恢复的血,和杀死boss耗的血,血量不能超过100,若过程中血小于0,则失败,问 是否能杀死boss(boss最后出现). 分析:就是求杀死n个怪后剩余 ...

  5. zoj 3812 状压dp

    转载:http://blog.csdn.net/qian99/article/details/39138329 题意:给出n个物品,每个物品有两种属性Wi,Ti,有q组查询,每组查询要求在n个物品中选 ...

  6. Long Dominoes(ZOJ 2563状压dp)

    题意:n*m方格用1*3的方格填充(不能重叠)求有多少种填充方法 分析:先想状态,但想来想去就是觉得不能覆盖所有情况,隔了一天,看看题解,原来要用三进制 0 表示横着放或竖放的最后一行,1表示竖放的中 ...

  7. Travel(HDU 4284状压dp)

    题意:给n个城市m条路的网图,pp在城市1有一定的钱,想游览这n个城市(包括1),到达一个城市要一定的花费,可以在城市工作赚钱,但前提有工作证(得到有一定的花费),没工作证不能在该城市工作,但可以走, ...

  8. ZOJ 3723 (浙大月赛)状压DP

    A了一整天~~~终于搞掉了. 真是血都A出来了. 题目意思很清楚,肯定是状压DP. 我们可以联系一下POJ 1185  炮兵阵地,经典的状压DP. 两道题的区别就在于,这道题的攻击是可以被X挡住的,而 ...

  9. ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds      Me ...

随机推荐

  1. PLSQL练习-数据共享与整合技术

    1.编写一个存储过程,根据输入的工作类型,输出该工作的平均工资. 命令如下: 创建存储过程: create or replace procedure avgsal(v_job in emp.job%t ...

  2. flask--Django 基本使用

    #导入flaskfrom flask import Flask #创建应用 app = Flask(__name__) #创建根路径视图 @app.route('/') def hello_world ...

  3. Python-OpenCV:cv2.imread(),cv2.imshow(),cv2.imwrite()

    为什么使用Python-OpenCV? 虽然python 很强大,而且也有自己的图像处理库PIL,但是相对于OpenCV 来讲,它还是弱小很多.跟很多开源软件一样OpenCV 也提供了完善的pytho ...

  4. for..in...时,注意hasOwnProperty验证

    for..in...时,注意hasOwnProperty验证 var obj = { a: 10, b: 20 }; // 注意词句代码 Object.prototype.c = 30; var it ...

  5. POI读word doc 03 文件的两种方法

    Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的.在hwpf里面我们使用HWPFDocument来表示一个word doc文档.在HWPFDocument里面有这么几个 ...

  6. javaEE(14)_文件上传下载

    一.文件上传概述 1.实现web开发中的文件上传功能,需完成如下二步操作: •在web页面中添加上传输入项•在servlet中读取上传文件的数据,并保存到本地硬盘中. 2.如何在web页面中添加上传输 ...

  7. leetcode-3-basic-divide and conquer

    解题思路: 因为这个矩阵是有序的,所以从右上角开始查找.这样的话,如果target比matrix[row][col]小,那么就向左查找:如果比它大,就 向下查找.如果相等就找到了,如果碰到边界,就说明 ...

  8. gnu printf可变参数宏

    可变参数的宏 标准C只支持可变参数的函数,意味着函数的参数可以是不固定的 例如printf()函数的原型是int printf(const char *format [,argument]...) 而 ...

  9. 菜鸟的《Linux程序设计》学习——MySQL数据库安装、配置及基本操作

    1. MySQL数据库: 在涉及到一些大型的Web系统或者嵌入式软件的开发时,都少不了用数据库来管理数据.在Windows操作系统下,使用过各种各样的数据库,如:sqlServer.Oracle.My ...

  10. ZZULIoj 1907 小火山的宝藏收益

    Description      进去宝藏后, 小火山发现宝藏有N个房间,且这n个房间通过N-1道门联通.   每一个房间都有一个价值为Ai的宝藏, 但是每一个房间也都存在一个机关.如果小火山取走了这 ...