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. DTW的原理及matlab实现

    参考: https://www.cnblogs.com/Daringoo/p/4095508.html

  2. 进阶开发——文档,缓存,ip限速

    一.文档自动化管理 1.django rest framework提供了一个接口: 可以将代码中注释转换为文档中内容(list,create等),以及help_text等等,且会生成JavaScrip ...

  3. k8s HPA自动收缩

    HPA自动收缩 autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量 #创建一个replicaset测试 [r ...

  4. jenkins 邮箱设置

    一.先设置管理员邮箱地址 二.设置邮箱

  5. linux中一些特殊的中文文件不能删除问题

    例: [root@iZ2zecl4i8oy1rvs00dqzeZ tmp]# ,),(,,' [root@iZ2zecl4i8oy1rvs00dqzeZ tmp]# echo "rm -rf ...

  6. Linux压缩和解压命令

    zip命令: 压缩 :zip -r files.zip fileFolder 解压:unzip files.zip tar命令: 压缩:tar -cvf files.tar fileFolder 解压 ...

  7. Span<T>

    Introduction Span<T> is a new type we are adding to the platform to represent contiguous regio ...

  8. 前后端进行数据交互时候 要优先考虑json格式即简直对形式,[{}] 列表形式 等便于操作的数据结构

    前后端进行数据交互时候 要优先考虑json格式即简直对形式,[{}] 列表形式 等便于操作的数据结构

  9. group by具有去重的功能

    group by具有去重的功能

  10. npm 和package.json 文件

    你可能还记得使用vue-cli 创建vue项目.当创建项目完成后,我们进入到项目目录,启动cmd命令窗口,输入npm install,它就会安装一堆东西(依赖),然后再输入npm run dev, 我 ...