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

 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
 
题意:你可以从2个地方点火,问能否把所有的草烧光,在一个联通块里的草火会传递,如果可以烧掉所有的草输出最少的时间
思路:我是先dfs确定连通块 然后再bfs判断是否可以全部烧完(其实这个方法很慢 需要600ms 其实直接bfs更快)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int n,m;
char G[][];
bool vis[][];
int ans;
int num;
struct node{
int x,y,step;
};
bool jug(){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
if(G[i][j]=='#'&&!vis[i][j])
return false;
}
return true;
}
void bfs(int x,int y,int x2,int y2){
queue<node > q;
node t; t.step=; t.x=x; t.y=y;
node tt; tt.step=; tt.x=x2; tt.y=y2;
if(!vis[x][y])
vis[x][y]=,q.push(t);
if(!vis[x2][y2])
vis[x2][y2]=,q.push(tt);
while(!q.empty()){
node temp=q.front();
q.pop();
ans=max(ans,temp.step);
for(int i=;i<;i++){
int xx=temp.x+dir[i][];
int yy=temp.y+dir[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=m&&G[xx][yy]=='#'&&!vis[xx][yy]){
vis[xx][yy]=;
node te; te.x=xx; te.y=yy; te.step=temp.step+;
q.push(te);
}
}
}
}
void dfs(int x,int y){
for(int i=;i<;i++){
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=m&&!vis[xx][yy]&&G[xx][yy]=='#'){
vis[xx][yy]=;
dfs(xx,yy);
}
}
}
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
int w=;
while(t--){
cin>>n>>m;
num=;
pair<int,int> p[];
int sz=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
cin>>G[i][j];
if(G[i][j]=='#'){
sz++;
p[sz].first=i;
p[sz].second=j;
}
}
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(!vis[i][j]&&G[i][j]=='#'){
num++;
vis[i][j]=;
dfs(i,j);
}
if(num<=){
int a=inf;
for(int i=;i<=sz;i++)
for(int j=;j<=sz;j++){
memset(vis,,sizeof(vis));
ans=;
bfs(p[i].first,p[i].second,p[j].first,p[j].second);
if(jug())
a=min(a,ans);
}
cout<<"Case "<<w++<<": "<<a<<endl;
}else cout<<"Case "<<w++<<": -1"<<endl;
}
return ;
}

FZU 2150 Fire Game (bfs+dfs)的更多相关文章

  1. (FZU 2150) Fire Game (bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...

  2. FZU - 2150 Fire Game bfs+双起点枚举

    题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...

  3. FZU 2150 Fire Game(点火游戏)

    FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description - 题目描述 ...

  4. fzu 2150 Fire Game 【身手BFS】

    称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...

  5. FZU 2150 fire game (bfs)

    Problem 2150 Fire Game Accept: 2133    Submit: 7494Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  6. FZU 2150 Fire Game (暴力BFS)

    [题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...

  7. FZU 2150 Fire Game

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

  8. 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) ...

  9. Fire Game (FZU 2150)(BFS)

    题解:一开始想错了,以为只要烧完就是那个答案,但是这不是最优的结果,需要每两个点都bfs一遍,找到如果能够全部烧完,找到花费时间最小的,如果不能return -1.在bfs的时候,记录答案的方法参考了 ...

随机推荐

  1. 字符串和ASCII之间的转换

    public class CharToAscii { public static void main(String[] args) { CharToAscii.AscToString(); CharT ...

  2. Web移动端---iPhone X适配(底部栏黑横线)

    一.相信大家有被iPhone X底部黑色横线支配的恐惧 上面我们可以看到,底部的导航栏被一条黑色横线所盖住,那么就很烦.下面我们可以开始进行适配环节 1.首先我们可以用 JS 判断手机环境是不是 iP ...

  3. Django框架导读

    1.虚拟环境的安装 2.web应用 C/S  B/S 架构 3.http协议介绍 4.状态码 5.原生socket 6.框架演变 7.项目演变 一.虚拟环境安装 什么是虚拟环境? 1.对真实环境的一个 ...

  4. MyBatis的demo

    把以前写的关于mybatis的demo放在这边,以便查看. 目录结构: package com.test.mybatis.util; import java.io.IOException; impor ...

  5. keyvalue对RDD s

    scala> val input =sc.textFile("/home/simon/SparkWorkspace/test.txt")input: org.apache.s ...

  6. python爬虫之初始Selenium

    1.初始 Selenium[1]  是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Moz ...

  7. zsh & tree & macOS

    zsh & tree & macOS https://unix.stackexchange.com/questions/22803/counting-files-in-leaves-o ...

  8. 循环神经网络RNN及LSTM

    一.循环神经网络RNN RNN综述 https://juejin.im/entry/5b97e36cf265da0aa81be239 RNN中为什么要采用tanh而不是ReLu作为激活函数?  htt ...

  9. border-color的深入理解

    .className{ width:100px;height:100px; border:100px solid; border-color: red green blue orange; } 最终的 ...

  10. WPF中如何调整TabControl的大小,使其跟随Window的大小而改变?

    多年不写技术博客,手生的很,也不知道大家都关注什么,最近在研究Wpf及3d模型的展示,碰到很多问题,这个是最后一个问题,写出来小结一下...... WPF中如何调整TabControl的大小,使其跟随 ...