FZU-2150 广搜

Problem 2150 Fire Game
Accept: 3701 Submit: 12637
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

题意:
两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一。求烧完所有的草需要的最少时间。如不能烧完输出-1。
*题意引用自luciozhang的博文。

没有人能一开始就想到正确的思路,就像第一次看到深搜和广搜的一些细节,书上都处理的很好,当时不理解为什么要这样写,其实没有什么不理解的,自己写一遍,再自己做几道题,就知道这些地方为什么这样写了,肯定是为了方便嘛!
下面这个代码有很多问题,也懒得改了,放在上面留个纪念,决定看下题解,自己再写一遍。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
char a[11][11];
int book[11][11];
struct node{
    int x;
    int y;
    int time;//记录自己是第几次被引燃的
};
queue<node> q;
int mintime;//同一次测试最小的时间
int time;//同一次测试每种情形的时间
int d[4][2]={{0,1},{1,0},{-1,0},{0,-1}};

void bfs(node n,int bx,int by){
    if(time<n.time)//记录最大时间,也就是实际需要的时间
        time=n.time;
    if(a[n.x+d[0][0]][n.y+d[0][1]]=='#' && n.y<by && book[n.x+d[0][0]][n.y+d[0][1]]){
        node tmp;
        tmp.x=n.x;
        tmp.y=n.y+1;
        tmp.time=n.time+1;
        q.push(tmp);
        book.insert(100*tmp.x+tmp.y);//不采用把烧过的草变为空地,那会破坏原图,所以用集合来标记。
    }
    if(a[n.x+d[1][0]][n.y+d[1][1]]=='#' && n.x<bx && book[n.x+d[1][0]][n.y+d[1][1]]){
        node tmp;
        tmp.x=n.x+1;
        tmp.y=n.y;
        tmp.time=n.time+1;
        q.push(tmp);
        book.insert(100*tmp.x+tmp.y);
    }
    if(a[n.x+d[2][0]][n.y+d[2][1]]=='#' && n.x>0 && book[n.x+d[2][0]][n.y+d[2][1]]){
        node tmp;
        tmp.x=n.x-1;
        tmp.y=n.y;
        tmp.time=n.time+1;
        q.push(tmp);
        book.insert(100*tmp.x+tmp.y);
    }
    if(a[n.x+d[3][0]][n.y+d[3][1]]=='#' && n.y>0  && book[n.x+d[3][0]][n.y+d[3][1]]){
        node tmp;
        tmp.x=n.x;
        tmp.y=n.y-1;
        tmp.time=n.time+1;
        q.push(tmp);
        book.insert(100*tmp.x+tmp.y);
    }
}
int main(){
    int t;
    int flag=0;//标记是否成功
    cin >> t;//接受测试次数
    int m,n;
    int cnt;//记录草的数目
    while(t--){
        cnt=0;
        mintime=999;
        cin >> m >> n;// m rows n colums
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++){
                cin >> a[i][j];
                if(a[i][j]=='#')
                    cnt++;
            }
        for(int i=0;i<m;i++)//找第一个点
            for(int j=0;j<n;j++){
                if(a[i][j]=='#'){
                    node tmp;
                    tmp.x=i;
                    tmp.y=j-1;
                    tmp.time=0;
                    node mothertmp=tmp;
                    int mother=100*tmp.x+tmp.y;
                    for(int k=0;k<m;k++)//找第二个点
                        for(int l=0;l<n;l++){
                            if(a[k][l]=='#' && (k!=i||l!=j)){
                                book.clear();
                                time=0;
                                node tmp;
                                tmp.x=k;
                                tmp.y=l-1;
                                tmp.time=0;
                                q.push(mothertmp);
                                book.insert(100*tmp.x+tmp.y);
                                book.insert(mother);
                                while(!q.empty()){
                                    //模拟火的蔓延
                                    node tmp = q.front();
                                    bfs(tmp,m,n);
                                    q.pop();
                                }
                                if(mintime>time)
                                    mintime = time;
                                if(book.size()==cnt && flag==0)
                                    flag=1;
                            }
                        }
                }
            }
            while(!q.empty())
                q.pop();
            if(flag==1)
                cout << mintime << endl;
            else
                cout << -1 << endl;
        }
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值