题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过‘x’方格须要两秒  '#'表示墙

因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断

令到达每一个点的时间初始为无穷大  当从一个点到达该点用的时间比他本来的时间小时  更新这个点的时间并将这个点入队  扫描全然图就得到答案咯

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; const int N = 205;
char mat[N][N];
int time[N][N], sx, sy;
int dx[4] = {0, 0, -1, 1};
int dy[4] = { -1, 1, 0, 0};
struct grid
{
int x, y;
grid(int xx = 0, int yy = 0): x(xx), y(yy) {}
}; void bfs()
{
memset(time, 0x3f, sizeof(time));
time[sx][sy] = 0;
queue<grid> g;
g.push(grid(sx, sy)); while(!g.empty())
{
grid cur = g.front();
g.pop();
int cx = cur.x, cy = cur.y, ct = time[cx][cy];
for(int i = 0; i < 4; ++i)
{
int nx = cx + dx[i], ny = cy + dy[i];
if(mat[nx][ny] && mat[nx][ny] != '#')
{
int tt = ct + 1;
if(mat[cx][cy] == 'x') ++tt;
if(tt < time[nx][ny])
{
time[nx][ny] = tt;
g.push(grid(nx, ny));
}
}
}
}
} int main()
{
int n, m, ex, ey;
while (~scanf("%d%d", &n, &m))
{
memset(mat, 0, sizeof(mat));
for(int i = 1; i <= n; ++i)
scanf("%s", mat[i] + 1);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if(mat[i][j] == 'a') sx = i, sy = j;
else if(mat[i][j] == 'r') ex = i, ey = j;
bfs();
if(time[ex][ey] != time[0][0])
printf("%d\n", time[ex][ey]);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
} return 0;
}

ZOJ 1649 Rescue(有敌人迷宫BFS)的更多相关文章

  1. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  2. zoj 1649 Rescue

    BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...

  3. BFS zoj 1649

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...

  4. ZOJ 1649:Rescue(BFS)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  5. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  6. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. zoj 1649 bfs

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...

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

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

  9. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

随机推荐

  1. SqlMap常用参数(一)

    sqlmap可谓是利用sql注入的神器了,sqlmap的参数很多,接下介绍几种常见的参数. 一.注入access数据库常用的参数 sqlmap.py -u "url"  //判断参 ...

  2. 微信公众号开发之文本消息自动回复,以及系统关注自动回复,php代码

    以tshop为例 直接上代码: 企业 cc_wx_sys表为自建,存储系统消息的配置的 字段: id type key status <?php /** * tpshop * ========= ...

  3. windows系统下nodejs安装、环境配置及删除NPM全局配置

    nodejs安装及设置NPM全局路径 删除NPM全局路径配置 一.nodejs安装及设置NPM全局路径 第一步:下载安装文件 下载nodejs,官网:http://nodejs.org/downloa ...

  4. 5.17从零开始搭建springboot-dubbo的例子

    https://www.cnblogs.com/baijinqiang/p/10848259.html

  5. php获取当地时间 time zone

      PHP5.2.4之前的版本无需设置时区.下面是修改PHP时区的三个办法. 1.修改PHP.ini这个文件 找到date.timezone这行,去掉前面的分号,改成: Java代码 date.tim ...

  6. js基础---数组方法

    数组数据的排序及去重 sort无形参的排序方式 arr1=[2,12,3,15]; var a=arr1.sort();console.log(arr1);console.log(a);//排序会改变 ...

  7. REST、RESTful、SOA

    1.http://www.imooc.com/article/17650 2.SOA面向服务架构

  8. openMSP430之io_test

    openMSP430: IO functionality test with interupt #include "omsp_system.h" volatile char shi ...

  9. SQL Server对数据进行添加

    下面介绍一下SQL Server如何往数据库中加入数据. <!-- 添加主人信息 --> <form action="add_Admin.jsp" method= ...

  10. Package和Activity

    Package Package 包.只是在我们的app中这个Package是唯一的,就像你身份证号码一样.在我们做app自动化时,我们就需要知道他的Package,我们知道了Package那么也就知道 ...