fzu2150 搜索

来源:互联网 发布:mysql limit 编辑:程序博客网 时间:2024/06/06 04:44
I - Fire Game
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice FZU 2150

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

题意: Fat brother 和 Maze想在一片地上玩耍,这块地上有一些草(#),他们要把草烧干净才能玩耍,两个人每人都有一根火柴,每个火柴只能使用一次便报废了,火焰一秒可以向四面延伸一格,问最少需要多少时间可以将所有草地烧干净,如果烧不干净输出-1


如果有3块以上草地直接输出-1就可以了;
如果有两个格子或者小于两个格子 ,直接输出0 就可以了;
如果只有一块地,那么要用两重循环找出最少时间;
如果有两块草地,那么要在两块草地上遍历寻找两块草地各自所用的最小时间,输出大者;
当只有一块草地时,寻找两个防火点,一起进行bfs,找到最小时间

具体实现见代码:

#include <iostream>#include <string.h>#include <stdio.h>#include <queue>#include <vector>using namespace std;int n,m;char str[20][20],str2[20][20];int d[20][20],vis[20][20];int dx[4]= {0,0,1,-1};int dy[4]= {-1,1,0,0};int flag,tot;struct point{    int x,y,num;};vector<point>G[105];void dfs(int x,int y){    vis[x][y]=1;    point p;    p.x=x;    p.y=y;    p.num=0;    G[flag].push_back(p);    int nx,ny;    for(int i=0; i<4; i++)    {        nx=x+dx[i];        ny=y+dy[i];        if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[nx][ny]==0&&str[nx][ny]=='#')        {            vis[nx][ny]=1;            dfs(nx,ny);        }    }}int min(int a,int b){    if(a<b)        return a;    return b;}int max(int a,int b){    if(a>b)        return a;    return b;}int bfs(point xx){    int maxd=-2;    point now;    memset(vis,0,sizeof(vis));    vis[xx.x][xx.y]=1;    //vis[yy.x][yy.y]=1;    queue<point>que;    que.push(xx);    //que.push(yy);    while(!que.empty())    {        point tmp=que.front();        que.pop();        int nx,ny;        for(int i=0; i<4; i++)        {            nx=tmp.x+dx[i];            ny=tmp.y+dy[i];            if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[nx][ny]==0&&str[nx][ny]=='#')            {                now.x=nx;                vis[nx][ny]=1;                now.y=ny;                now.num=tmp.num+1;                que.push(now);                maxd=max(now.num,maxd);            }        }    }    return maxd;}int bfs2(point xx,point yy){    int maxd=-2;    point now;    memset(vis,0,sizeof(vis));    vis[xx.x][xx.y]=1;    vis[yy.x][yy.y]=1;    queue<point>que;    que.push(xx);    que.push(yy);    while(!que.empty())    {        point tmp=que.front();        que.pop();        int nx,ny;        for(int i=0; i<4; i++)        {            nx=tmp.x+dx[i];            ny=tmp.y+dy[i];            if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[nx][ny]==0&&str[nx][ny]=='#')            {                now.x=nx;                vis[nx][ny]=1;                now.y=ny;                now.num=tmp.num+1;                que.push(now);                maxd=max(now.num,maxd);            }        }    }    return maxd;}int main(){    int t;    int T=0;    int num;    cin>>t;    while(cin>>n>>m)    {        G[1].clear();        G[2].clear();        num=0;        memset(str,0,sizeof(0));        for(int i=0; i<n; i++)        {            cin>>str[i];            for(int j=0; j<m; j++)                if(str[i][j]=='#')                    num++;        }       cout<<"Case "<<++T<<": ";        if(num<=2)        {            cout<<0<<endl;            continue;        }        flag=0;        tot=0;        memset(vis,0,sizeof(vis));        for(int i=0; i<n; i++)        {            for(int j=0; j<m; j++)            {                if(str[i][j]=='#'&&vis[i][j]==0)                {                    flag++;                 dfs(i,j);                }            }        }        int ans1=999,ans2=999;        if(flag>=3)        {            cout<<-1<<endl;            continue;        }        int temp=0,ttp=0;        if(flag==1)        {            for(int i=0;i<G[1].size();i++)            {                for(int j=i+1;j<G[1].size();j++)                {                    ttp=bfs2(G[1][i],G[1][j]);                    ans1=min(ttp,ans1);                }            }        }        else        {            for(int i=1; i<=flag; i++)        {            for(int j=0;j<G[i].size();j++)            {                     temp=bfs(G[i][j]);                     //cout<<"temp=="<<temp<<endl;                     if(i==1)                        ans1=min(ans1,temp);                     if(i==2)                        ans2=min(ans2,temp);            }        }        }      //  cout<<ans1<<" "<<ans2<<endl;      // cout<<flag<<"=flag"<<endl;        if(flag==1)        {            cout<<ans1<<endl;        }        else        {            cout<<max(ans1,ans2)<<endl;        }    }    return 0;}


0 0