码迷,mamicode.com
首页 > 其他好文 > 详细

FZU 2150 Fire Game BFS

时间:2016-08-14 07:21:27      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

Fire Game
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

第一次队内的组队赛 被虐的好惨 看来和大神实力还是有很大的差距 要努力一下了
这道题在比赛的时候没做出来 一开始想着可不可以dfs 后来思路又转到求最长的连续草的长度(事实证明这是错的
而且这道题的代码我写了一半 馒头写了一半 导致看上去非常的乱 后面很难下手去改 然后就放弃了

题意:#号是草 又两把火扔在草上面去烧 火往上下左右去蔓延 每次蔓延花费1s 问把所有的草都烧了要花多久
两把火一起bfs

赛后看学长的代码强力模仿了一波


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN     freopen("input.txt","r",stdin);
#define FOUT    freopen("output.txt","w",stdout);
#define INF     0x3f3f3f3f
#define INFLL   0x3f3f3f3f3f3f3f
#define lson    l,m,rt<<1
#define rson    m+1,r,rt<<1|1
typedef long long LL;
typedef pair<int,int> PII;

int N,M;
char mp[15][15];
int vis[15][15];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

struct node{
   int x,y;
   int time;
   node(int a,int b,int c){
      x=a;
      y=b;
      time=c;
   }
};


int check(int x,int y){
   return x>=0&&x<N&&y>=0&&y<M&&!vis[x][y]&&mp[x][y]==‘#‘;

}

int bfs(int x,int y,int x2,int y2,int& tot){
   queue<node>q;
   q.push(node(x,y,0));
   if(x!=x2||y!=y2)  q.push(node(x2,y2,0));
   vis[x][y]=1;
   vis[x2][y2]=1;
   int time=0;
   while(!q.empty()){
       node fro=q.front();
       q.pop();
       tot++;
       for(int i=0;i<4;i++){
           int xx=fro.x+dx[i];
           int yy=fro.y+dy[i];
           if(check(xx,yy)){
               vis[xx][yy]=1;
               q.push(node(xx,yy,fro.time+1));
               time=max(time,fro.time+1);
           }

       }
   }
   return time;

}

int main()
{
    //FIN
    int T;
    int cas=1;
    scanf("%d",&T);
    while(T--){
        int tot=0;
        scanf("%d%d",&N,&M);
        for(int i=0;i<N;i++){
            scanf("%s",mp[i]);
            for(int j=0;j<M;j++)  if(mp[i][j]==‘#‘)  tot++;

        }

        int ans=INF;

        for(int i=0;i<N*M;i++){
            int x=i/M , y=i%M;
            if(mp[x][y]==‘.‘)  continue;
            for(int j=i;j<N*M;j++){
                int x2=j/M , y2=j%M;
                if(mp[x2][y2]==‘.‘)  continue;
                int t=0;
                memset(vis,0,sizeof(vis));
                int now=bfs(x,y,x2,y2,t);
                if(t!=tot)  continue;
                ans=min(ans,now);

            }

        }
        printf("Case %d: %d\n",cas++,ans==INF ? -1:ans);


    }
}

  




FZU 2150 Fire Game BFS

标签:

原文地址:http://www.cnblogs.com/Hyouka/p/5769101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!