foj 2150 Fire Game(bfs暴力)
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+ which refers to the grid (x+, y), (x-, y), (x, y+), (x, y-). 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. <= T <=, <= n <=, <= m <=
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 -. See the sample input and output for more details.
Sample Input
.#.
###
.#. .#.
#.#
.#. ...
#.#
... ###
..#
#.#
Sample Output
Case :
Case : -
Case :
Case :
暴力枚举两点,bfs统计,最后求最小值。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 16
#define inf 1<<26
int n,m;
char mp[N][N];
int vis[N][N];
int dirx[]={,,-,};
int diry[]={-,,,};
struct Node{
int x,y;
int t;
}node[N*N]; int bfs(Node a,Node b){
queue<Node>q;
q.push(a);
q.push(b);
vis[a.x][a.y]=;
vis[b.x][b.y]=;
int T=;
while(!q.empty()){
Node tmp=q.front();
q.pop();
Node cnt;
T=max(T,tmp.t);
for(int i=;i<;i++){
cnt.x=tmp.x+dirx[i];
cnt.y=tmp.y+diry[i];
if(cnt.x< || cnt.x>=n || cnt.y< || cnt.y>=m) continue;
if(vis[cnt.x][cnt.y]) continue;
if(mp[cnt.x][cnt.y]=='.') continue;
vis[cnt.x][cnt.y]=;
cnt.t=tmp.t+;
q.push(cnt);
}
}
return T;
} int main()
{
int t,ac=;
scanf("%d",&t);
while(t--){
int num=;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='#'){
node[num].x=i;
node[num].y=j;
node[num++].t=;
}
}
} printf("Case %d: ",++ac);
if(num<=){
printf("0\n");
continue;
} int ans=inf;
for(int i=;i<num;i++){
for(int j=i;j<num;j++){
int flag=;
memset(vis,,sizeof(vis));
int w=bfs(node[i],node[j]);
//printf("===%d\n",w);
for(int k=;k<n;k++){
for(int l=;l<m;l++){
if(mp[k][l]=='#' && vis[k][l]==){
flag=;
break;
}
}
if(flag==){
break;
}
}
if(flag){
ans=min(ans,w);
}
}
} if(ans!=inf){
printf("%d\n",ans);
}
else{
printf("-1\n");
}
}
return ;
}
foj 2150 Fire Game(bfs暴力)的更多相关文章
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- (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+dfs)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...
- FZU - 2150 Fire Game bfs+双起点枚举
题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...
- 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 Problem 2150 Fire Game
Problem 2150 Fire Game Accept: 145 Submit: 542 Time Limit: 1000 mSec Memory Limit : 32768 KB P ...
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- FZOJ Problem 2150 Fire Game
...
随机推荐
- 【Xamarin挖墙脚系列:Xamarin正式发布了IOS的模拟器在Windows下】
xamarin 的发展越来越迅速.如果还感觉这玩意儿是个鸡肋,辣么请跟的上时代吧 . (额,对微软产品有严重偏见的请绕行..............其实你可以看看.net 基金会现有的开源项目再说不开 ...
- linux之SQL语句简明教程---SELECT
SQL是用来做什么的呢?一个最常用的方式是将资料从数据库中的表格内选出.从这一句回答中,我们马上可以看到两个关键字: 从 (FROM) 数据库中的表格内 选出 (SELECT).(表格是一个数据库内的 ...
- android 读取txt文件内容
Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问. 比如我们 ...
- 国内外DNS服务器地址列表
DNS(Domain Name System)是域名解析服务器的意思,它在互联网的作用是把域名转换成为网络可以识别的IP地址.目前国内电信运营商通过使用DNS劫持的方法,干扰用户正常上网,使得用户无法 ...
- 修改ORACLE-NLS_DATE_FORMAT时间格式的四种方式
修改ORACLE-NLS_DATE_FORMAT时间格式的四种方式 改变ORACLE -NLS_DATE_FORMAT中时间显示格式的显示有以下方式: 1.可以在用户环境变量中指定(LINUX). 在 ...
- read op case $op in
read op case $op in
- UVA 11212 Editing a Book
题意: 有一篇由n个自然段组成的文章.希望将他们排成递增序列.只能剪贴和粘贴交替进行,剪贴时可以剪贴一段连续的自然段. 分析: 用IDA*算法求解.当3*d+h>maxd时剪枝. 代码: #in ...
- 使用 HttpWebRequest 发送模拟 POST 请求
使用HttpWebRequest发送模拟POST请求 网页中,如果form的method="POST",这时点击submit按钮可以给服务器发送了一个POST请求,如果metho ...
- 基于粒子滤波的物体跟踪 Particle Filter Object Tracking
Video来源地址 一直都觉得粒子滤波是个挺牛的东西,每次试图看文献都被复杂的数学符号搞得看不下去.一个偶然的机会发现了Rob Hess(http://web.engr.oregonstate.edu ...
- 使用XmlReader读取xml文件之二
在.net开发中经常需要读写xml形式的文件(app.config和web.config分别是WinForm和WebForm中使用到的 xml文件的一个特列,并且微软提供了通用的方法,在此就不赘述了) ...