FZU2150 Fire Game

来源:互联网 发布:禁止局域网上网软件 编辑:程序博客网 时间:2024/04/30 00:57
Problem 2150 Fire Game 

Time Limit: 1000 mSec Memory Limit : 32768 KB


Problem Description


Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

Input


The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

Output


For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.

Sample Input
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

Sample Output
Case 1: 1
Case 2: -1
Case 3: 0

Case 4: 2
题意:给出一个m*n的图,‘#’表示草坪,‘ . ’表示空地。选择在任意的两个草坪格子(可相同)点火,火每 1 s会向周围四个格子扩散,问选择那两个点使得燃烧所有的草坪花费时间最小?

思路:双起点BFS . 枚举起点+普通BFS

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <cstdlib>using namespace std ;int col , row ;int num , checknum;                                 //num :'#'总个数 ; checknum : 每次BFS的‘#'个数int ans ;int next[4][2] = {{-1,0} ,{0,1} ,{1,0} ,{0,-1} } ;  //方向int vis[20][20] ;                                   //记录步数char ch[20][20] ;                                   //存图struct point {    int row ;    int col ;};bool judge(point check) {                           //判断边界    if( check.row >= row  || check.row < 0 || check.col >= col || check.col < 0 )        return false ;    return true ;}bool bfs(){    bool flag = false ;    point start1 ;                                  //起点1 ,起点2    point start2 ;    point init  ;    queue<point> Q1 ;    while(!Q1.empty()) {Q1.pop() ;}                 //初始化    for( int i = 0 ;  i < row ; ++i ) {             //枚举起点1        for( int j = 0 ;j  < col ; ++j ) {                if(ch[i][j]=='.' ) continue ;            for( int k = i ; k < row ; ++k ) {                for( int l = 0 ; l < col ; ++l ) {                    if( k == i && l < j ) continue ;//从第一个起点开始找第二个                    if( ch[k][l] == '#' ) {                        memset(vis,0,sizeof(vis)) ; //切记初始化                        start1.row = i , start1.col = j ;                        start2.row = k , start2.col = l ;                        Q1.push(start1) ;                        Q1.push(start2) ;                        vis[start1.row][start1.col] = vis[start2.row][start2.col] = 1 ;                        (i==k && j == l) ? (checknum = 1)  : (checknum = 2 ) ;                        while( !Q1.empty()  ) {      //若相同,则初始为1个,否则2个                                start1 = Q1.front() ;                                Q1.pop() ;                                point  init ;                                for(int step = 0 ; step < 4 ; ++step) {                                   init.row = start1.row + next[step][0] ;                                        init.col = start1.col +next[step][1] ;                                            if( judge(init) && (!vis[init.row][init.col]) && ch[init.row][init.col] == '#' ) {                                                Q1.push(init) ; // 1判边界 ;2判是否经过 ; 3判目标                                                vis[init.row][init.col] = vis[start1.row][start1.col] + 1  ;//记录步数                                                checknum++;     //找到+1                                            }                                }                        }                        if(checknum == num ) {flag = true ;//标记有解                        int maxx = 0 ;                     //找当前解                        for( int m = 0 ; m < row ; ++m ) {                            for( int n = 0 ; n < col ; ++n ){                                if( maxx < vis[m][n] ) maxx =vis[m][n] ;                            }                        }                       ans = (ans > maxx) ? maxx : ans ;}//找最优解                    }                }            }        }    }    if(!flag) return false ;    return true ;}int main () {    int T , text  =  1;    cin >> T ;    while(T--) {            num  = 0 ;        scanf("%d%d",&row ,&col) ;        for( int i = 0; i < row ; ++i ) {                scanf("%s",ch[i]) ;                for ( int j = 0 ; j < col ; ++j ) {                if( ch[i][j] == '#' ) {                    num++ ;                }            }        }        ans = 214748364 ;        printf("Case %d: ",text++) ;       if( num <= 2 ) {        printf("0\n");        continue ;       }       if(!bfs()) {        printf("-1\n");       }       else {        printf("%d\n",ans-1) ;       }    }    return 0 ;}


0 0
原创粉丝点击