uva 11624 Fire! 【 BFS 】
按白书上说的,先用一次bfs,求出每个点起火的时间
再bfs一次求出是否能够走出迷宫
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; const int maxn = ;
const int INF = << -;
int r,c;//r行,c列
int d[maxn][maxn];
int vis[maxn][maxn];
char g[maxn][maxn];
int sx,sy; int dir[][] = {,,-,,,,,-}; struct node{
int x,y;
int step;
}p[maxn]; queue<node> Q; void bfs1(){
memset(vis,,sizeof(vis));
while(!Q.empty()){
node v = Q.front();Q.pop();
for(int i = ;i < ;i++){
int xx = v.x + dir[i][];
int yy = v.y + dir[i][];
if(xx < || xx > r || yy < || yy > c || vis[xx][yy] || g[xx][yy] != '.') continue;
vis[xx][yy] = ;
d[xx][yy] = min(d[xx][yy],v.step+);
Q.push(node{xx,yy,v.step+});
}
}
} void bfs2(){
queue<node> q;
memset(vis,,sizeof(vis));
q.push(node{sx,sy,});vis[sx][sy] = ; while(!q.empty()){
node u = q.front();q.pop();
if(u.x == r || u.y == c || u.x == || u.y == ){
printf("%d\n",u.step + );
return;
}
for(int i = ;i < ;i++){
int xx = u.x + dir[i][];
int yy = u.y + dir[i][];
if(xx < || xx > r || yy < || yy > c || vis[xx][yy] || g[xx][yy] != '.') continue;
if(u.step + >= d[xx][yy]) continue;
vis[xx][yy] = ;
q.push(node{xx,yy,u.step+});
}
}
puts("IMPOSSIBLE");
} int main(){
int T;
scanf("%d",&T);
while(T--){
while(!Q.empty()) Q.pop();
scanf("%d %d",&r,&c);
for(int i = ;i <= r;i++){
for(int j = ;j <= c;j++) d[i][j] = INF;
}
for(int i = ;i <= r;i++){
for(int j = ;j <= c;j++) {
cin>>g[i][j];
if(g[i][j] == 'F') Q.push(node{i,j,});
if(g[i][j] == 'J') sx = i,sy = j;
}
}
bfs1();
bfs2();
}
return ;
}
加油~~~gooooooo~~
uva 11624 Fire! 【 BFS 】的更多相关文章
- UVA - 11624 Fire! 【BFS】
题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
- UVA - 11624 Fire! 双向BFS追击问题
Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...
- UVA_11624 Fire! 【BFS】
一.题面 略 二.题意分析 一个迷宫中,有一个人Joe和一个或多个起火点,起火点可以蔓延,人可以走动,都只能走4个方向,问人能走出去的最少步数,如果不能输出不可能.很多大佬说是两遍BFS,先一遍火,记 ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
随机推荐
- BZOJ 1264: [AHOI2006]基因匹配Match DP_树状数组_LCS转LIS
由于有重复数字,我们以一个序列为基准,另一个序列以第一个序列每个数所在下标为这个序列每个数对应的值. 注意的是,拆值的时候按照在第一个序列中的位置从大到小排,强制只能选一个. 最后跑一边最长上升子序列 ...
- 【udacity】机器学习-波士顿房价预测
import numpy as np import pandas as pd from Udacity.model_check.boston_house_price import visuals as ...
- 【XSY3347】串后缀
原题:2018 ICPC Asia-East Continent Final J 想看原题解的可以去看吉老师的直播题解 题意: 题解: (dllca膜你赛搬原题差评) 考虑题目中给出的式子的含义,实际 ...
- immutable-js基础
Immutable.js(和原生方法不同): 用于深层次的数组和对象的比较 数据结构:Map Set Seq List Rang(和原生不同) 首先:先忘记es5 es6的数组对象方法 官 ...
- 训练1-o
给出2个N * N的矩阵M1和M2,输出2个矩阵相乘后的结果. Input 第1行:1个数N,表示矩阵的大小(2 <= N <= 100)第2 - N + 1行,每行N个数,对应M1的1行 ...
- 训练1-T
一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,则称其为与7相关的数.求所有小于等于N的与7无关的正整数的平方和. 例如:N = 8,<= 8与7无关的数包括:1 2 ...
- --- Error: failed to execute '.\ARMCC\bin\ArmAsm'
1.KEIL4在开发STM32程序时报: Error: failed to execute '.\ARMCC\bin\ArmAsm' 或是Error: failed to execute '.\ARM ...
- JavaScript之Ajax技术
- APS.NET webform中的isPostBack
IsPostBack介绍Page.IsPostBack是一个标志:当前请求是否第一次打开. 调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者th ...
- arp与免费arp的差别,arp老化
免费arp:应用场景: case1:PC通过DHCP申请地址.在获取到IP地址后,会发送免费ARP,目的用于探測同一网段时候存在同样的IP地址终端,防止IP冲突. case2:PC的MAC地址发生变化 ...