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
output
input
output
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;
}
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;
}
相关文章推荐
- 2010-2011 ACM-ICPC, NEERC, Southern Subregional Contest Fire in the Country(博弈论+搜索)
- 2010-2011 ACM-ICPC, NEERC, Southern Subregional Contest
- 2010-2011 ACM-ICPC, NEERC, Southern Subregional Contest【solved :8 / 12 】
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem K. KMC Attacks 交互题 暴力
- 2009-2010 ACM-ICPC, NEERC, Southern Subregional Contest B kakuro
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem A. Alien Visit 计算几何
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem C. Contest 水题
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem D. Distance 迪杰斯特拉
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem F. Finance 模拟题
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem H. Hometask 水题
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem I. Interest Targeting 模拟题
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem J. Joke 水题
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest A. Toda 2
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest F - Infinite Go
- 2007-2008 ACM-ICPC, NEERC, Southern Subregional Contest J - Numbers Painting
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem C. Equivalent Cards 计算几何
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest K. Road Widening
- codeforces883I 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rule
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest G. Car Repair Shop(优先队列)