HDU3529 Bomberman - Just Search!重复覆盖DLX

 

Bomberman - Just Search!

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


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.
HDU3529 Bomberman - Just Search!重复覆盖DLX_第1张图片

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
2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU

Recommend
zhengfeng
 
重复覆盖模型

问题描述:选定最少的行,使得每列至少有一个1(即不一定为一个)

实现思路:将当前列去掉,并将选作当前列的解的行能够覆盖到的列全部去掉,因为不要求每列仅有一个1,故不必要把能够覆盖某一列的所有行全部去掉。remove和resume和精确覆盖有所不同。需要注意。

此外,由于删去的少了,所以矩阵密度的下降也变慢了,因此要加个A*剪枝来提高效率。就是利用A*搜索中的估价函数,即对于当前的递归深度K下的矩阵,估计其最好情况下即最少还需要多少步才能出解。即如果将能够覆盖当前列的所有行全部选中,去掉这些行能够覆盖到的列,将这个操作作为一层深度,重复次操作直到所有列全部出解的深度是多少。如果当前深度加上最佳步数已经不可能由于当前最优解,则直接返回不必再算这种情况。感觉和分支限界很像,其实以前也不自觉地经常用。

//转载自 http://blog.sina.com.cn/s/blog_6a46cc3f0100s2d4.html
 
 
 ------------------------------------------------------------------------------------------------------------------------------------------
题意是:在空地上放一个炸弹可以把四个方向扩展遇到的第一个普通墙炸掉,现在求放置最少的炸弹炸掉所有的普通墙,基础的重复覆盖模型。
用空地作为行,普通墙作为列。
 
代码:
#include<cstdio>
#define N 205
#define M 205

int m,n,cnt,size[M],ans,dir[4][2]={0,1,0,-1,1,0,-1,0};
struct Node
{
    int r,c;
    Node *U,*D,*L,*R;
}node[505],row[N],col[M],head;
void init(int r,int c)
{
    cnt=0;
    head.r=r;head.c=c;
    head.L=head.R=head.U=head.D=&head;
    for(int i=0;i<c;i++)
	{
        col[i].r=r;col[i].c=i;
        col[i].L=&head;col[i].R=head.R;
        col[i].U=col[i].D=col[i].L->R=col[i].R->L=&col[i];
        size[i]=0;
    }
    for(int i=r-1;i>=0;i--)
	{
        row[i].r=i;row[i].c=c;
        row[i].U=&head;row[i].D=head.D;
        row[i].L=row[i].R=row[i].U->D=row[i].D->U=&row[i];
    }
}
void insert(int r,int c)
{
    Node *p=&node[cnt++];
    p->r=r;p->c=c;
    p->R=&row[r];p->L=row[r].L;
    p->L->R=p->R->L=p;
    p->U=&col[c];p->D=col[c].D;
    p->U->D=p->D->U=p;
    size[c]++;
}
inline void cover(Node *p)
{
    for(Node *C=p->D;C!=p;C=C->D)
	{
		size[C->c]--;
		C->L->R=C->R;
		C->R->L=C->L;
	}
}
inline void resume(Node *p)
{
    for(Node *C=p->U;C!=p;C=C->U)
	{
		size[C->c]++;
		C->L->R=C->R->L=C;
	}
}
int h()
{
	bool f[M]={0};
	int k=0;
	Node *p,*R,*C;
	for(p=head.L;p!=&head;p=p->L)
		if(!f[p->c])
		{
			k++;
			f[p->c]=1;
			for(C=p->U;C!=p;C=C->U)
				for(R=C->R;R!=C;R=R->R)
					f[R->c]=1;
		}
	return k;
}
void dfs(int k)
{
	if(k+h()>=ans)
		return;
    if(head.L==&head)
	{
		if(k<ans)
			ans=k;
		return;
	}
    int INF=-1u>>1,c=-1;
	Node *p,*q;
    for(p=head.L;p!=&head;p=p->L)
        if(size[p->c]<INF)
            INF=size[c=p->c];
	if(!INF)
		return;
    for(p=col[c].D;p!=&col[c];p=p->D)
	{
		cover(p);
        for(q=p->L;q!=p;q=q->L)
            cover(q);
        dfs(k+1);
        for(q=p->R;q!=p;q=q->R)
            resume(q);
		resume(p);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
	{
		int i,j,k,x,y,n1=0,n2=0,h1[255]={0},h2[255]={0};
		char map[25][25];
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='.')
					h1[i*m+j]=n1++;
				if(map[i][j]=='#')
					h2[i*m+j]=n2++;
			}
		}
		init(n1,n2);
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				if(map[i][j]=='.')
					for(k=0;k<4;k++)
					{
						x=i+dir[k][0];
						y=j+dir[k][1];
						while(map[x][y]=='.')
						{
							x+=dir[k][0];
							y+=dir[k][1];
						}
						if(map[x][y]=='#')
							insert(h1[i*m+j],h2[x*m+y]);
					}
		for(i=0;i<n1;i++)
		{
			row[i].R->L=row[i].L;
			row[i].L->R=row[i].R;
		}
		ans=999;
		dfs(0);
		printf("%d\n",ans);
	}
}

 

你可能感兴趣的:(c,search,insert,each,output,Training)