本题大意:给出一个n * m的地,‘#’ 代表草, ‘.’代表陆地,每次选择这片地里的两片草,可选相等的草,选择的两片草初始状态为被燃状态,每一分钟被点燃的草会将身边的四连块点。问你需要对于给定的这片地最少需要多少分钟能燃烧完,燃烧不完输出 -1.

  本题思路:很直观的一道题,枚举所有可能开始燃烧的点,更新最小值就行,无法燃烧完则是输出-1。

  参考代码:

 //要勤于思考,思考使人明智,不要总是因为一两个点没有想到就浪费时间
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std; struct node {
int x, y, step;
}now, Next; const int maxn = + , INF = 0x3f3f3f3f;
int n, m, t, ans, connected_block, maxstep, Case = ;
char maze[maxn][maxn];
bool vis_index[maxn][maxn];
vector <node> grass; bool check () {
for(int i = ; i < n; i ++)
for(int j = ; j < m; j ++)
if(maze[i][j] == '#' && !vis_index[i][j]) return false;
return true;
} bool useful(int x, int y) {
return (x >= && y >= && x < n && y < m && maze[x][y] == '#' && !vis_index[x][y]);
} void Init() {
grass.clear();
memset(vis_index, false, sizeof vis_index);
ans = INF, connected_block = ;
} int bfs(node n1, node n2) {
queue < node> Q;
memset(vis_index, false, sizeof vis_index);
Q.push(n1), Q.push(n2);
maxstep = ;
while(!Q.empty()) {
now = Q.front();
Q.pop();
if(vis_index[now.x][now.y]) continue;
maxstep = now.step;
vis_index[now.x][now.y] = true;
for(int dx = -; dx <= ; dx ++) {
for(int dy = -; dy <= ; dy ++) {
if(abs(dx - dy) == ) {
if(useful(now.x + dx, now.y + dy)) {
Next.x = now.x + dx, Next.y = now.y + dy, Next.step = now.step + ;
Q.push(Next);
}
}
}
}
}
return maxstep;
} int main () {
scanf("%d", &t);
while(t --) {
Init();
scanf("%d %d", &n, &m);
getchar();
for(int i = ; i < n; i ++) {
for(int j = ; j < m; j ++) {
maze[i][j] =getchar();
if(maze[i][j] == '#') {
node g;
g.x = i, g.y = j, g.step = ;
grass.push_back(g);
}
}
getchar();
}
//Find answer
for(int i = ; i < grass.size(); i ++)
for(int j = i; j < grass.size(); j ++) {
grass[i].step = , grass[j].step = ;
int temp = min(bfs(grass[i], grass[j]), ans);
if(check()) ans = min(temp, ans);
}
printf("Case %d: ", ++ Case);
if(ans == INF) printf("-1\n");
else printf("%d\n", ans);
}
return ;
}

FZU-2150.FireGame.(BFS))的更多相关文章

  1. Fire Game FZU - 2150 (bfs)

    Problem 2150 Fire Game Accept: 3772    Submit: 12868Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  2. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

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

  3. FZU 2150 Fire Game (暴力BFS)

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

  4. FZU 2150 Fire Game

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

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

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

  6. (广搜)Fire Game -- FZU -- 2150

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS    ...

  7. fzu 2150 Fire Game 【身手BFS】

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

  8. (FZU 2150) Fire Game (bfs)

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

  9. FZU 2150 fire game (bfs)

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

  10. FZU 2150 Fire Game(BFS)

    点我看题目 题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的.问你最少花多少时间 ...

随机推荐

  1. Hadoop 3.0 安装

    1.      下载Hadoop 3.0 http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-3.0.0/hadoop-3. ...

  2. python 阿狸的进阶之路(7)

    面向对象 转自林海峰的博客  http://www.cnblogs.com/linhaifeng/articles/6182264.html 面向对象的理解: 将数据分类,比如学生类.数据有关的函数, ...

  3. centos7 Apache 2.4.6 多域名多网站配置

    Apache 2.4.6 多域名多网站配置 在/etc/httpd/conf 下 编辑 vim httpd.conf 添加:ServerName 外网IP 并注释 #DocumentRoot &quo ...

  4. URL中文乱码及特殊字符处理

    一.中文乱码 IE高版本(应该是9以上,不确定),在get方式请求中中文传到后台容易出现乱码问题.解决方法如下: 1.第一种,换成post方式 如果可以得话换成post方式就可以.如果采用表单或者aj ...

  5. 配置maven访问nexus,配置项目pom.xml以发布maven项目到nexus中

    maven访问nexus有三种配置方法,分别为: 项目pom.xml,优先级最高: user的settings.xml,优先级中,未在pom.xml中配置repository标签,则使用这个配置: m ...

  6. 配置tomcat的开发环境

    第一步:鼠标右键计算机->属性->高级系统设置,进去之后,点击环境变量,如下图所示: 第二步:开始配置tomcat的环境变量,新建系统变量名CATALINA_BASE,值tomcat的安装 ...

  7. text-align: justify;浏览器、安卓手机不兼容问题

    https://segmentfault.com/a/1190000013146385

  8. Oracle问题小结

    1.win8.1安装Oracle11g后,重启电脑,出现黑屏. 解决办法:安全模式下,找到以oracle开头的全部服务,所有“自动”或者“自动(延迟启动)”的都设置为“手动”,只需要开启OracleO ...

  9. Python开发环境搭建指导

    本文主要介绍Python开发环境的搭建.主要包括如下几部分内容: (1)Python软件的安装.注意版本的选择和安装过程中选项的勾选. (2)pip工具环境变量.镜像源的配置使用和常用镜像源介绍.pi ...

  10. CHAR 和VARCHAR的区别

    CHAR(10)是不可变长度为10的字符串,占的存储空间始终为10个字符的长度,而VARCHAR(10)是可变长度的字符串,故而可以节省空间.例如:储存"aaaaabbbbb",则 ...