hdu 3529 舞蹈链之可重复覆盖

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

题意:如图中所示,‘#’是可摧毁的墙,‘*’不可摧毁的墙,‘.’为空地,在空地上放炸弹可以炸毁同行同列的墙,但是不可摧毁的墙是炸不动的,问;在最开始的局面上最少同时放几个炸弹可以把所有#炸毁

思路:典型的舞蹈链可重复覆盖问题,以#为列(需要标序号),以能够炸毁墙的空地为行,然后就是舞蹈链模板喽

兴奋啊,这是独立完成的第一道舞蹈链的问题,纪念一下

ac代码:

#include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; const int inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f3fll; const int maxn=260; int L[maxn*maxn],R[maxn*maxn],U[maxn*maxn],D[maxn*maxn]; int C[maxn*maxn],H[maxn],cnt[maxn],vis[maxn]; int n,m,id,fans; void init(int ll) {     for(int i=0; i<=ll; i++)     {         cnt[i]=0;         U[i]=D[i]=i;         L[i+1]=i;         R[i]=i+1;     }     R[ll]=0;     id=ll+1;     memset(H,-1,sizeof(H)); } void Link(int r,int c) {     cnt[c]++;     C[id]=c;     U[id]=U[c];     D[U[c]]=id;     D[id]=c;     U[c]=id;     if(H[r]==-1) H[r]=L[id]=R[id]=id;     else     {         L[id]=L[H[r]];         R[L[H[r]]]=id;         R[id]=H[r];         L[H[r]]=id;     }     id++; } void Remove(int Size) {     for(int j=D[Size]; j!=Size; j=D[j])         L[R[j]]=L[j],R[L[j]]=R[j]; } void Resume(int Size) {     for(int j=D[Size]; j!=Size; j=D[j])         L[R[j]]=R[L[j]]=j; } int h() {     int sum=0;     memset(vis,0,sizeof(vis));     for(int i=R[0]; i; i=R[i])     {         if(vis[i]) continue;         sum++;         for(int j=D[i]; j!=i; j=D[j])         {             for(int k=R[j]; k!=j; k=R[k])                 vis[C[k]]=1;         }     }     return sum; } void Dance(int k) {     int mm=maxn,pos;     if(k+h()>=fans) return;     if(!R[0])     {         if(k<fans) fans=k;         return;     }     for(int i=R[0]; i; i=R[i])         if(mm>cnt[i]) mm=cnt[i],pos=i;     for(int i=D[pos]; i!=pos; i=D[i])     {         Remove(i);         for(int j=R[i]; j!=i; j=R[j]) Remove(j);         Dance(k+1);         for(int j=R[i]; j!=i; j=R[j]) Resume(j);         Resume(i);     } } int B[20][20]; char A[20][20]; int main() {     int u,v,len,wid;     while(scanf("%d%d",&n,&m)!=-1)     {         getchar();       for(int i=0;i<n;i++)       gets(A[i]);       int kk=0;       for(int i=0;i<n;i++)       {           for(int j=0;j<m;j++)           {               if(A[i][j]=='#')               B[i][j]=(++kk);           }       }       init(kk);       int kkk=1;       for(int i=0;i<n;i++)       {           for(int j=0;j<m;j++)           {               if(A[i][j]=='.')               {                   for(int k=i;k<n;k++)                   {                       if(A[k][j]=='#')                       {                           Link(kkk,B[k][j]);                           break;                       }                       if(A[k][j]=='*')                       break;                   }                   for(int k=i;k>=0;k--)                   {                       if(A[k][j]=='#')                       {                           Link(kkk,B[k][j]);                           break;                       }                       if(A[k][j]=='*')                       break;                   }                   for(int k=j;k<m;k++)                   {                       if(A[i][k]=='#')                       {                           Link(kkk,B[i][k]);                           break;                       }                       if(A[i][k]=='*')                       break;                   }                   for(int k=j;k>=0;k--)                   {                       if(A[i][k]=='#')                       {                           Link(kkk,B[i][k]);                           break;                       }                       if(A[i][k]=='*')                       break;                   }               }               kkk++;           }       }     fans=kkk-1;     Dance(0);     printf("%d\n",fans); } return 0; }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值