程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> FZU 2150 Fire Game£¨BFS)

FZU 2150 Fire Game£¨BFS)

編輯:C++入門知識

FZU 2150 Fire Game£¨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.

\ 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<†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KCjxoMj48aW1nIHNyYz0="https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012113382616.gif" alt="\"> 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
ÌâÒ⣺Á½¸öСº¢·Ö±ðÔÚÁ½¸öµØ·½·Å»ð£¬ÎÊÊÇ·ñÄÜÉÕ¹â²ÝµØ£¬ÈôÄÜÇó×îÉÙʱ¼ä¡£ ÓÉÓÚn,m<=10£¬¿¼ÂDZ©Á¦Ã¶¾ÙËÑË÷£¨BFS)Çó×îÉÙʱ¼ä¡£¡£¸½ÉÏ£¨10000MSµÄ´úÂ룩
#include
#include
#include
#include
#include
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]={{0,1},{0,-1},{-1,0},{1,0}};
//typedef pairpill;
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);
    queueq;
    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  "<
¸½ÉÏÎҵij¬Ê±µÄ£º
#include
#include
#include
#include
#include
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 pairpill;
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);
    queueq;
    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  "<>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<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved