FZU-2150.FireGame.(BFS))
本题大意:给出一个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))的更多相关文章
- Fire Game FZU - 2150 (bfs)
Problem 2150 Fire Game Accept: 3772 Submit: 12868Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 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(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- (广搜)Fire Game -- FZU -- 2150
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS ...
- fzu 2150 Fire Game 【身手BFS】
称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...
- (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)
Problem 2150 Fire Game Accept: 2133 Submit: 7494Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- FZU 2150 Fire Game(BFS)
点我看题目 题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的.问你最少花多少时间 ...
随机推荐
- PHP 扩展在 Linux(centos7)系统下的编译与安装 以 mysqli 为例
(操作系统 Centos7,环境版本 php7) 01,进入到 PHP 解压后的源码包的的 ext 文件夹 02,查看是否存在 mysqli 扩展 => ls, 如果不存在需要去响应网站下载 ( ...
- mybatis 3.2.2_环境搭建
1.创建一个工程 utf-8 2.导入jar mybatis-3.2.2.jar 核心包 依赖包: asm-3.3.1.jar cglib-2.2.2.jar commons-logging-1.1. ...
- APPium-Xpath,swipe练习
写自动化测试,实现 滚动到 口碑最佳 部分,并且打印出所有 口碑最佳 部分的5个应用名称 # coding:utf-8from appium import webdriverimport time d ...
- js检查字符串的包含关系
首先想到的是str.contains(),用的时候发现报错了 正确的判断方法应该用:str.indexof(substring),不包含的话,返回-1.大小写敏感 var rsp = response ...
- jsfl 常用自定义方法
//创建文件夹 function creatFile(fileURl) { if (FLfile.createFolder(fileURl)) { //alert("创建成功 "+ ...
- Others-常用数学符号大全
常用数学符号读法大全 大写 小写 英文注音 国际音标注音 中文注音 Α α alpha alfa 阿耳法 Β β beta beta 贝塔 Γ γ gamma gamma 伽马 Δ δ deta de ...
- vue.js 作一个用户表添加页面----初级
使用vue.js 制作一个用户表添加页面,实际上是把原来需要使用js写的部分,改写成vue.js的格式 首先,想象一下,先做思考,我们要添加用户表,设涉及到哪些数据,一个是用户id,一个是用户名,一个 ...
- jquery接触初级----jquery 选择器
css 选择器主要有:元素选择器,ID选择器,类选择器,群组选择器,后代选择器,普通配符选择器等,通过css选择,我们可以很方便的给元素添加样式,使网页看起来更加好看 jquery 选择器也有相似的功 ...
- unity Flash Animation Toolset插件使用
插件网站:http://matov.me/flash-animation-toolset/ 1.在unity上打开资源商店,Window -> Asset Store -> 搜索Flash ...
- vscode 不显示指定后缀名pyc文件
不显示python生成的pyc文件 不显示java eclipse编辑器生成的.metadata生成的文件夹 py文件执行后会生成.pyc文件,会影响侧边栏的使用,可以通过如下设置隐藏.pyc等中间文 ...