FZU-2150 Fire Game

来源:互联网 发布:80端口打不开 编辑:程序博客网 时间:2024/06/06 08:41

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
43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#
Sample Output
Case 1: 1Case 2: -1Case 3: 0Case 4: 2


题意:两个起点,然后问你走完全部#所需要的最少时间。如果不能输出-1.

简单bfs,记录搜索的点数+最长步数即可。

#include <cstdio>#include <queue>#include <cstring>using namespace std;const int MAXN=100+7;int n ,m;char tu[12][12];int x[MAXN],y[MAXN];bool vis[12][12];int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};int cnt;int sum,MAX;struct node{    int x,y,step;    node(int a,int b,int c)    {        x=a;        y=b;        step=c;    }};void bfs(int sx1,int sy1,int sx2,int sy2){    queue<node>q;    memset(vis,0,sizeof(vis));    vis[sx1][sy1]=1;    q.push(node(sx1,sy1,0));    if(sx1!=sx2||sy1!=sy2)    {        vis[sx2][sy2]=1;        q.push(node(sx2,sy2,0));    }    sum=MAX=0;    while(!q.empty())    {        node u=q.front();        q.pop();        sum++;        MAX=max(MAX,u.step);        for(int i = 0;i < 4 ; ++i)        {            int nx=u.x+dx[i];            int ny=u.y+dy[i];            if(nx>=0&&nx<n&&ny>=0&&ny<m&&!vis[nx][ny]&&tu[nx][ny]=='#')            {                vis[nx][ny]=1;                q.push(node(nx,ny,u.step+1));            }        }    }}int main(){    int t;    scanf("%d",&t);    int ca=0;    while(t--)    {        ca++;        cnt=0;        scanf("%d%d",&n,&m);        for(int i = 0 ; i < n ; ++i)        {            scanf("%s",tu[i]);            for(int j = 0 ; j < m ; ++j)            {                if(tu[i][j] == '#')                {                    x[cnt]=i;                    y[cnt++]=j;                }            }        }        printf("Case %d: ",ca);        int ans=1e9;        for(int i = 0;i < cnt; ++i)            for(int j = 0;j < cnt; ++j)        {            bfs(x[i],y[i],x[j],y[j]);            if(sum==cnt)ans=min(ans,MAX);        }        if(ans==1e9)puts("-1");        else printf("%d\n",ans);    }    return 0;}






原创粉丝点击