FZU 2150 Fire Game (BFS), fzubfs

Source: Internet
Author: User

FZU 2150 Fire Game (BFS), fzubfs
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 wowould never burn out and the empty grid wowould 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 Input43 3. #. ###. #. 3. #. #. #. #. 3 3... #. #... 3 3 ###.. ##. # Sample OutputCase 1: 1 Case 2:-1 Case 3: 0 Case 4: 2
The two children set fire in two places and asked if they could burn the lawn. Because n, m <= 10, consider brute force enumeration search (BFS) to find the minimum time .. Attachment (milliseconds code)
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> typedef long LL; using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof a) const int INF = 0x3f3f3f3f; int dr [] [2] = {0, 1 }, {0,-1}, {-}, {}; // typedef pair <int, int> pill; struct point {int x, Y; point () {} point (int a, int B) {x = a; y = B ;}}; char mp [20] [20]; int vis [20] [20]; int d [20] [20]; int n, m, t, ans; int bfs (int x1, int y1, int x2, int y2) {CLEAR (d, INF); queue <point> q; point st; d [x1] [y1] = d [x2] [y2] = 0; q. push (point (x1, y1); q. push (point (x2, y2); while (! Q. empty () {st = q. front (); q. pop (); REP (I, 4) {int xx = st. x + dr [I] [0]; int yy = st. y + dr [I] [1]; if (xx <0 | xx> = n | yy <0 | yy> = m) continue; if (mp [xx] [yy]! = '#') Continue; if (d [xx] [yy]> d [st. x] [st. y] + 1) {d [xx] [yy] = d [st. x] [st. y] + 1; q. push (point (xx, yy) ;}} int sum = 0; REP (I, n) {REP (j, m) {if (mp [I] [j] = '#') sum = max (sum, d [I] [j]) ;}} return sum ;} void solve () {ans = INF; REP (I, n) {REP (j, m) {if (mp [I] [j] = '#') {REP (k, n) {REP (l, m) {if (k = I & l <= j) continue; // if (mp [k] [l] = '#') ans = min (ans, bfs (I, j, k, l) ;}}}}// cout <"2333" <ans <endl; printf ("% D \ n", ans = INF? -1: ans);} int main () {// std: ios: sync_with_stdio (false); scanf ("% d", & t ); int cas = 1; while (t --) {scanf ("% d", & n, & m); getchar (); int num = 0; REP (I, n) {gets (mp [I]); REP (j, m) {if (mp [I] [j] = '#') num ++ ;}} printf ("Case % d:", cas ++); if (num <= 2) {cout <0 <endl; continue;} solve ();} return 0 ;}

Attached to my timeout:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;int dr[][2]={{-1,0},{1,0},{0,-1},{0,1}};typedef pair<int,int>pill;char mp[15][15];int vis[15][15];int d[15][15];int n,m,t,ans;int bfs(int x1,int y1,int x2,int y2){    CLEAR(d,INF);    queue<pill>q;    pill st;    d[x1][y1]=d[x2][y2]=0;    q.push(make_pair(x1,y1));    q.push(make_pair(x2,y2));    while(!q.empty())    {        st=q.front();        q.pop();        REP(i,4)        {            int xx=st.first+dr[i][0];            int yy=st.second+dr[i][1];            if(xx<0&&xx>=n&&yy<0&&yy>=m)   continue;            if(mp[xx][yy]!='#')  continue;            if(d[xx][yy]>d[st.first][st.second]+1)            {                d[xx][yy]=d[st.first][st.second]+1;                q.push(make_pair(xx,yy));            }        }    }    int sum=0;    REP(i,n)    {        REP(j,m)       {           if(mp[i][j]=='#')              sum=max(sum,d[i][j]);       }    }    return sum;}void solve(){    ans=INF;    REP(i,n)    {        REP(j,m)        {            if(mp[i][j]=='#')            {                REP(k,n)                {                    REP(l,m)                    {                        if(k<=i&&l<=j)  continue;                        if(mp[k][l]=='#')                            ans=min(ans,bfs(i,j,k,l));                    }                }            }        }    }//    cout<<"2333  "<<ans<<endl;    printf("%d\n",ans==INF?-1:ans);}int main(){     std::ios::sync_with_stdio(false);     cin>>t;     int cas=1;     while(t--)     {         cin>>n>>m;         int num=0;         REP(i,n)         {             REP(j,m)             {                 cin>>mp[i][j];                 if(mp[i][j]=='#')                    num++;             }         }         printf("Case %d: ",cas++);         if(num<=2)         {              cout<<0<<endl;              continue;         }         solve();     }     return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.