您的位置:首页 > 其它

C. Explode 'Em All 搜索 2010-2011 ACM-ICPC, NEERC, Southern Subregional Contest

2017-07-18 10:50 501 查看
C. Explode 'Em All

time limit per test
1 second

memory limit per test
512 megabytes

input
input.txt

output
output.txt

The prime minister of Berland decided to build a new city in the country. It's hard to describe the excitement of all Berland citizens, but indeed this is great news from the economic, social and cultural standpoints.

The land in Berland is occupied almost entirely and it's very hard to find free space for construction, so it was decided to build the city on a stony terrain. The map of this terrain is represented as an n × m grid,
where each cell of the grid is either an empty space or a rock.

Of course, before construction is started, the given terrain must be completely cleared from rocks. As you may guess, you were hired to complete this mission. Your goal is to destroy all rocks by dropping bombs from a plane. A bomb can be dropped on any cell
of the map, and you are free to select where you want to drop each bomb. When a bomb targeted for cell (i, j)
reaches the ground, it destroys all rocks in row i and also all rocks in column j of
the grid. If cell (i, j)
contains a rock, this rock is also destroyed.

Please help the prime minister of Berland to find the minimum number of bombs required to completely clear the given terrain from rocks.

Input

The first line of input contains two integers n and m (1 ≤ n, m ≤ 25) —
the number of rows and columns correspondingly. Each of the next n lines contains m characters
describing the terrain. An empty space is denoted by ".", while a rock is denoted by "*".

Output

Write a single integer to the output — the minimum numbers of bombs required for destroying all rocks on the terrain.

Examples

input
8 10
..........
..***..*.*
.*.......*
.*.......*
.*.......*
.....*****
..........
.........*


output
2


input
3 4
....
....
....


output
0


Note

In the first sample test it's only required to drop 2 bombs from a plane: one bomb to cell (2,2) and another bomb to cell (6, 10). Row and column indices in this explanation are 1-based.
http://codeforces.com/gym/101246/problem/C
一直以为是最小点覆盖,思考了很长时间如何建图,最后发现原来是搜索。

直接按每一行炸或不炸dfs,再加个小剪枝,216ms AC。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <bitset>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=35,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
char s[maxn];
int mp[maxn],a[(1<<25)],dp[(1<<25)];
int n,m,ans;

int lowbit(int x) {
return x&(-x);
}

int init(int x) {
if (dp[x]) return dp[x]; //减少时间复杂度
int j=x;
while (j) {
dp[x]++;
j-=lowbit(j);
}
return dp[x];
}

void dfs(int x,int k,int state) { //x:行数,k:炸的
ee52
行数,state:当前所有列状态
if (x==n+1) {
ans=min(ans,max(init(state),k));
return;
}
if (mp[x]!=0) dfs(x+1,k+1,state); //剪枝,若某行不需要炸则不炸这一行
dfs(x+1,k,state|mp[x]);
}

int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int i,j;
scanf("%d%d",&n,&m);
mem0(mp);
mem0(dp);
for (i=1;i<=n;i++) {
scanf("%s",s+1);
for (j=1;j<=m;j++) {
if (s[j]=='*') mp[i]+=(1<<(j-1));
}
}
ans=min(n,m);
dfs(1,0,0);
cout << ans;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐