-----FZU 2150-Fire Game

来源:互联网 发布:网页下载app源码 编辑:程序博客网 时间:2024/05/29 17:36

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的区域内放火,该区域有草,有的是空地,只有草才可以传播火,可以同时向上下左右传播,问你两兄弟能不能把草全烧光,不能输出-1,能的话输出草全烧光花的时间

解题思路:
数据量很小..直接枚举两个点bfs,值得注意的是,两兄弟可以选择在同一块草地放火…

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#define INF 99999999using namespace std;char map[15][15];int dp[15][15],x[105],y[105];///dp[i][j]存的是火烧到i,j方格所花的最小时间int n,m,len;int dri[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};struct node{    int x,y;};queue<node>que;int bfs(int x1,int y1,int x2,int y2){    int maxx=-99999;    for(int i=0; i<n; i++)        for(int j=0; j<m; j++)            dp[i][j]=INF;    node q1,q2,now,nex;    q1.x=x1,q1.y=y1;    q2.x=x2,q2.y=y2;    dp[x1][y1]=0;    dp[x2][y2]=0;    que.push(q1);    que.push(q2);    while(!que.empty())    {        now=que.front();        que.pop();        int x=now.x;        int y=now.y;        for(int k=0; k<4; k++)        {            int dx=x+dri[k][0];            int dy=y+dri[k][1];            if(dx<0||dy<0||dx>=n||dy>=m||map[dx][dy]=='.') continue;            if(dp[dx][dy]>dp[x][y]+1)///取最小时间            {                nex.x=dx,nex.y=dy;                dp[dx][dy]=dp[x][y]+1;                que.push(nex);            }        }    }    for(int i=0; i<n; i++)        for(int j=0; j<m; j++)            if(map[i][j]=='#')///只判断草地                maxx=max(maxx,dp[i][j]);    return maxx;}int main(){    int t;    scanf("%d",&t);    int p=0;    while(t--)    {        while(!que.empty()) que.pop();        scanf("%d%d",&n,&m);        len=0;        for(int i=0; i<n; i++)        {            scanf("%s",map[i]);            for(int j=0; j<m; j++)            {                if(map[i][j]=='#')                    x[len]=i,y[len++]=j;            }        }        int minn=INF;        ///枚举两个点        for(int i=0; i<len; i++)            for(int j=i; j<len ; j++)///注意,一开始可以选择在同一个方格放火            {                int k=bfs(x[i],y[i],x[j],y[j]);                minn=min(minn,k);///取最小时间            }            p++;            printf("Case %d: ",p);            if(minn==INF) printf("-1\n");///存在某个草地方格的dp值没有被更新,也就是火没有烧到这个方格        else printf("%d\n",minn);    }}
原创粉丝点击