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

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 (ij) reaches the ground, it destroys all rocks in row i and also all rocks in column j of the grid. If cell (ij) 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:炸的行数,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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值