传送门:【HDU】3529 Bomberman - Just Search!
题目分析:明白炸弹是全图能放的地方都可以放,且是同时爆炸的,那么就是重复覆盖问题了,没什么可以思考的地方,又是模板题= =,没意思。。。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define REV( i , a , b ) for ( int i = a - 1 ; i >= b ; -- i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )
#define REC( i , A , o ) for ( int i = A[o] ; i != o ; i = A[i] )
const int MAXR = 250 ;
const int MAXC = 35 ;
const int MAXNODE = 1000 ;
const int INF = 0x3f3f3f3f ;
struct DLX {
int L[MAXNODE] , R[MAXNODE] , U[MAXNODE] , D[MAXNODE] ;
int row[MAXNODE] , col[MAXNODE] ;
int H[MAXR] , S[MAXC] ;
bool vis[MAXC] ;
int deep ;
int size ;
int nv ;
int G[20][20] ;
int n , m ;
char s[20] ;
void init () {
CLR ( H , -1 ) ;
FOR ( i , 0 , nv ) {
S[i] = 0 ;
U[i] = D[i] = i ;
L[i] = i - 1 ;
R[i] = i + 1 ;
}
L[0] = nv ;
R[nv] = 0 ;
size = nv ;
deep = INF ;
}
void link ( int r , int c ) {
++ size ;
++ S[c] ;
row[size] = r ;
col[size] = c ;
U[size] = U[c] ;
D[size] = c ;
D[U[c]] = size ;
U[c] = size ;
if ( ~H[r] ) {
L[size] = L[H[r]] ;
R[size] = H[r] ;
L[R[size]] = size ;
R[L[size]] = size ;
}
else
H[r] = L[size] = R[size] = size ;
}
void remove ( int c ) {
REC ( i , D , c ) {
L[R[i]] = L[i] ;
R[L[i]] = R[i] ;
}
}
void resume ( int c ) {
REC ( i , U , c ) {
L[R[i]] = i ;
R[L[i]] = i ;
}
}
int h () {
int cnt = 0 ;
CLR ( vis , 0 ) ;
REC ( i , R , 0 )
if ( !vis[i] ) {
++ cnt ;
vis[i] = 1 ;
REC ( j , D , i )
REC ( k , R , j )
vis[col[k]] = 1 ;
}
return cnt ;
}
void dance ( int d ) {
if ( d + h () >= deep )
return ;
if ( R[0] == 0 ) {
deep = min ( deep , d ) ;
return ;
}
int c = R[0] ;
REC ( i , R , 0 )
if ( S[c] > S[i] )
c = i ;
REC ( i , D , c ) {
remove ( i ) ;
REC ( j , R , i )
remove ( j ) ;
dance ( d + 1 ) ;
REC ( j , L , i )
resume ( j ) ;
resume ( i ) ;
}
}
void solve () {
nv = 0 ;
REP ( i , 0 , n ) {
scanf ( "%s" , s ) ;
REP ( j , 0 , m ) {
if ( s[j] == '*' )
G[i][j] = -1 ;
else if ( s[j] == '.' )
G[i][j] = 0 ;
else
G[i][j] = ++ nv ;
}
}
init () ;
int r = 0 ;
REP ( i , 0 , n )
REP ( j , 0 , m )
if ( !G[i][j] ) {
++ r ;
REP ( x , i + 1 , n )
if ( G[x][j] ) {
if ( G[x][j] > 0 )
link ( r , G[x][j] ) ;
break ;
}
REV ( x , i , 0 )
if ( G[x][j] ) {
if ( G[x][j] > 0 )
link ( r , G[x][j] ) ;
break ;
}
REP ( y , j + 1 , m )
if ( G[i][y] ) {
if ( G[i][y] > 0 )
link ( r , G[i][y] ) ;
break ;
}
REV ( y , j , 0 )
if ( G[i][y] ) {
if ( G[i][y] > 0 )
link ( r , G[i][y] ) ;
break ;
}
}
dance ( 0 ) ;
printf ( "%d\n" , deep ) ;
}
} dlx ;
int main () {
while ( ~scanf ( "%d%d" , &dlx.n , &dlx.m ) )
dlx.solve () ;
return 0 ;
}