先对火BFS一次,求出每个点的最小着火时间。

再对人BFS一次,求出走到边界的最少时间。

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n,m,bz[1005][1005],qihuo[1005][1005];
char map[1005][1005];
int h[4][2]={-1,0,1,0,0,-1,0,1};
struct point
{
int x,y,step;
};
queue<point> q;
void bfs_huo()
{
int i,j,x,y;
point t,tt;
while(!q.empty())
{
t=q.front(); q.pop();
for(i=0;i<4;i++)
{
x=t.x+h[i][0]; y=t.y+h[i][1];
if(x>=0&&x<n&&y>=0&&y<m&&(map[x][y]=='.'||map[x][y]=='J')&&!bz[x][y])
{
bz[x][y]=1;
tt.x=x; tt.y=y; tt.step=t.step+1; qihuo[x][y]=tt.step;
q.push(tt);
}
}
}
}
int bfs_men(point s)
{
int i,j,x,y;
point t,tt;
while(!q.empty()) q.pop();
q.push(s); bz[s.x][s.y]=1;
while(!q.empty())
{
t=q.front(); q.pop();
if(t.x<=0||t.x==n-1||t.y<=0||t.y==m-1) return t.step; //??磬?0
for(i=0;i<4;i++)
{
x=t.x+h[i][0]; y=t.y+h[i][1];
if(x>=0&&x<n&&y>=0&&y<m&&(map[x][y]=='.'||map[x][y]=='J')&&!bz[x][y])
{
if(qihuo[x][y]<=t.step+1) continue;
bz[x][y]=1;
tt.x=x; tt.y=y; tt.step=t.step+1;
q.push(tt);
}
}
}
return -1;
}
int main(int argc, char *argv[])
{
int nn,i,j;
point s,t;
cin>>nn;
while(nn--)
{
cin>>n>>m;
while(!q.empty()) q.pop();
memset(bz,0,sizeof(bz));
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cin>>map[i][j]; qihuo[i][j]=1000000000; //???
if(map[i][j]=='J') {s.x=i; s.y=j; s.step=0;}
if(map[i][j]=='F') {t.x=i; t.y=j; t.step=0; q.push(t); bz[i][j]=1; qihuo[i][j]=0;}
}
bfs_huo();
memset(bz,0,sizeof(bz));
int ans=bfs_men(s);
if(ans>=0) cout<<ans+1<<endl;
else cout<<"IMPOSSIBLE"<<endl;
}
return 0;
}

UVA 11624 Fire!(二次BFS)的更多相关文章

  1. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  2. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  3. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  4. UVA - 11624 Fire! 【BFS】

    题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...

  5. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  6. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  7. UVA 11624 Fire! (bfs)

    算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...

  8. (简单) UVA 11624 Fire! ,BFS。

    Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...

  9. UVA 11624 Fire! bfs 难度:0

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

随机推荐

  1. 关于js中return false、event.preventDefault()和event.stopPropagation()

    在平时项目中,如果遇到需要阻止浏览器默认行为,大家经常会用return false;和event.preventDefault()来阻止,但对它俩的区别还是有些一知半解,于是看了文档,查了些资料,在此 ...

  2. iOS UIKit:Navigation Controllers

    navigation controller是一种层次结构的container view controller,即其通过一个view controllers栈来管理内部的content view con ...

  3. centos6.7 install chrome

    1.yum仓库 (如果用rpm包安装 可以忽略此步) vim /etc/yum.repos.d/google-chrome.repo [google-chrome] name=google-chrom ...

  4. everything is nothing

    可以选ios,可以选择android ,可以选择javaee,因为不想让自己这段时间的努力没有一个完美的结局.最起码真的能做点东西出来,所以6.10--8.10,两个月把javaee实训的该准备.可以 ...

  5. php 计算一个字符串在另一个字符串中出现的次数

    <?php $text = 'This is a test'; echo strlen($text); // 14 echo substr_count($text, 'is'); // 2 // ...

  6. 如何:确定调用 ASP.NET 网页的方式

    如何:确定调用 ASP.NET 网页的方式 通常有必要了解调用 ASP.NET 网页的方式:是由原始请求 (HTTP GET).回发 (HTTP POST).来自其他页的跨页面发送 (HTTP POS ...

  7. UWP textbox 只能输入数字

    private void Testbox_TextChanged(object sender, TextChangedEventArgs e) {    var textbox = (TextBox) ...

  8. LINQ 101——约束、投影、排序

    什么是LINQ:LINQ 是一组 .NET Framework 扩展模块集合,内含语言集成查询.集合以及转换操作.它使用查询的本机语言语法来扩展 C# 和 Visual Basic,并提供利用这些功能 ...

  9. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  10. localStorage.ie6.js

    !window.localStorage && function() { window.localStorage = {}; var prefix = 'data-userdata' ...