zoukankan      html  css  js  c++  java
  • FZU-2150 Fire Game(BFS)

    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

    这个题一开始想麻烦了,一开始想的是先判断连通块的个数,再在每一个连通块里寻找最长链,于是去想了很久的最长链求
    法,然后发现如果是一坨都是草的话就会出问题,看了一眼标程,竟是最朴素的暴力算法,然后去查了一下原来1s可以运行
    的次数是5*1e8次,是我孤陋寡闻了一直以为是千万级。
    像这种有关最短路问题的搜索一般都是用bfs去做。
    程序为了避免卡常还是做了一点操作的qwq
     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <queue>
     6 #include <stack>
     7 #include <vector>
     8 #include <iostream>
     9 #include "algorithm"
    10 using namespace std;
    11 const int MAX=15;
    12 int t,cas,n,m,ans;
    13 char s[MAX][MAX];
    14 bool vis[MAX][MAX];
    15 int dx[]={1,0,-1,0};
    16 int dy[]={0,1,0,-1};
    17 struct Poi{
    18     int x,y,ste;
    19 };
    20 queue <Poi> q;
    21 inline int mx(int x,int y){return x>y?x:y;}
    22 inline int mn(int x,int y){return x<y?x:y;}
    23 void get_pos(int nu,int *x,int *y){
    24     *x=nu/m+1;if (nu%m==0) (*x)--;
    25     *y=nu%m;if (nu%m==0) (*y)=m;
    26 }
    27 void bfs(int cx,int cy,int vx,int vy){
    28     int i,j,cc,vv,an=0;Poi a,b;
    29     while (!q.empty()) q.pop();
    30     memset(vis,false,sizeof(vis));
    31     a.x=cx;a.y=cy,a.ste=0;
    32     b.x=vx;b.y=vy,b.ste=0;
    33     q.push(a);q.push(b);
    34     vis[cx][cy]=vis[vx][vy]=true;
    35     while (!q.empty()){
    36         a=q.front();q.pop();an=mx(an,a.ste);
    37         for (i=0;i<4;i++){
    38             cc=a.x+dx[i];
    39             vv=a.y+dy[i];
    40             if (s[cc][vv]=='#' && !vis[cc][vv]){
    41                 b.x=cc,b.y=vv,b.ste=a.ste+1;
    42                 vis[cc][vv]=true;
    43                 q.push(b);
    44             }
    45         }
    46     }
    47     for (i=1;i<=n;i++)
    48         for (j=1;j<=m;j++)
    49             if (s[i][j]=='#' && !vis[i][j])
    50                 return;
    51     ans=mn(ans,an);
    52 }
    53 int main(){
    54     freopen ("fire.in","r",stdin);
    55     freopen ("fire.out","w",stdout);
    56     int i,j,cx,cy,vx,vy;
    57     scanf("%d",&t);
    58     while (t--){
    59         scanf("%d%d
    ",&n,&m);
    60         ans=1e8;
    61         for (i=1;i<=n;i++) scanf("%s",s[i]+1);
    62         for (i=1;i<=n*m;i++){
    63             get_pos(i,&cx,&cy);
    64             if (s[cx][cy]=='#')
    65                 for (j=i+1;j<=n*m;j++){
    66                     get_pos(j,&vx,&vy);
    67                     if (s[vx][vy]=='#')
    68                         bfs(cx,cy,vx,vy);
    69                 }
    70         }
    71         printf("Case %d: ",++cas);
    72         if (ans==1e8) printf("-1
    ");
    73         else printf("%d
    ",ans);
    74     }
    75     return 0;
    76 }
    
    
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    SCP测试服务器的上行/下行带宽
    React-Native 之 GD (四)使用通知方式隐藏或显示TabBar
    React-Native 之 GD (五)属性声明和属性确认 及 占位图
    React-Native 之 GD (三)近半小时热门
    React-Native 之 GD (二)自定义共用导航栏样式
    React-Native 之 GD (一)目录结构与第三方框架使用与主题框架搭建
    React Native商城项目实战16
    React Native商城项目实战15
    React Native商城项目实战14
    React Native商城项目实战13
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/14489276.html
Copyright © 2011-2022 走看看