本题大意:给出一个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. SPring cloud (3)A Ribbon 负载均衡 配置初步

    1.引用pom <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...

  2. Xshell 用鼠标选中一段文字后自动换行

    现象: 使用Xshell连接远程服务器,一般选中都是鼠标选中,然后按快捷键 Ctrl+Insert复制,Shift+Insert粘贴. 可是当选中后松开鼠标,这时候仿佛在xshell里自动输了一个回车 ...

  3. java解析文件

    遇到两个小坑: 1.使用String.split,部分分隔符需要转义:https://www.cnblogs.com/mingforyou/archive/2013/09/03/3299569.htm ...

  4. dshow采集过程

    捕捉静态图片常用的filter是Sample Graber filter,它的用法参考手册.然后将捕捉filter的静态PIN连接到Sample Grabber,再将Sample Grabber连接到 ...

  5. Others-工具箱

    pycharm下载激活工具 : https://www.lanzous.com/i20tl8f作者(来源):https://www.52pojie.cn/thread-803822-1-1.html ...

  6. RabbitMQ系列教程之五:主题(Topic)(转载)

    RabbitMQ系列教程之五:主题(Topic) (本实例都是使用的Net的客户端,使用C#编写),说明,中文方括号[]表示名词. 在上一个教程中,我们改进了我们的日志记录系统. 没有使用只能够进行虚 ...

  7. Android对接WebService -- The given SOAPAction http://webservice.ams.cnpc.com/getServerTime does not match an operation.

    问题描述:Chrome Restlet下模拟请求出现如下问题 The given SOAPAction http://webservice.ams.cnpc.com/getServerTime doe ...

  8. Myeclipse2017 安装反编译插件和SVN插件

    亲测有效 2018年1月22日10:36:33 https://www.cnblogs.com/liuyk-code/p/7519886.html

  9. mybatis实现一对多连接查询

    问题:两个对象User和Score,它们之间的关系为一对多. 底层数据库为postgresql,ORM框架为mybatis. 关键代码如下: mybatis配置文件如下: mybatis.xml文件内 ...

  10. javascript 模拟java 实现继承的5种方式

    1.继承第一种方式:对象冒充 function Parent(username){ this.username = username; this.hello = function(){ alert(t ...