ZOJ 3306 状压dp
转自: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
- 3
- ....................
- ....................
- ..###...............
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- ....................
- 10
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- ####################
- 0
Sample Output
- 2
- 25
题意:每次给出一张20*20的图,#表示拥有两滴血的怪物,有两种减怪物血的方式,一种是整列减1滴血,另一种是整行减1滴血,且每行每列只能减一次。提供n次,问最多能杀掉多少怪物。
思路:主要还是围绕着消除哪些行跟哪些列,并使行+列=n,这样很容易就相当枚举其中一个,行不容易处理,反而列可以用状压dp进行处理。用1表示选取了当前列。接下来只要选取所在列下,对能每行能消除的个数进行排序,选取最多能消除的前几行。
- #include<iostream>
- #include<cstring>
- #include<cstdlib>
- #include<cstdio>
- #include<algorithm>
- #include<cmath>
- #include<queue>
- #include<map>
- #include<vector>
- #define N 25
- #define M 15
- #define mod 1000000007
- #define mod2 100000000
- #define ll long long
- #define maxi(a,b) (a)>(b)? (a) : (b)
- #define mini(a,b) (a)<(b)? (a) : (b)
- using namespace std;
- int n;
- char s[N][N];
- int have[N];
- int ma;
- int tot;
- int cou[N];
- int cc[(<<)+];
- int now;
- void ini1()
- {
- memset(cc,,sizeof(cc));
- for(int i=;i<(<<);i++)
- {
- int cnt=;
- for(int j=;j<;j++)
- if(i&(<<j))
- cnt++;
- cc[i]=cnt;
- }
- }
- void ini()
- {
- int i,j;
- ma=;
- memset(have,,sizeof(have));
- for(i=;i<;i++){
- scanf("%s",s[i]);
- }
- for( i=;i<;i++)
- for( j=;j<;j++)
- if(s[i][j]=='#')
- have[i]+=(<<j);
- }
- bool cmp(int i,int j)
- {
- return i>j;
- }
- void solve()
- {
- int o,i,j,temp;
- for(o=;o<tot;o++)
- {
- memset(cou,,sizeof(cou));
- if(cc[o]>n) continue;
- // printf(" o=%d now=%d\n",o,now);
- for(i=;i<;i++){
- cou[i]=cc[ o &have[i] ];
- }
- sort(cou,cou+,cmp);
- //for(j=0;j<20;j++) printf(" j=%d cou=%d")
- temp=;
- for(j=;j<n-cc[o] && j<;j++){ //注意这里j<20
- temp+=cou[j];
- }
- ma=max(ma,temp);
- }
- }
- int main()
- {
- // freopen("data.in","r",stdin);
- // freopen("data.out","w",stdout);
- //scanf("%d",&T);
- // for(int cnt=1;cnt<=T;cnt++)
- // while(T--)
- tot=<<;
- ini1();
- while(scanf("%d",&n)!=EOF)
- {
- if(n==) break;
- //printf(" %d\n",n);
- ini();
- solve();
- printf("%d\n",ma);
- //cout<<tot<<endl;
- }
- return ;
- }
ZOJ 3306 状压dp的更多相关文章
- Problem Arrangement ZOJ - 3777(状压dp + 期望)
ZOJ - 3777 就是一个入门状压dp期望 dp[i][j] 当前状态为i,分数为j时的情况数然后看代码 有注释 #include <iostream> #include <cs ...
- ZOJ - 3777(状压dp)
The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...
- Most Powerful(ZOJ 3471状压dp)
题意:n个原子,两两相撞其中一个消失,产生能量,给出任意两原子相撞能产生的能量,求能产生的最大能量. 分析:dp[i]表示情况为i时产生的最大能量 /*#include <map> #in ...
- Survival(ZOJ 2297状压dp)
题意:有n个怪,已知杀死第i个怪耗费的血和杀死怪恢复的血,和杀死boss耗的血,血量不能超过100,若过程中血小于0,则失败,问 是否能杀死boss(boss最后出现). 分析:就是求杀死n个怪后剩余 ...
- zoj 3812 状压dp
转载:http://blog.csdn.net/qian99/article/details/39138329 题意:给出n个物品,每个物品有两种属性Wi,Ti,有q组查询,每组查询要求在n个物品中选 ...
- Long Dominoes(ZOJ 2563状压dp)
题意:n*m方格用1*3的方格填充(不能重叠)求有多少种填充方法 分析:先想状态,但想来想去就是觉得不能覆盖所有情况,隔了一天,看看题解,原来要用三进制 0 表示横着放或竖放的最后一行,1表示竖放的中 ...
- Travel(HDU 4284状压dp)
题意:给n个城市m条路的网图,pp在城市1有一定的钱,想游览这n个城市(包括1),到达一个城市要一定的花费,可以在城市工作赚钱,但前提有工作证(得到有一定的花费),没工作证不能在该城市工作,但可以走, ...
- ZOJ 3723 (浙大月赛)状压DP
A了一整天~~~终于搞掉了. 真是血都A出来了. 题目意思很清楚,肯定是状压DP. 我们可以联系一下POJ 1185 炮兵阵地,经典的状压DP. 两道题的区别就在于,这道题的攻击是可以被X挡住的,而 ...
- ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds Me ...
随机推荐
- Scalatra
SBT和giter8 在你开始着手之前,你需要安装两个工具(我假设你已经安装了JDK1.6+).我将给你提供简缩的安装指令,详细版的安装指令可通过 下面的scalatra页面找到( http://ww ...
- select a.no,a.name,b.subid,b.subname,c.score
select a.no,a.name,b.subid,b.subname,c.score from a,b,c where a.no = c.no and b.subid = c.subid ;
- lg、ln的表示方法
c语言中 函数 log(x) 表示是以e为底的自然对数,即 ln(x) 函数 log10(x) 以10为底的对数,即 lg(x) 以其它数为底的对数用换底公式来表示 log(a)/log(b) 函数 ...
- 在Terminal中,如何打开Finder,并显示当前的目录
这是一个非常方便实用的小技巧,在Terminal中输入如下命令: $ open . 有图有真相: 参考: Open Finder in Current Folder from Terminal
- PAT (Basic Level) Practise (中文)-1037. 在霍格沃茨找零钱(20)
PAT (Basic Level) Practise (中文)-1037. 在霍格沃茨找零钱(20) http://www.patest.cn/contests/pat-b-practise/1037 ...
- RedHat7搭建KVM虚拟机
RedHat7搭建KVM虚拟机 1. 宿主机安装RedHat7.3系统 1.1选择语言 中文.简体中文(中国) 1.2安装位置 1.2.1自定义分区,选择LVM,将分区空间全部分配给根 1.2.2禁用 ...
- 如何用纯 CSS 创作一个永动的牛顿摆
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qKmGaJ 可交互视频教 ...
- Django 模版语法 三
使用自定义simple_tag 在 app01 下面创建 templatetags 文件夹,在创建 my_tag.py 文件,内容如下: from django import template fro ...
- 数据结构( Pyhon 语言描述 ) — —第11章:集和字典
使用集 集是没有特定顺序的项的一个集合,集中的项中唯一的 集上可以执行的操作 返回集中项的数目 测试集是否为空 向集中添加一项 从集中删除一项 测试给定的项是否在集中 获取两个集的并集 获取两个集的交 ...
- Django中模板查找路径配置