天天看點

HDU 3529 Bomberman - Just Search!

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.

HDU 3529 Bomberman - Just Search!

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

#include<cstdio>
#include<vector>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 1005;
int n, T, m;
struct point
{
    int x,y;
    point(){};
    point(int x,int y):x(x),y(y){}
}a[maxn],b[maxn];

char s[25][25];
int tot1,tot2;

inline void read(int &ret)
{
    char c;
    do {
        c = getchar();
    } while (c < '0' || c > '9');
    ret = c - '0';
    while ((c = getchar()) >= '0' && c <= '9')
        ret = ret * 10 + (c - '0');
}

struct DLX
{
    #define maxn 10005
    #define F(i,A,s) for (int i=A[s];i!=s;i=A[i])
    int L[maxn], R[maxn], U[maxn], D[maxn];
    int row[maxn], col[maxn], ans[maxn], cnt[maxn];
    int n, m, num, sz;

    bool Flag;
    void add(int now, int l, int r, int u, int d, int x, int y)
    {
        L[now] = l;    R[now] = r;    U[now] = u;
        D[now] = d;   row[now] = x;  col[now] = y;
    }
    void reset(int n, int m)
    {
        Flag = false;
        this->n = n;    this->m = m;
        for (int i = 0; i <= m; i++)
        {
            add(i, i - 1, i + 1, i, i, 0, i);
            cnt[i] = 0;
        }
        L[0] = m;     R[m] = 0;     sz = m + 1;
    }
    void insert(int x, int y)
    {
        int ft = sz - 1;
        if (row[ft] != x)
        {
            add(sz, sz, sz, U[y], y, x, y);
            U[D[sz]] = sz; D[U[sz]] = sz;
        }
        else
        {
            add(sz, ft, R[ft], U[y], y, x, y);
            R[L[sz]] = sz; L[R[sz]] = sz;
            U[D[sz]] = sz; D[U[sz]] = sz;
        }
        ++cnt[y];    ++sz;
    }

    //精确覆寫
    void remove(int now)
    {
        R[L[now]] = R[now];
        L[R[now]] = L[now];
        F(i,D,now) F(j,R,i)
        {
            D[U[j]] = D[j];
            U[D[j]] = U[j];
            --cnt[col[j]];
        }
    }
    void resume(int now)
    {
        F(i,U,now)    F(j,L,i)
        {
            D[U[j]] = j;
            U[D[j]] = j;
            ++cnt[col[j]];
        }
        R[L[now]] = now;
        L[R[now]] = now;
    }
    int dfs(int x)
    {
        if (!R[0]) return 1;
        int now = R[0];
        F(i,R,0) if (cnt[now]>cnt[i]) now = i;
        remove(now);
        F(i,D,now)
        {
            ans[x] = row[i];
            F(j,R,i) remove(col[j]);
            if (dfs(x + 1)) return 1;
            F(j,L,i) resume(col[j]);
        }
        resume(now);
        return 0;
    }

    //精确覆寫
    //重複覆寫
    void Remove(int now)
    {
        F(i,D,now) 
        {
            L[R[i]]=L[i];
            R[L[i]]=R[i];
        }
    }
    void Resume(int now)
    {
        F(i,U,now) L[R[i]]=R[L[i]]=i;
    }
    int vis[maxn];
    int flag[maxn];
    int A()
    {
        int dis=0;
        F(i,R,0) vis[i]=0;
        F(i,R,0) if (!vis[i])
        {
            dis++;    vis[i]=1;
            F(j,D,i) F(k,R,j) vis[col[k]]=1;
        }
        return dis;
    }
    void Dfs(int x)
    {
        if (!R[0]) num=min(num,x);
        else if (x+A()<num)
        {
            int now=R[0];
            F(i,R,0) if (cnt[now]>cnt[i]) now = i;
            F(i,D,now)
            {
                Remove(i);F(j,R,i) Remove(j);
                Dfs(x+1);
                F(j,L,i) Resume(j);Resume(i);
            }
        }
    }
    void mul()
    {
        num=0x7FFFFFFF;
    }
    //重複覆寫
}dlx;

bool check(point &a,point &b)
{
    if (a.x==b.x)
    {
        for (int i=min(a.y,b.y)+1;i<max(a.y,b.y);++i)
            if (s[a.x][i]!='.') return false;
        return true;
    }
    if (a.y==b.y)
    {
        for (int i=min(a.x,b.x)+1;i<max(a.x,b.x);++i)
            if (s[i][a.y]!='.') return false;
        return true;
    }
    return false;
}

void reset()
{
    tot1=tot2=0;
    for (int i=1;i<=n;++i)
    {
        scanf("%s",s[i]+1);
        for (int j=1;j<=m;++j)
        {
            if (s[i][j]=='.') a[++tot1]=point(i,j);
            if (s[i][j]=='#') b[++tot2]=point(i,j);
        }
    }
    dlx.reset(tot1,tot2);
    for (int i=1;i<=tot1;++i)
        for (int j=1;j<=tot2;++j)
            if (check(a[i],b[j])) dlx.insert(i,j);
}

int main()
{
    //read(T);
    while (~scanf("%d%d",&n,&m))
    {
        reset();
        dlx.mul();
        dlx.Dfs(0);
        printf("%d\n",dlx.num);
    }
    return 0;
}