重复点覆盖的题, 建好图就可以直接A 了。。。

Bomberman - Just Search!

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

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

Recommend
zhengfeng
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 100000
#define INF 0x3ffffff

int n,m;
int U[N],D[N],R[N],L[N],num[N],H[N],col[N],line[N];
int nn,mm;
char g[20][20];
int tg[20][20];
int head,id,mi;
int up[4]={1,-1,0,0};
int rl[4]={0,0,1,-1};

void prepare()
{
    for(int i=0;i<=mm;i++)
    {
        num[i]=0;
        D[i]=i;
        U[i]=i;
        R[i]=i+1;
        L[i+1]=i;
    }
    R[mm]=0;
    L[0]=mm;
    memset(H,-1,sizeof(H));
}

void link(int tn,int tm)
{
    id++;
    num[ line[id]=tm ]++;
    col[id]=tn;

    U[D[tm]]=id;
    D[id]=D[tm];
    D[tm]=id;
    U[id]=tm;
    if(H[tn]<0) H[tn]=R[id]=L[id]=id;
    else
    {
        L[R[H[tn]]]=id;
        R[id]=R[H[tn]];
        L[id]=H[tn];
        R[H[tn]]=id;
    }
}
void build()
{
    id=mm;
    prepare();
    for(int i=1;i<n-1;i++)
        for(int j=1;j<m-1;j++)
        {
            int flag=0;
            if(g[i][j]=='.')
            {
                for(int i1=0;i1<4;i1++)
                {
                    int ti=i,tj=j;
                    ti+=up[i1];
                    tj+=rl[i1];
                    while( g[ti][tj]!='*' )
                    {
                        if(g[ti][tj]=='#')
                        {
                            flag=1;
                            link(nn,tg[ti][tj]);
                            break;
                        }
                        ti+=up[i1];
                        tj+=rl[i1];
                    }
                }
            }
            if(flag==1) nn++;
        }
}

void remove(int s)
{
    for(int i=D[s];i!=s;i=D[i])
    {
        R[L[i]]=R[i];
        L[R[i]]=L[i];
    }
}

void resume(int s)
{
    for(int i=D[s];i!=s;i=D[i])
        R[L[i]]=L[R[i]]=i;
}

int h()
{
    int mark[33];
    memset(mark,0,sizeof(mark));
    int sum=0;
    for(int i=R[head];i!=head;i=R[i])
    {
        if(mark[i]==0)
        {
            sum++;
            mark[i]=1;
            for(int j=D[i];j!=i;j=D[j])
                for(int k=R[j];k!=j;k=R[k])
                    mark[line[k]]=1;
        }
    }
    return sum;
}
void dfs(int s)
{
    if(s+h()>=mi) return ;
    if(R[head]==head)
    {
        mi=s;
        return ;
    }
    int tmi=INF,tu;
    for(int i=R[head];i!=head;i=R[i])
        if(num[i]<tmi)
        {
            tmi=num[i];
            tu=i;
        }
        for(int i=D[tu];i!=tu;i=D[i])
        {
            remove(i);
            for(int j=R[i];j!=i;j=R[j])
            {
                remove(j);
            }
            dfs(s+1);
            for(int j=L[i];j!=i;j=L[j])
                resume(j);
            resume(i);
        }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        head=0;
        for(int i=0;i<n;i++)
            scanf("%s",g[i]);
        nn=1; mm=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(g[i][j]=='#')
                {
                    mm++;
                    tg[i][j]=mm;
                }
            }
        build();
        mi=INF;
        dfs(0);
        printf("%d\n",mi);
    }
    return 0;
}

优质内容筛选与推荐>>
1、[团购]《庖丁解牛:纵向切入ASP.NET 3.5控件和组件开发技术》团购活动~ ;")
2、Python time 模块
3、(十四)分页展示商品
4、Hadoop发展历史简介
5、C# 部分类: partial关键字的作用(转摘)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn