Fire Game(FZU 2150) —— BFS

来源:互联网 发布:股东大会网络投票方式 编辑:程序博客网 时间:2024/05/16 15:03
 Problem 2150 Fire Game

Accept: 384    Submit: 1467
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

43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#

Sample Output

Case 1: 1Case 2: -1Case 3: 0Case 4: 2


我的第一道BFS,异常艰辛啊。。这是到起点有两个点的BFS,学长讲完BFS的思想后,我在网上疯狂地找BFS的模板,因为我不会写啊= =。 尼玛看到网上一大串一大串的,瞬间傻了,怎么这么多啊,我的天。。后来我问我一同学做出来没,一看,别人已经写的差不多了,是自己写的。。顿时觉得自己弱爆了%>_<%, 于是决定还是自己慢慢写吧,,然后看了看刘汝佳的非STL写的代码,,最后自己一点一点写出来了,,待大功告成时,提交,意外的Compile Error ,用GNU C++提交的,于是改为Visual C++,后来改后n次 WA,再后来无数次TLE,,最后终于该为GNU C++后提交AC。。

#include<cstdio>#include<cstring>#include<queue>using namespace std;int n,m;int dx[4] = {0,0,-1,1}, dy[4] = {-1,1,0,0}; //坐标方向移动char s[12][12];int bfs(int x1,int y1,int x2,int y2) //x代表行,y代表列{    int vis[12][12]={0},time[12][12]={0}; //vis记录是否遍历过    int u,d,nx,ny;    queue<int> q;    q.push(x1*m+y1); q.push(x2*m+y2); //把坐标所在序号压进队列    vis[x1][y1] = vis[x2][y2] = 1;    time[x1][y1] = time[x2][y2] = 0;    while(!q.empty())    {        u = q.front(); q.pop();        x1 = u/m; y1 = u%m;        for(d=0; d<4; d++) //遍历四周        {            nx = x1+dx[d], ny = y1+dy[d];            if(nx>=0 && nx<n && ny>=0 && ny<m && !vis[nx][ny] && s[nx][ny]!='.')            {                q.push(nx*m+ny); vis[nx][ny] = 1;                time[nx][ny] = time[x1][y1]+1;            }        }    }    int f = 0, maxn = 0; //maxn为一次搜索的最终时间    for(int i=0; i<n; i++)    {        for(int j=0; j<m; j++)        {            if(s[i][j] == '#' && !vis[i][j]) f = 1; //f指无法遍历完            if(s[i][j] == '#' && time[i][j] > maxn) maxn = time[i][j];        }    }    if(f) return 0;    return maxn;}int main(){    int T,i,j,t=1;    scanf("%d", &T);    while(T--)    {        int c, mmin = 1000000, flag = 0, k = 0;        scanf("%d%d", &n,&m);        for(i=0; i<n; i++)        {            scanf("%s", s[i]);            for(j=0; j<m; j++)                if(s[i][j] == '#') k++; //k代表有多少个‘#’        }        for(i=0; i<n; i++)        {            for(j=0; j<m; j++)            {                if(s[i][j] == '#')                {                    int u,v;                    for(u=i; u<n; u++) //i,j,u,v用来把所有可能全部遍历                    {                        v = (u == i ? j+1 : 0);                        for(v; v<m; v++)                        {                            if(s[u][v] == '#')                            {                                c = bfs(i,j,u,v);                                if(!c) flag++; //flag求无法全部遍历完的数目                                if(c < mmin && c > 0) mmin = c; //mmin求最短时间                            }                        }                    }                }            }        }        if(mmin == 1000000) mmin = 0;        printf("Case %d: ", t++);        if(flag == k*(k-1)/2 && k > 2) printf("-1\n"); //当所有情况都无法遍历完时,则输出-1        else  printf("%d\n", mmin);    }    return 0;}


3 0
原创粉丝点击