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[][]是多余的,完全可以用 ...
随机推荐
- Spring boot 搭配 JPA 生成表注释 和 字段注释
原文地址:https://blog.csdn.net/qq_39996837/article/details/84717748 由于在数据库表反向生成过程中呢,需要通过jpa自动生成表,并且这个表必须 ...
- 【ZJOI2017 Round1练习】D7T1 graph(提答)
题意: n<=1000 m<=10000 思路:
- 使用MediaPlayer播放、暂停、停止音乐
package com.pingyijinren.test; import android.media.MediaPlayer; import android.os.Environment; impo ...
- Minimum Depth of Binary Tree(二叉树DFS)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 技术杂记之:在阿里云centos7上部署JDK MYSQL TOMCAT
今日小编闲来无事,乘着公司新项目即将上线之际,在阿里云上整了一台centos作为测试机.原本以为一个小时搞定,结果还是花了一点小小时间.不管怎么说,记录下来,给各位小白当成课后甜点吧. 价格 先上价格 ...
- PLU Decomposition
PLU分解的优点是,能够将Ax=b的矩阵,转换成Ly=b, Ux = y 的形式.当我们改变系数矩阵b时,此时因为矩阵L和U均是固定 的,所以总能高效的求出矩阵的解. // LU.cpp : Defi ...
- 推荐IOS开发3个工具:Homebrew、TestFight、Crashlytics-b
1. Homebrew 什么是Homebrew? Homebrew is the easiest and most flexible way to install the UNIX tools App ...
- HDU 5428 分解质因数
The F ...
- pagefile.sys
pagefile.sys
- spring 之 IOC 依赖注入详解
当我们对一个javaBean进行实例化时,在原本的情况下我们会选择新建一个接口,然后进行实例化,为了进一步降低耦合度我们还会使用工厂模式进行封装. 例: 当我们想要去造,Chinese.America ...