题目链接

题意:帮助joe走出一个大火蔓延的迷宫,其中joe每分钟可往上下左右四个方向之一走,所有着火的格子都会蔓延(空格与着火格有公共边,下一分钟这个空格也会着火)。迷宫中有一些障碍格,joe和火都无法进入,当joe走到一个边界的格子我们认为他走出了迷宫

输出R行C列的迷宫,#表示墙,.表示空地,J表示joe,F是着火格

如果能走出,输出最少时间否则,impossible

分析:不知道怎么处理大火蔓延这个东西,看了书上的方法,太棒了,就是对所以得着火点进行以bfs,就可以求出到蔓延到每一个格子的时间了,然后joe走的时候就看看他到这个格子的时候,时间到没到火蔓延的时候

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int Max = + ;
int fire[Max][Max],g[Max][Max],vis[Max][Max];
int R,C;
int sx, sy;
int gx[] = {, , , -};
int gy[] = {-, , , };
int Time;
struct Node
{
int x,y;
};
queue<Node> Fire;
void solve(int i, int j, char ch)
{
if(ch == '#')
{
g[i][j] = -;
vis[i][j] = INF;
fire[i][j] = INF;
}
else if(ch == '.')
{
g[i][j] = ;
vis[i][j] = INF;
fire[i][j] = INF;
}
else if(ch == 'J')
{
sx = i;
sy = j;
vis[i][j] = ;
fire[i][j] = INF;
}
else if(ch == 'F')
{
fire[i][j] = ;
vis[i][j] = INF;
g[i][j] = ;
Node node;
node.x = i;
node.y = j;
Fire.push(node);
}
}
void fire_bfs() // 对大火蔓延的bfs
{
while(!Fire.empty())
{
Node node = Fire.front();
Fire.pop();
for(int i = ; i < ; i++)
{
int fx = node.x + gx[i];
int fy = node.y + gy[i];
if(fx >= && fx <= R && fy >= && fy <= C && g[fx][fy] != -)
{
if(fire[fx][fy] > fire[node.x][node.y] + )
{
fire[fx][fy] = fire[node.x][node.y] + ;
Node temp;
temp.x = fx;
temp.y = fy;
Fire.push(temp);
}
}
}
}
}
void road_bfs()
{
queue<Node> Road;
Node node;
node.x = sx;
node.y = sy;
Road.push(node);
while(!Road.empty())
{
node = Road.front();
Road.pop();
if(node.x == || node.x == R || node.y == || node.y == C)
{
Time = vis[node.x][node.y];
return;
}
for(int i = ; i < ; i++)
{
int fx = node.x + gx[i];
int fy = node.y + gy[i];
if(fx >= && fy >= && fx <= R && fy <= C && g[fx][fy] != -)
{
if(vis[fx][fy] > vis[node.x][node.y] + && vis[node.x][node.y] + < fire[fx][fy])
{
vis[fx][fy] = vis[node.x][node.y] + ;
Node temp;
temp.x = fx;
temp.y = fy;
Road.push(temp);
}
}
}
}
}
int main(int argc, char** argv)
{
int test;
scanf("%d", &test);
while (test--)
{
while(!Fire.empty())
Fire.pop();
scanf("%d%d", &R, &C);
getchar();
char ch;
for(int i = ; i <= R; i++)
{
for(int j = ; j <= C; j++)
{
scanf("%c", &ch);
solve(i, j, ch);
}
getchar();
}
Time = INF;
fire_bfs();
road_bfs();
if(Time != INF)
printf("%d\n", Time + );
else
printf("IMPOSSIBLE\n"); }
return ;
}

UVA11624Fire!(BFS)的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. 解决问题:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。

    WindowServer2012服务器,添加角色安装完.netframework和iis之后,运行aspx页面就报如下错误: HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法 ...

  2. [poj 3537]Crosses and Crosses(博弈论)

    题目:http://poj.org/problem?id=3537 题意:给你n个格子,两个人依次在n个格子的任意空位置画"X",谁如果画了一个后,3个X连在了一起,那么那个人就获 ...

  3. mybatis缓存学习笔记

    mybatis有两级缓存机制,一级缓存默认开启,可以在手动关闭:二级缓存默认关闭,可以手动开启.一级缓存为线程内缓存,二级缓存为线程间缓存. 一提缓存,必是查询.缓存的作用就是查询快.写操作只能使得缓 ...

  4. mailto: HTML e-mail 链接

    转载: http://www.haorooms.com/post/mailto_link_html 什么是mailto链接? mailto链接是一种html链接,能够设置你电脑中邮件的默认发送信息.但 ...

  5. fio 2种画图方法 fio_generate_plots 和 gfio

    fio 安装fio apt-get install fio 可以把fio的输出数据自动画图的插件gnuplot apt-get install gnuplot 1.输出bw,lat和iops数据并画图 ...

  6. ubuntu eclipse 不能新建javaweb项目解决方案

    ubuntu下,通过sudo apt-get install eclipse 成功安装了eclipse,可它简洁的都让我不知如何新建web project.网上查了众多资料,终于找到了一系列简洁的方法 ...

  7. [转] java中int,char,string三种类型的相互转换

    原文地址:http://blog.csdn.net/lisa0220/article/details/6649707 如何将字串 String 转换成整数 int? int i = Integer.v ...

  8. C#-WebForm-★ 制作图片验证码 ★

    在前台放在如下四个控件 <div> <asp:TextBox ID="TextBox1" runat="server"></asp ...

  9. 使用jmeter进行性能测试-Jmeter教程及技巧汇总 (转)

    http://www.jmeter.cf/loadtesting-jmeter.html 为什么使用jmeter, 它免费开源, 不断发展, 功能逐渐强大. 可以做功能,负载, 性能测试.一套脚本可以 ...

  10. C# 定时器运用

    在晚上12点执行任务 using System;using System.Collections.Generic;using System.ComponentModel;using System.Da ...