天天看點

Fire Game(FZU 2150) —— BFS

Fire Game(FZU 2150) —— BFS

 Problem 2150 Fire Game

Accept: 384    Submit: 1467

Time Limit: 1000 mSec    Memory Limit : 32768 KB

Fire Game(FZU 2150) —— BFS
 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.

Fire Game(FZU 2150) —— BFS
 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

Fire Game(FZU 2150) —— BFS
 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      

我的第一道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;
}