FZU 2150 Fire Game (bfs+dfs)
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
Sample Output
#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)的更多相关文章
- (FZU 2150) Fire Game (bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...
- FZU - 2150 Fire Game bfs+双起点枚举
题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- fzu 2150 Fire Game 【身手BFS】
称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...
- FZU 2150 fire game (bfs)
Problem 2150 Fire Game Accept: 2133 Submit: 7494Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- FZU 2150 Fire Game
Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- 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) ...
- Fire Game (FZU 2150)(BFS)
题解:一开始想错了,以为只要烧完就是那个答案,但是这不是最优的结果,需要每两个点都bfs一遍,找到如果能够全部烧完,找到花费时间最小的,如果不能return -1.在bfs的时候,记录答案的方法参考了 ...
随机推荐
- winform使用相关
1.回车键触发按钮点击事件——回车登录 设置窗体的AccessButton属性 2.密码框样式设置 设置PasswordChar为想要的密码显示的样式,如* 3.设置窗口居中 设置StartPosi ...
- 关于spring的源码的理解
从最基础的Hello World开始. spring的Hello World就三行代码: public void test() { ApplicationContext context = new C ...
- CMake--变量
1.一般变量 1)CMake变量引用的方式 使用${}进行变量的引用.例如: ${PROJECT_NAME} #返回项目名称 在 IF 等语句中,是直接使用变量名而不通过${}取值. 2)cmake自 ...
- java连接CentOS7上的redis
这篇博客写得挺全的: https://blog.csdn.net/achenyuan/article/details/78521831?locationNum=3&fps=1 我也是跟着这篇博 ...
- Sublime Text3 配置 NodeJs 开发环境
题外话:使用visual studio开发NodeJs也是很方便,只需要安装插件即可. 本着对Sublime Text3的喜爱,尤其是最近更新后,界面和功能上感觉更nice了,那就配置一发环境吧! ( ...
- C# Note5:使用相对路径读取文件
一.C#中使用相对路径读取配置文件 一般Solution的目录结构如下图所示: (如过看不到某些文件,可以点击 “显示所有文件” 图标) 方法一:由于生成的exe文件在bin\debug目录下,可以使 ...
- vue前端框架面试问题汇总
1.active-class是哪个组件的属性?嵌套路由怎么定义?答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么获取传过来的动态参数? 答: ...
- mongodb的安装方法
下载安装 mongodb官网下载地址:https://www.mongodb.org/downloads#produc...直接下载.msi文件并安装到指定目录即可.我的安装路径是D:\mongodb ...
- React Native & debug & debugger
React Native & debug & debugger http://localhost:8081/debugger-ui/ react-devtools # yarn: $ ...
- PHP人工智能库
PHP虽然不是人工智能语言,但做人工智能理论上没问题,下面本人整理了一些PHP人工智能库.1.NLPTools(http://php-nlp-tools.com/)NLPTools是一个PHP自然语言 ...