BFS(两点搜索) FZOJ 2150 Fire Game
题意:'#'表示草地,两个人在草地上点火,相邻的草地会烧起来,每烧一格等1秒,问最少要等几秒草地才烧完
分析:这题和UVA 11624 Fire!有点像,那题给定了两个点,这题两点不确定,取最小值。可以仿照11624的做法,两次BFS,第二次更新最小值,这样我跑了900多ms。后来发现不需要这样,枚举两个点,将它们一起入队,vis过的点一定已经是最优的,广搜的特点。
收获:1. 进一步理解BFS的过程 2. 差错能力有待提高 3. 写过的题一定要总结!(刚刷过专题!!!)
代码:
/************************************************
* Author :Running_Time
* Created Time :2015-8-23 12:40:49
* File Name :J.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 12;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Node {
int x, y, step;
};
Node g[N*N];
char maze[N][N];
int d[N][N];
bool vis[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int n, m, cnt, res; bool judge(int x, int y) {
if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || maze[x][y] != '#') return false;
return true;
} void BFS(int sx, int sy, int ex, int ey) {
queue<Node> Q;
Q.push ((Node) {sx, sy, 0});
Q.push ((Node) {ex, ey, 0});
vis[sx][sy] = vis[ex][ey] = true;
while (!Q.empty ()) {
Node r = Q.front (); Q.pop ();
int x = r.x, y = r.y, step = r.step;
res = max (res, step);
for (int i=0; i<4; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (!judge (tx, ty)) continue;
vis[tx][ty] = true;
Q.push ((Node) {tx, ty, step + 1});
}
}
} bool check(void) {
for (int i=1; i<=cnt; ++i) {
int x = g[i].x, y = g[i].y;
if (!vis[x][y]) return false;
}
return true;
} int work(void) {
int ans = INF;
if (cnt <= 2) return 0;
for (int i=1; i<=cnt; ++i) {
for (int j=i+1; j<=cnt; ++j) {
memset (vis, false, sizeof (vis)); res = 0;
BFS (g[i].x, g[i].y, g[j].x, g[j].y);
if (check ()) ans = min (ans, res);
}
} if (ans == INF) return -1;
else return ans;
} int main(void) {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
cnt = 0;
scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf ("%s", maze[i] + 1);
for (int j=1; j<=m; ++j) {
if (maze[i][j] == '#') {
g[++cnt].x = i; g[cnt].y = j;
}
}
} printf ("Case %d: %d\n", ++cas, work ());
} return 0;
}
BFS(两点搜索) FZOJ 2150 Fire Game的更多相关文章
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- FZOJ Problem 2150 Fire Game
...
- 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 - 题目描述 ...
- BFS广度优先搜索 poj1915
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...
- 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想
dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...
- 图的遍历BFS广度优先搜索
图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...
随机推荐
- Window Pains(poj 2585)
题意: 一个屏幕要同时打开9个窗口,每个窗口是2*2的矩阵,整个屏幕大小是9*9,每个窗口位置固定. 但是是否被激活(即完整显示出来)不确定. 给定屏幕状态,问是否可以实现显示. 分析:拓扑排序,把完 ...
- struts2开发action 的三种方法以及通配符、路径匹配原则、常量
struts2开发action 的三种方法 1.继承ActionSupport public class UserAction extends ActionSupport { // Action中业务 ...
- SQL SERVER示例:修改自定义数据类型精度
/*--修改自定义数据类型精度的示例 自定义数据类型一旦被引用,就不能再修改和删除,如果要修改数据的精度,就非常麻烦,下面的示例演示了如何修改 假设要修改的自定义变量名为aa -- ...
- 洛谷 P3984 高兴的津津
P3984 高兴的津津 题目描述 津津上高中了.她在自己的妈妈的魔鬼训练下,成为了一个神犇,每次参加一次OI比赛必拿Au虐全场.每次她拿到一个Au后就很高兴.假设津津不会因为其它事高兴,并且她的高兴会 ...
- Mycat环境搭建教程收集(待实践)
先收集,后续再实践. http://blog.csdn.net/dreamcode/article/details/44307377 http://blog.csdn.net/lanonola/art ...
- 总结 React 组件的三种写法 及最佳实践
React 专注于 view 层,组件化则是 React 的基础,也是其核心理念之一,一个完整的应用将由一个个独立的组件拼装而成. 截至目前 React 已经更新到 v15.4.2,由于 ES6 的普 ...
- Alluxio增强Spark和MapReduce存储能力
Alluxio的前身为Tachyon.Alluxio是一个基于内存的分布式文件系统:Alluxio以内存为中心设计,他处在诸如Amazon S3. Apache HDFS 或 OpenStack Sw ...
- kvc VS kvo
Kvo是Cocoa的一个重要机制,它主要是用于对一个属性的新旧值的监控. 例如说依据A(数 据类)的某个属性值变化,B(view类)中的某个属性做出对应变化.对于MVC,kvo应用的地方很广泛. 使用 ...
- 关于使用Xshell远程连接启动tomcat导致图片不显示,报错Could not initialize class sun.awt.X11GraphicsEnvironment解决方案
如果您是使用xshell远程启动tomcat,导致二维码.验证码,头像等各种图片不显示,并且打开图片链接报错Could not initialize class sun.awt.X11Graphics ...
- train_action
# 导入数值计算模块 import numpy as np import tensorflow as tf # 创建计算会话 sess = tf.Session() # 生成数据,创建占位符和变量A ...