首页 > ACM题库 > HDU-杭电 > HDU 3529-Bomberman – Just Search![解题报告]HOJ
2014
11-05

HDU 3529-Bomberman – Just Search![解题报告]HOJ

Bomberman – Just Search!

问题描述 :

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.
The Simple Programming Language

输入:

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.

输出:

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.

样例输入:

9 11
***********
*#.#...#.#*
*.*.*.*.*.*
*.........*
*.*.*.*.*.*
*....#....*
*.*.*.*.*.*
*....#....*
***********
3 13
*************
*..##...##..*
*************

样例输出:

3
3

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> 
using namespace std;
const int inf = (-1u>>1);
#define N 15*15 
#define M N
struct node{
	int r,c;
	node *L,*R,*U,*D;
};
node DD[N*4+5], row[N + 5], col[M+5], head;
int cnt, size[M+5];
int n, m;
char s[20][20]; 
int tag[20][20]; 
int ans; 
int rcnt, ccnt; 

inline void init(int r, int c){
 cnt = 0;
 head.L = head.R = head.U = head.D = &head;
 for (int i = c - 1; i >= 0; i--){
 col[i].c = i;
 col[i].r = r;
 col[i].L = &head;
 col[i].R = head.R;
 col[i].L->R = col[i].R->L = &col[i];
 col[i].U = col[i].D = &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].U->D = row[i].D->U = &row[i];
 row[i].L = row[i].R = &row[i]; 
 }
}

inline void delLR(node *p){
 p->L->R = p->R;
 p->R->L = p->L;
}
inline void delUD(node *p){
 p->U->D = p->D;
 p->D->U = p->U;
} 
inline void recLR(node *p){
 p->L->R = p->R->L = p;
}
inline void recUD(node *p){
 p->U->D = p->D->U = p;
} 
inline void add(int r, int c){
 node *p = &DD[cnt++];
 p->c = c;
 p->r = r;
 p->U = &col[c];
 p->D = col[c].D;
 p->U->D = p->D->U = p;
 p->L = &row[r];
 p->R = row[r].R;
 p->L->R = p->R->L = p;
 size[c]++;
} 

 


void cover(node *p){
 if (p->c == ccnt)
 return;
 node *q;
 for (q = p->D; q != p; q = q->D){
 delLR(q); 
 }
} 

void resume(node *p){
 if (p->c == ccnt)
 return;
 node *q;
 for (q = p->D; q != p; q = q->D){
 recLR(q); 
 }
}

int h(){
 bool vis[N] = {false};
 node *p, *q, *pi;
 int nu = 0; 
 for (p = head.R; p != &head; p = p->R){
 if (!vis[p->c]){
 vis[p->c] = true;
 nu++;
 for (q = col[p->c].D; q != &col[p->c]; q = q->D)
 for (pi = q->R; pi != q; pi = pi->R)
 vis[pi->c] = true;
 }
 } 
 return nu;
} 
 
void DLX(int k){
 node *p;
 if (head.L == (&head)){
 ans = k; 
 return;
 }
 if (k + h() >= ans)return; 
 
 int MIN = inf, c = 1;
 for (p = head.R; p != (&head); p = p->R){
 if (size[p->c] < MIN){
 MIN = size[p->c];
 c = p->c;
 }
 }
 
 for (p = col[c].D; p != (&col[c]); p = p->D){
 node *q;
 cover(p); 
 for (q = p->L; q != p; q = q->L){
 cover(q);
 }
 DLX(k + 1); 
 for (q = p->R; q != p; q = q->R){ 
 resume(q);
 } 
 resume(p); 
 }
}


 
int main(){
 //freopen("in.txt", "r", stdin);
 // freopen("out.txt", "w", stdout); 
 while (scanf("%d %d", &n, &m) == 2){
 rcnt = 0, ccnt = 0;
 for (int i = 0; i < n; i++){
 scanf("%s", s[i]);
 for (int j = 1; s[i][j]; j++){
 if (s[i][j] == '#')
 tag[i][j] = ccnt++;
 } 
 }
 init((n - 2 )*(m - 2), ccnt); 
 for (int i = 1; i < n - 1; i++){
 for (int j = 1; j < m - 1; j++){
 int x, y; 
 if (s[i][j] == '.'){
 x = i, y = j - 1;
 while (s[x][y] == '.')y--;
 if (s[x][y] == '#')add(rcnt, tag[x][y]);
 
 x = i - 1, y = j;
 while (s[x][y] == '.')x--;
 if (s[x][y] == '#')add(rcnt, tag[x][y]);
 
 x = i, y = j + 1;
 while (s[x][y] == '.')y++;
 if (s[x][y] == '#')add(rcnt, tag[x][y]); 
 
 x = i + 1, y = j;
 while (s[x][y] == '.')x++;
 if (s[x][y] == '#')add(rcnt, tag[x][y]);
 
 rcnt++; 
 } 
 }
 }
 ans = inf;
 DLX(0);
 printf("%d\n", ans); 
 }
 return 0;
}

  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。

  3. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)