UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671
首先对火进行一次bfs,得到着火时间,然后对人进行一次bfs,只允许进入还没有着火的点
注意:出迷宫条件是从任何一墙出去,过墙需要1时间
- #include <cstdio>
- #include <cstring>
- #include <queue>
- using namespace std;
- const int maxn=1003;
- const int inf=0x3fffffff;
- char maz[maxn][maxn];
- int time[maxn][maxn];
- int dp[maxn][maxn];
- int n,m;
- queue<int> que;
- const int dx[8]={0,0,1,-1,1,1,-1,-1};
- const int dy[8]={1,-1,0,0,1,-1,1,-1};
- struct pnt {
- int x,y;
- pnt(){x=y=0;}
- pnt(int tx,int ty){x=tx,y=ty;}
- };
- bool in(int x,int y){
- return x>=0&&x<n&&y>=0&&y<m;
- }
- void bfs(){
- while(!que.empty()){
- int x=que.front()/maxn,y=que.front()%maxn;que.pop();
- for(int i=0;i<4;i++){
- int tx=x+dx[i],ty=y+dy[i];
- if(in(tx,ty)&&maz[tx][ty]!='#'&&time[tx][ty]>time[x][y]+1){
- time[tx][ty]=time[x][y]+1;
- que.push(tx*maxn+ty);
- }
- }
- }
- }
- int bfs2(int sx,int sy){
- while(!que.empty())que.pop();
- dp[sx][sy]=0;
- que.push(sx*maxn+sy);
- while(!que.empty()){
- int x=que.front()/maxn,y=que.front()%maxn;que.pop();
- for(int i=0;i<4;i++){
- int tx=x+dx[i],ty=y+dy[i];
- if(!in(tx,ty)){
- return dp[x][y]+1;
- }
- if(maz[tx][ty]!='#'&&time[tx][ty]>dp[x][y]+1&&dp[tx][ty]>dp[x][y]+1){
- dp[tx][ty]=dp[x][y]+1;
- que.push(tx*maxn+ty);
- }
- }
- }
- return -1;
- }
- void init(){
- while(!que.empty())que.pop();
- for(int i=0;i<n;i++){
- for(int j=0;j<m;j++){
- time[i][j]=inf;
- dp[i][j]=inf;
- }
- }
- }
- int main(){
- int T;
- scanf("%d",&T);
- for(int ti=1;scanf("%d%d",&n,&m)==2&&ti<=T;ti++){
- for(int i=0;i<n;i++){
- scanf("%s",maz[i]);
- }
- init();
- int sx,sy;
- for(int i=0;i<n;i++){
- for(int j=0;j<m;j++){
- if(maz[i][j]=='F'){
- que.push(i*maxn+j);
- time[i][j]=0;
- }
- else if(maz[i][j]=='J'){
- sx=i;
- sy=j;
- }
- }
- }
- bfs();
- int ans=bfs2(sx,sy);
- if(ans!=-1)printf("%d\n",ans);
- else puts("IMPOSSIBLE");
- }
- return 0;
- }
UVA 11624 Fire! bfs 难度:0的更多相关文章
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVA - 11624 多点bfs [kuangbin带你飞]专题一
题意:某人身陷火场,总有k个点着火,着火点可向四周扩散,问此人能否逃离. 思路:可能有多个着火点,以这些着火点作为起点进行bfs,得到整个火场的最短距离,然后又以人所在坐标作为起点进行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 ...
随机推荐
- DNS中A记录和CNAME记录的区别(转)
A记录是域名到ip的映射,即为ip起别名:CNAME是域名别名到域名的映射,即为域名起别名. 还有一个常用的记录是MX记录,它是与邮件相关的,MX记录记录了发送电子邮件时域名对应的服务器地址. 原文: ...
- 使用Navicat连接Mysql报错:can not get hostname for your address
以管理员的身份使用cmd命令运行netsh winsock reset即可!
- Mirror--镜像相关操作
其他相关操作1. 关闭镜像--关闭镜像USE [master]GOALTER DATABASE Demo1 SET PARTNER OFFGO 2. 证服务器--移除见证服务器USE [master ...
- 百度天气接口api
百度天气接口 以GET形式提交,返回JSON或XML URL:http://api.map.baidu.com/telematics/v3/weather?location={城市名}&out ...
- Underscore.js (1.7.0)-函数预览
集合(Collections)(25) - each - map - reduce - reduceRight - find - filter - where - findWhere - reject ...
- PAT 1110 Complete Binary Tree[判断完全二叉树]
1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...
- Deep Learning(1)
深度学习是机器学习研究中的一个新的领域,其动机在于建立.模拟人脑进行分析学习的神经网络,它模仿人脑的机制来解释数据,例如图像,声音和文本.深度学习是无监督学习的一种. 深度学习的概念源于人工神经网络的 ...
- web前端几个小知识点笔记
1.css实现宽度是百分比的盒子为正方形 <div style="width:50%;padding-bottom:50%;height:0px;background:#ccc;&qu ...
- .net core 2.2 & Mongodb
.net core 2.2 API项目中使用Mongodb 简单的CRUD封装 创建FoodPlan.Core 项目 创建IEntityBase.cs 接口约束 创建Single.cs 实体 IEnt ...
- Cooperation.GTST团队第四周项目总结
项目进展 这周我们的主要学习内容是: 1.研究学习如何导入博客详情页. 2.继续研究如何使用博客园的相关接口,导入相关jar包实现页面整体效果: 在我们使用其它APP或者上网浏览论坛.网页等时,通常都 ...