搜索--I - Fire Game (两个节点)

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

题目:

    题目的意思是,两个人在玩游戏之前,需要把草堆全部清除掉。他们两个可以选择同一个草堆点燃,也可以选择不同的(但只能点燃一次,此外火势是可以蔓延的时间为1分钟)。问是否可以清除(燃烧)所有的草堆,若能的话输出做小的燃烧时间,不能就请输出-1。

  一开始思路很混乱,不清楚标记的过程,找不到合适的标记方案。经过一番百度研究,才有了一点点思路。

  毋庸置疑,求最优解一定使用广度搜索。我们可以讲任意两个火堆(一个作为起点,一个作为终点,所以传入bfs()函数里面是两个变量),燃烧完全部火堆所需要的时间,枚举任意两个火堆后,取最小值。在枚举两个火堆的过程中(思路是:从起点开始,逐个记录蔓延到某个火堆的时间。要清除所有的火堆,就是从这些时间里面求最大值)。

#include <cstdio>  
#include <cstring>   
#include <iostream>  
#include <algorithm>  
#include <cstdlib>  
#include <queue>  
using namespace std;  
#define INF 0x3f3f3f3f
typedef struct node  
{
  int x;
  int y;
}node;
node S,T;  
  
int t,n,m; 
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 
int cnt[15][15];  
char cc[15][15];  
bool vis[15][15];  


 
int check(node a)  
{  
    if(a.x>=0&&a.x<n&&a.y>=0&&a.y<m) 
	   return 1;  
    else
	  return 0;  
}  
int bfs(node S,node T)  
{  
    queue<node>Q;  
    memset(vis,false,sizeof(vis));  
    memset(cnt,INF,sizeof(cnt)); 
	Q.push(S);  
    Q.push(T); 
    cnt[S.x][S.y]=0;  
    cnt[T.x][T.y]=0;    
    vis[S.x][S.y]=true;  
    vis[T.x][T.y]=true;  
    while(!Q.empty())  
    {  
        node head;
		head=Q.front();  
        Q.pop();  
        for(int i=0;i<4;i++)  
        {  
            node e;  
            e.x=head.x+dir[i][0];  
            e.y=head.y+dir[i][1];  
            if(check(e)&&cc[e.x][e.y]=='#'&&!vis[e.x][e.y]&&cnt[e.x][e.y]>cnt[head.x][head.y]+1)  
            {  
                vis[e.x][e.y]=true;  
                cnt[e.x][e.y]=cnt[head.x][head.y]+1; //燃记录烧到到e所需要花费的时间 
                Q.push(e);  
            }  
        }  
    }  
    int ma=0;  
    for(int i=0;i<n;i++)  
    {  
        for(int j=0;j<m;j++)  
        {  
            if(cc[i][j]=='#')  
            {  
                ma=max(ma,cnt[i][j]);  
            }  
        }  
    }  
    return ma;  
}  
int main(void)  
{  
    scanf("%d",&t);  
    for(int cas=1;cas<=t;cas++)  
    {  
        
        int ans=INF;  
        scanf("%d%d",&n,&m);  
        for(int i=0;i<n;i++)  
            scanf("%s",cc[i]);  
        for(int i=0;i<n;i++)  
        {  
            for(int j=0;j<m;j++)  
            {  
                if(cc[i][j]=='#')  
                {  
                    S.x=i;  
                    S.y=j;  
                    for(int ii=0;ii<n;ii++)  
                    {  
                        for(int jj=0;jj<m;jj++)  
                        {  
                            if(cc[ii][jj]=='#')  
                            {  
                                T.x=ii;  
                                T.y=jj;  
                                ans=min(ans,bfs(S,T));  //去找一个作为起点,一个作为终点的两个点 ,燃烧它们所需要的时间,最后取所有的最小的值 
                            }  
                        }  
                    }  
                }  
            }  
        }  
        printf("Case %d: ",cas);  
        if(ans==INF)
		   cout<<-1<<endl;  
        else 
		  cout<<ans<<endl;  
    }  
    return 0;  
}  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值