HDU 3529 Bomberman

来源:互联网 发布:联合网络图书采访 编辑:程序博客网 时间:2024/06/02 02:34

题目地址
题意:就是和泡泡堂游戏一样,你这个游戏就是要把所有可以炸的墙全部炸掉,问你最少要用多少个炸弹。(‘#’为可以炸掉的墙,‘.’为可以放炸弹的地方,‘*’是不可破坏的墙,整个地图的四周全部是不可破坏的墙)
思路:我们可以创建一个这样的矩阵,每个行是可以放置炸弹的每个位置,每一列为每个可以炸的墙的编号。然后求一边DLX重复覆盖就好了,这样就能求出最少要用多少个炸弹就可以把所有可以炸的墙全部炸掉。

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <cmath>#include <cstdio>#include <algorithm>#include <iomanip>#define N 1010#define M 110#define MAXSIZE 1010*1010#define LL __int64#define inf 0x3f3f3f3f#define lson l,mid,ans<<1#define rson mid+1,r,ans<<1|1#define getMid (l+r)>>1#define movel ans<<1#define mover ans<<1|1using namespace std;const LL mod = 1000000007;const double eps = 0.001;struct node {    int left, right, up, down, col, row;}mapp[MAXSIZE];int S[N], H[N];//S记录该列中1元素的个数int head, cnt;int len;struct Dancing_Links_X {    void init(int m) {        head = 0;        for (int i = 0; i <= m; i++) {            S[i] = 0;            mapp[i].up = mapp[i].down = i;            mapp[i].left = (i == 0 ? m : i - 1);            mapp[i].right = (i == m ? 0 : i + 1);        }        cnt = m;        memset(H, -1, sizeof(H));    }    void link(int x, int y) {        cnt++;        mapp[cnt].row = x;        mapp[cnt].col = y;        S[y]++;        mapp[cnt].up = mapp[y].up;        mapp[cnt].down = y;        mapp[mapp[y].up].down = cnt;        mapp[y].up = cnt;        if (H[x] == -1) H[x] = mapp[cnt].left = mapp[cnt].right = cnt;        else {            mapp[cnt].left = mapp[H[x]].left;            mapp[cnt].right = H[x];            mapp[mapp[H[x]].left].right = cnt;            mapp[H[x]].left = cnt;        }    }    void remove(int c) {//删去c这个点,以及关联的一列        for (int i = mapp[c].down; i != c; i = mapp[i].down) {            mapp[mapp[i].left].right = mapp[i].right;            mapp[mapp[i].right].left = mapp[i].left;        }    }    void resume(int c) {//恢复c这个点,以及关联的一列        for (int i = mapp[c].up; i != c; i = mapp[i].up) {            mapp[mapp[i].left].right = i;            mapp[mapp[i].right].left = i;        }    }    bool used[N];    int h() {        int sum = 0;        for (int i = mapp[head].right; i != head; i = mapp[i].right) used[i] = false;        for (int i = mapp[head].right; i != head; i = mapp[i].right) {            if (!used[i]) {                sum++;                used[i] = true;                for (int j = mapp[i].down; j != i; j = mapp[j].down)                    for (int k = mapp[j].right; k != j; k = mapp[k].right)                         used[mapp[k].col] = true;            }        }        return sum;    }    void dance(int nums) {        if (nums + h() >= len) return;        if (mapp[head].right == head) {            len = min(len, nums);            return;        }        int s = inf, c;        for (int t = mapp[head].right; t != head; t = mapp[t].right) {            if (S[t] < s) s = S[t], c = t;        }        for (int i = mapp[c].down; i != c; i = mapp[i].down) {            remove(i);            for (int j = mapp[i].right; j != i; j = mapp[j].right) {                remove(j);            }            dance(nums + 1);            for (int j = mapp[i].left; j != i; j = mapp[j].left) {                resume(j);            }            resume(i);        }        return;    }}DLX;char map1[20][20];int num[20][20];int main() {    int n, m, t;    int a;    while (~scanf("%d %d", &n, &m)) {        int col = 1, row = 1;        for (int i = 0; i < n; i++) {            scanf("%s", map1[i]);        }        for (int i = 0; i < n; i++) {            for (int j = 0; j < m; j++) {                if (map1[i][j] == '#') num[i][j] = col, col++;            }        }        DLX.init(col - 1);//特别提醒,这个点查了一天了        for (int i = 0; i < n; i++) {            for (int j = 0; j < m; j++) {                if (map1[i][j] == '.') {                    int x, y;                    x = i;                    y = j;                    while (x > 1) {                        if (map1[x - 1][y] == '*') break;                        if (map1[x - 1][y] == '#') {                            DLX.link(row, num[x - 1][y]);                            break;                        }                        x--;                    }                    x = i;                    y = j;                    while (x < n - 1) {                        if (map1[x + 1][y] == '*') break;                        if (map1[x + 1][y] == '#') {                            DLX.link(row, num[x + 1][y]);                            break;                        }                        x++;                    }                    x = i;                    y = j;                    while (y > 1) {                        if (map1[x][y - 1] == '*') break;                        if (map1[x][y - 1] == '#') {                            DLX.link(row, num[x][y - 1]);                            break;                        }                        y--;                    }                    x = i;                    y = j;                    while (y < m - 1) {                        if (map1[x][y + 1] == '*') break;                        if (map1[x][y + 1] == '#') {                            DLX.link(row, num[x][y + 1]);                            break;                        }                        y++;                    }                    row++;                }            }        }        len = row - 1;        DLX.dance(0);        printf("%d\n", len);    }    return 0;}
原创粉丝点击