uva11624 fire bfs 最短路

// uva11624 bfs最短路
// Fire 给你一个图,有一个起点'J',从任何边界走出去就算成功
// 但是,会有起火的地方,每秒火会上下左右四个方向蔓延,人不能
// 走有火的地方,问能不能逃出去,逃出去的最少步数是多少
//
// 我的思路就是先预处理出每个地方的火的燃烧的最开始的时间
// 这bfs就可以了,计为f[i][j];
// 之后的再走一遍最短路,用s[i][j]记录人所能达到的最早的时间
// 走不到为-1,则,最后只要判断边界上能走到的就ok了
// 优化就是当走的点的最少时间比着火的时间小的时候就就放进去
// 最后,就ac啦~
//
// 哎,wa了好几发了,先开始没有用s[i][j]记录最短时间,最后导致
// TLE了好几发,用自定义的队列也是TLE,猛然想到,如果当前已经走到的
// 点是最短的直接记录就ok了,不用再去走,这是一个重要的剪枝。
//
// 搜索的道路刚刚开始,继续练吧。。。

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L);

template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }
template<class T> inline T lowBit(const T& x) { return x&-x; }
template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }
template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; }

const int maxn = 1008;
char mp[maxn][maxn];
int n,m;
int f[maxn][maxn];
int s[maxn][maxn];
struct node {
	int x;
	int y;
//	int time;
};
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
node que[maxn*maxn*10*10];
void print(){
	for (int i=0;i<n;i++){
		for (int j=0;j<n;j++)
			printf("%d ",f[i][j]);
		puts("");
	}
	puts("");
}
void init(){
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
			f[i][j]=-1;

	//print();

	for (int i=0;i<n;i++)
		scanf("%s",mp[i]);

	//queue<node> q;
	int head = 0,tail=0;
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
			if (mp[i][j]=='F'){
				f[i][j]=0;
				que[tail++]=(node){i,j};
			}
	while(head<tail){
		node x = que[head++];
	//	f[x.x][x.y] = x.time;
		//q.push()
		//q.pop();
		for (int i=0;i<4;i++){
			int tx = x.x + dx[i];
			int ty = x.y + dy[i];
			if (tx>=0&&tx<n&&ty>=0&&ty<m&&mp[tx][ty]=='.'&&f[tx][ty]==-1){
				f[tx][ty]=f[x.x][x.y]+1;
				que[tail++]=(node){tx,ty};
			}
		}
	}

//	print();
}

void solve(){
	node now ;
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
			s[i][j]=-1;
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
			if (mp[i][j]=='J'){
				now.x = i;
				now.y = j;
				s[i][j]=0;
			}
//	now.time = 0;
//	queue<node> q;
	int head=0,tail=0;
	que[tail++]=now;
	int time = -1;
	while(head<tail){
		node x = que[head++];
	//	s[x.x][x.y]=x.time;
		//q.pop();
		if (x.x==0||x.x==n-1||x.y==0||x.y==m-1){
			time = s[x.x][x.y]+1;
			break;
		}
		for (int i=0;i<4;i++){
			int tx = x.x + dx[i];
			int ty = x.y + dy[i];
			if (s[tx][ty]!=-1)	continue;
			if (tx>=0 && tx<n && ty>=0 && ty<m && mp[tx][ty]=='.' && (s[x.x][x.y]+1<f[tx][ty] || f[tx][ty]==-1)){
				s[tx][ty] = s[x.x][x.y]+1;
				que[tail++]=(node){tx,ty};
			}
		}
	}
	if (time==-1){
		printf("IMPOSSIBLE");
	}else {
		printf("%d",time);
	}
	puts("");

}

int main() {
	int t;
	//freopen("G:\\Code\\1.txt","r",stdin);
	scanf("%d",&t);
	while(t--){
		init();
		solve();
	}
	return 0;
}

时间: 2024-08-05 15:16:50

uva11624 fire bfs 最短路的相关文章

BZOJ 1698 [Usaco2007 Feb]Lilypad Pond 荷叶池塘 BFS+最短路

题意:链接 **方法:**BFS+最短路 解析: 这道题还是挺有意思的. 第一个想法被卡,第一发是二分+bfs,但是这样的话,会有什么处理不了呢?到一个点的流量. 所以就得换想法. 不妨重新定义最短路. 把图中所有的0的点看做可行点,每一次搜出从该点可以到打的0点,然后将这个边权视作1. 即f[i][j]代表到i,j这点的最短路. 显然终点特判: 然后同时记录一下最短路的数量即可. 代码: #include <queue> #include <cstdio> #include &l

题解——逃离僵尸岛(BFS+最短路+虚拟节点)

题解--逃离僵尸岛(BFS+最短路+虚拟节点) 一道很巧妙的最短路问题,细节也要注意 题面 Description 小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家. 该国有N个城市,城市之间有道路相连.一共有M条双向道路.保证没有自环和重边. K个城市已经被僵尸控制了,如果贸然闯入就会被感染TAT...所以不能进入.由其中任意城市经过不超过S条道路就可以到达的别的城市,就是危险城市.换句话说只要某个没有被占城市到某个被占城市不超过s距离,就是危险. 小a住在1号城市,国际

uva11624 - Fire! 两次bfs

题目链接 Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe's location in the maze and which squares of the

UVA11624 Fire! BFS

11624 Fire!Joe works in a maze. Unfortunately, portions of the maze have caughton fire, and the owner of the maze neglected to create a fire escape plan.Help Joe escape the maze.Given Joe’s location in the maze and which squares of the maze areon fir

[UVa11624]Fire!(BFS)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671 题意:一个人在树林里要到出口,同时有几个火点在扩展.问这个人能不能跑出树林,最少多少步能跑出去. 先扩展人,再扩展火.在同一个队列里扩展,所以要记下接下来要扩展几个人或者火点. 1 #include <algorithm> 2 #include <iost

UVA11624(bfs)

题意:给一张图,有火源,有障碍物,剩下的是道路,火源在下一分钟能够让上下左右四个方向的道路也着火,告诉人的位置,问最短时间能逃出去的时间是多少: 思路:一个bfs用来求出所有的火源能蔓延到的地方,另一个bfs求最短路,本来我以为bfs只能求最短路: 超级源:有多个源头的时候,就把所有的都压进去,每个源头能蔓延到的地方都看作是新的源头,继续压进去,每个只能蔓延4个方向,直到空: 1 #include <iostream> 2 #include <cstdio> 3 #include

Uva 1600 Patrol Robot (BFS 最短路/DFS剪枝)

这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #include <bits/stdc++.h> using namespace std; struct Node{ int r; int c; int g; int cnt; Node(int r,int c,int g,int cnt):r(r),c(c),g(g),cnt(cnt){} }; int v

HDU2433 BFS最短路

Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2789    Accepted Submission(s): 939 Problem Description One day, Tom traveled to a country named BGM. BGM is a small country, but there ar

BFS(最短路) HDU 2612 Find a way

题目传送门 1 /* 2 BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 19:36:36 7 File Name :HDOJ_2612.cpp 8 ************************************************