[codeforces] Gym
来源:互联网 发布:美国非农数据统计 编辑:程序博客网 时间:2024/05/17 23:07
[codeforces] Gym - 101246C Explode ‘Em All (状压+DFS + lowbit)
题目链接: C. Explode ‘Em All
题目大意:
给一个
数据范围:
解题思路:
刚开始看这道题都会想到完美匹配, 结果发现做不了, 后来想到了dfs搜索, 但是复杂度始终优化不下来, 比赛结束之后看了学长的代码才发现状压的神奇。
对于每一行我们都可以把这一行压缩成一个数, 这个数转化成二进制之后1就代表有石头。 比如:
转化成:
那么我们可以dfs,两种状态, 当前行要么放, 要么不放。
对于每一行的状态, 如果不放雷, 最终结果就(|)或一下当前行状态。 最后看一下最终状态里有几个1就是还有几列需要炸弹, 和行数放的雷数取个
那么如何快速的求出最终状态有几个1呢, 可以用lowbit预处理出来。
/********************************************** *Author* :ZZZZone *reated Time* : 2017/8/9 14:23:34 *ended Time* : 2017/8/9 14:33:23*********************************************/#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#include <stack>using namespace std;typedef pair<int, int> PII;typedef long long LL;typedef unsigned long long ULL;const int MaxN = 4e7;char a[30][30];int boom[30], f[MaxN + 5];int n, m, ans;void dfs(int dis, int cnt, int p){ if(dis > n){ int tmp = f[p]; ans = min(ans, max(cnt, tmp)); return; } dfs(dis + 1, cnt, p | boom[dis]); dfs(dis + 1, cnt + 1, p);}int main(){ //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); for(int i = 1; i <= (1 << 25); i++) f[i] = f[i ^ (i & (-i))] + 1; while(~scanf("%d %d", &n, &m)){ ans = n * m; for(int i = 1; i <= n; i++) scanf("%s", a[i] + 1); for(int i = 1; i <= n; i++){ int sum = 0, x = 1; for(int j = m; j >= 1; j--){ if(a[i][j] == '*') sum += x; x *= 2; } boom[i] = sum; } dfs(1, 0, 0); printf("%d\n", ans); } return 0;}
阅读全文
0 0
- 【Codeforces Gym
- 【codeforces Gym
- codeforces Gym
- codeforces Gym
- codeforces Gym
- codeforces Gym
- Codeforces Gym
- Codeforces Gym
- Codeforces Gym
- codeforces Gym
- Codeforces Gym
- Codeforces Gym
- Codeforces Gym
- [codeforces] Gym
- [codeforces] Gym
- [codeforces] Gym
- [codeforces] Gym
- [codeforces] Gym
- Spring MVC 配置报错: Error creating bean with name 'userController': Injection of resource dependencies
- 知道这20个正则表达式,能让你少写1,000行代码
- linux实验楼学习笔记4
- 《numpy学习指南》学习笔记——数组
- Creating and using Clang plugin with Xcode
- [codeforces] Gym
- HDU1864最大报销额 ——01背包
- 二叉树详解
- 动态规划--多段图的最短路--单向TSp(Unidirectional TSP ,UVa 116)
- C++中值传递、指针传递、引用传递的总结
- 一、aapt的用法(上)
- 不相交集(并查集)
- 2017/8/7 第十五天
- WebGIS主流瓦片切片资源