BFS(两点搜索) UVA 11624 Fire!
/*
BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界。
*/
/************************************************
Author :Running_Time
Created Time :2015-8-4 8:11:54
File Name :UVA_11624.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 MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
int step[MAXN][MAXN];
int dx[] = {-, , , };
int dy[] = {, , -, };
int n, m; bool judge_f(int x, int y) {
if (x < || x > n || y < || y > m || vis[x][y] || maze[x][y] == '#') return false;
return true;
} bool judge_j(int x, int y) {
if (x < || x > n || y < || y > m) return true;
return false;
} void BFS_F(void) {
memset (step, INF, sizeof (step));
memset (vis, false, sizeof (vis));
queue<pair<int, int> > Q;
for (int i=; i<=n; ++i) {
for (int j=; j<=m; ++j) {
if (maze[i][j] == 'F') {
Q.push (make_pair (i, j)); vis[i][j] = true;
step[i][j] = ;
}
}
}
while (!Q.empty ()) {
int x = Q.front ().first, y = Q.front ().second; Q.pop ();
for (int i=; i<; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (!judge_f (tx, ty)) continue;
step[tx][ty] = step[x][y] + ;
Q.push (make_pair (tx, ty)); vis[tx][ty] = true;
}
}
} void BFS_J(void) {
memset (vis, false, sizeof (vis));
queue<pair<int, int> > Q;
for (int i=; i<=n; ++i) {
for (int j=; j<=m; ++j) {
if (maze[i][j] == 'J') {
Q.push (make_pair (i, j)); step[i][j] = ; vis[i][j] = true; break;
}
}
}
while (!Q.empty ()) {
int x = Q.front ().first, y = Q.front ().second; Q.pop ();
for (int i=; i<; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (judge_j (tx, ty)) {
printf ("%d\n", step[x][y] + ); return ;
}
if (step[x][y] + >= step[tx][ty] || vis[tx][ty] || maze[tx][ty] == '#') continue;
Q.push (make_pair (tx, ty)); step[tx][ty] = step[x][y] + ; vis[tx][ty] = true;
}
}
puts ("IMPOSSIBLE");
} int main(void) { //UVA 11624 Fire!
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
for (int i=; i<=n; ++i) {
scanf ("%s", maze[i] + );
}
BFS_F (); BFS_J ();
} return ;
}
BFS(两点搜索) UVA 11624 Fire!的更多相关文章
- BFS(两点搜索) FZOJ 2150 Fire Game
题目传送门 题意:'#'表示草地,两个人在草地上点火,相邻的草地会烧起来,每烧一格等1秒,问最少要等几秒草地才烧完 分析:这题和UVA 11624 Fire!有点像,那题给定了两个点,这题两点不确定, ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVA 11624 Fire!(广度优先搜索)
题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- uva 11624 Fire!(搜索)
开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...
随机推荐
- MySQL查看表结构及查看建表语句
查看表结构:desc 表名 mysql> use recommend; Database changed mysql> desc user; +--------------+------- ...
- Linux下汇编语言学习笔记43 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- 调用系统文件管理器选择图片,调用系统裁剪AIP对图片处理,显示裁剪之后的图片
package com.pingyijinren.test; import android.annotation.TargetApi; import android.app.Notification; ...
- python之模块随笔记-os
操作系统模块:import os os.remove() 删除文件 os.unlink() 删除链接文件 os.rename() 重命名文件 os.listdir() 列出指定目录下所有文件 os.c ...
- SecureCRT在Tab下的Title显示IP
注意:要针对每个Session进行修改才行. 参考: http://www.cnblogs.com/tyhmj/archive/2013/12/20/3483247.html
- php7.3源码编译
前置条件 autoconf:2.59+ automake:1.4+ libtool:1.4.x(除了1.4.2) re2c:0.13.4+ bison:建议1.281.35 或 1.75 依赖的库 y ...
- Windows如何安装MSMQ消息队列
1 打开控制面板,找到下图所示的服务器核心,然点击确定 2 等待安装完成
- 使用shell分页读取600万+的MySQL数据脚本
shell-mysql 脚本背景 因为要在Linux上.远程读取mysql的表的数据,然后做一定清洗后.把数据上传至Hadoop集群中,使用Java写吧,感觉太麻烦了.得在Win上开发好,还得打成ja ...
- Datagridview CurrentRow.Index
int index = dataGridView1.CurrentRow.Index; //获取当前选择行引导 string str = dataGridView1.Rows[index].Cell ...
- ≪统计学习精要(The Elements of Statistical Learning)≫课堂笔记(三)
照例文章第一段跑题,先附上个段子(转载的哦~): I hate CS people. They don't know linear algebra but want to teach projecti ...