HDU3529-Bomberman - Just Search!

19 篇文章 0 订阅

Bomberman - Just Search!

                                                                      Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                                Total Submission(s): 766    Accepted Submission(s): 430


Problem Description
Bomberman has been a very popular game ever since it was released. As you can see above, the game is played in an N*M rectangular room. Bomberman can go around the room and place bombs. Bombs explode in 4 directions with radius r. To finish a stage, bomberman has to defeat all the foes with his bombs and find an exit behind one of the walls.
Since time is limited, bomberman has to do this job quite efficiently. Now he has successfully defeated all the foes, and is searching for the exit. It's really troublesome to destroy the walls one by one, so he's asking for your help to calculate the minimal number of bombs he has to place in order to destroy all the walls, thus he can surely find the exit.
 

Input
The input contains several cases. Each case begins with two integers: N and M(4 <= N, M <= 15). N lines follow, each contains M characters, describing the room. A '*' means a concrete wall which can never be destroyed, a '#' is an ordinary wall that can be destroyed by a single bomb, a '.' is an empty space where bombs can only be placed. There're at most 30 ordinary walls. The borders of the room is always surrounded by concrete walls, as you can see from the samples. You may assume that the explosion radius r is infinite, so that an explosion can reach as far as possible until it meets a wall and destroys it if it's an ordinary one. Proceed until the end of file.
 

Output
For each case, output the minimal number of bombs that should be placed to destroy all the ordinary walls. Note that two bombs can't be placed at the same position and all bombs explode simultaneously, which makes the result for the second sample to be 3 instead of 2. You may assume that there's always a solution.
 

Sample Input
  
  
9 11 *********** *#.#...#.#* *.*.*.*.*.* *.........* *.*.*.*.*.* *....#....* *.*.*.*.*.* *....#....* *********** 3 13 ************* *..##...##..* *************
 

Sample Output
  
  
3 3
 

Source
 


题意:给你一个n*m的地图,‘*’是无法摧毁的,在‘.’上放尽量少的炸弹摧毁所有的‘#’

解题思路:对‘.’和'#'分别进行标号,并处理出每个位置的‘.’能炸掉哪些'#',然后就是舞蹈链的重复覆盖


#include <iostream>    
#include <cstdio>    
#include <cstring>    
#include <string>    
#include <algorithm>    
#include <cctype>    
#include <map>    
#include <cmath>    
#include <set>    
#include <stack>    
#include <queue>    
#include <vector>    
#include <bitset>    
#include <functional>    

using namespace std;

#define LL long long    
const int INF = 0x3f3f3f3f;
const int maxn = 300005;

int n, m, x, y, tot1, tot2;
char ch[20][20];
pair<int, int>a[1009], b[1009];

struct DLX
{
	int L[maxn], R[maxn], U[maxn], D[maxn];
	int row[maxn], col[maxn], ans[maxn], sum[maxn];
	int n, m, num, cnt;
	int vis[maxn], flag[maxn];
	void add(int k, int l, int r, int u, int d, int x, int y)
	{
		L[k] = l;   R[k] = r;   U[k] = u;
		D[k] = d;   row[k] = x;  col[k] = y;
	}
	void reset(int n, int m)
	{
		this->n = n;   this->m = m;
		for (int i = 0; i <= m; i++)
		{
			add(i, i - 1, i + 1, i, i, 0, i);
			sum[i] = 0;
		}
		L[0] = m, R[m] = 0, cnt = m + 1;
	}
	void insert(int x, int y)
	{
		int temp = cnt - 1;
		if (row[temp] != x)
		{
			add(cnt, cnt, cnt, U[y], y, x, y);
			U[D[cnt]] = cnt; D[U[cnt]] = cnt;
		}
		else
		{
			add(cnt, temp, R[temp], U[y], y, x, y);
			R[L[cnt]] = cnt; L[R[cnt]] = cnt;
			U[D[cnt]] = cnt; D[U[cnt]] = cnt;
		}
		sum[y]++, cnt++;
	}
	void Remove(int k)
	{
		for (int i = D[k]; i != k; i = D[i])
		{
			L[R[i]] = L[i];
			R[L[i]] = R[i];
		}
	}
	void Resume(int k)
	{
		for (int i = U[k]; i != k; i = U[i]) L[R[i]] = R[L[i]] = i;
	}
	int A()
	{
		int dis = 0;
		for (int i = R[0]; i != 0; i = R[i]) vis[i] = 0;
		for (int i = R[0]; i != 0; i = R[i])
			if (!vis[i])
			{
				dis++, vis[i] = 1;
				for (int j = D[i]; j != i; j = D[j])
					for (int k = R[j]; k != j; k = R[k])
						vis[col[k]] = 1;
			}
		return dis;
	}
	bool Dfs(int k)
	{
		if (!R[0]) { num = min(num, k); return true; }
		else if (k + A() < num)
		{
			int now = R[0];
			for (int i = R[0]; i != 0; i = R[i])
				if (sum[now] > sum[i]) now = i;
			for (int i = D[now]; i != now; i = D[i])
			{
				Remove(i);
				for (int j = R[i]; j != i; j = R[j]) Remove(j);
				if (Dfs(k + 1)) return true;
				for (int j = L[i]; j != i; j = L[j]) Resume(j);
				Resume(i);
			}
		}
		return false;
	}
	void mul()
	{
		memset(flag, 0, sizeof flag);
		num = 0x7FFFFFFF;
	}
}dlx;

int check(int x, int y)
{
	if (a[x].first == b[y].first)
	{
		for (int i = min(a[x].second, b[y].second) + 1; i < max(a[x].second, b[y].second); i++)
			if (ch[a[x].first][i] != '.') return 0;
		return 1;
	}
	if (a[x].second == b[y].second)
	{
		for (int i = min(a[x].first, b[y].first) + 1; i < max(a[x].first, b[y].first); i++)
			if (ch[i][a[x].second] != '.') return 0;
		return 1;
	}
	return 0;
}

int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		tot1 = tot2 = 0;
		for (int i = 1; i <= n; i++)
		{
			scanf("%s", ch[i] + 1);
			for (int j = 1; j <= m; j++)
			{
				if (ch[i][j] == '.') a[++tot1] = make_pair(i, j);
				if (ch[i][j] == '#') b[++tot2] = make_pair(i, j);
			}
		}
		dlx.reset(tot1, tot2);
		for (int i = 1; i <= tot1; i++)
			for (int j = 1; j <= tot2; j++)
				if (check(i, j)) dlx.insert(i, j);
		dlx.mul();
		dlx.Dfs(0);
		printf("%d\n", dlx.num);
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值