救援行动(save)

题目描述

Angel被人抓住关在一个迷宫了!迷宫的长、宽均不超过200,迷宫中有不可以越过的墙以及监狱的看守。Angel的朋友带了一个救援队来到了迷宫中。他们的任务是:接近Angel。我们假设接近Angel就是到达Angel所在的位置。
假设移动需要1单位时间,杀死一个看守也需要1单位时间。到达一个格子以后,如果该格子有看守,则一定要杀死。交给你的任务是,最少要多少单位时间,才能到达Angel所在的地方(只能向上、下、左、右4个方向移动)?

输入

第1行两个整数n,m。表示迷宫的大小为n×m。
以后n行,每行m个字符。其中“#”代表墙,“.”表示可以移动,“x”表示看守,“a”表示Angel,“r”表示救援队伍。字母均为小写。

输出

l行,代表救出Angel的最短时间。如果救援小组永远不能达到Angel处,则输出“NO ANSWER”。

样例输入

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

样例输出

13
分析:bfs,先走当前拜访时间小的;
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include <queue>
#define ll long long
#define rep(i,m,n) for(i=m;i<=n;i++)
#define inf 0x3f3f3f3f
const int maxn=2e2+;
using namespace std;
int n,m,vis[maxn][maxn];
const int dis[][]={,,,,-,,,-};
char a[maxn][maxn];
bool flag;
void dfs(int x,int y)
{
priority_queue<pair<int,pair<int,int> >,vector<pair<int,pair<int,int> > >, greater<pair<int,pair<int,int> > > >p;
vis[x][y]=;p.push(make_pair(,make_pair(x,y)));
while(!p.empty())
{
pair<int,pair<int,int> > b=p.top();p.pop();
for(int i=;i<;i++)
{
int l=b.second.first+dis[i][],r=b.second.second+dis[i][];
if(l>=&&l<n&&r>=&&r<m&&!vis[l][r]&&a[l][r]!='#')
{
vis[l][r]=vis[b.second.first][b.second.second]++(a[l][r]=='x');
p.push(make_pair(vis[l][r],make_pair(l,r)));
if(a[l][r]=='a')return;
}
}
}
}
int main()
{
int i,j,k,t;
scanf("%d%d",&n,&m);
rep(i,,n-)scanf("%s",a[i]);
rep(i,,n-)rep(j,,m-)
{
if(a[i][j]=='r'){dfs(i,j);break;}
}
rep(i,,n-)rep(j,,m-)if(a[i][j]=='a')goto loop;
loop:;
if(vis[i][j]!=)printf("%d\n",vis[i][j]-);
else puts("NO ANSWER");
//system("pause");
return ;
}

救援行动(save)的更多相关文章

  1. 救援行动(save) (BFS)

    时间限制: 1 Sec  内存限制: 64 MB提交: 42  解决: 9[提交][状态][讨论版] 题目描述 Angel被人抓住关在一个迷宫了!迷宫的长.宽均不超过200,迷宫中有不可以越过的墙以及 ...

  2. Backbone中的model和collection在做save或者create操作时, 如何选择用POST还是PUT方法 ?

    Model和Collection和后台的WEB server进行数据同步非常方便, 都只需要在实行里面添加一url就可以了,backbone会在model进行save或者collection进行cre ...

  3. redis-内存异常 Redis is configured to save RDB snapshots解决

    连接reids获取数据时提示 Redis is configured to save RDB snapshots, but is currently not able to persist on di ...

  4. (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk

    今天运行Redis时发生错误,错误信息如下: (error) MISCONF Redis is configured to save RDB snapshots, but is currently n ...

  5. Money, save or spend, this is a problem .

    Win a lottery? Had a great hand at the casino? Did fortune shine upon you in the stock market? 彩票中了大 ...

  6. canvas的save与restore方法的作用

    网上搜罗了一堆资料,最后总结一下. save:用来保存Canvas的状态.save之后,可以调用Canvas的平移.放缩.旋转.错切.裁剪等操作. restore:用来恢复Canvas之前保存的状态. ...

  7. Unity 好坑的Save Scene

    在编辑一个Untiy工程的时候,有很多的教程提到了 "Save Scene",也知道是干么用的.但是,后面打开工程的时候,工程界面是很多东西都不见了,又忘了有个Save Scene ...

  8. spring spring data jpa save操作事务

    整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...

  9. Hibernate Session中的save(),update(),delete(),saveOrUpdate() 细粒度分析

    Hibernate在对资料库进行操作之前,必须先取得Session实例,相当于JDBC在对资料库操作之前,必须先取得Connection实例, Session是Hibernate操作的基础,它不是设计 ...

随机推荐

  1. 将dom4j格式化为标准的xml字符串

    StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); fo ...

  2. Python2中while 1比while True更快

    1) bool类是从int类继承而来的 2) True/False 在python2中不是关键字,但是在python3是(True,False,None) PS > python2 Enthou ...

  3. Apriori算法-位运算-C语言

    原文地址:http://blog.csdn.net/liema2000/article/details/6118423 //////////////////////////////////////// ...

  4. 1.5后台修改添加TDK

    manager\includes\languages\english.php //注意 是后台的语言包define('BOX_CONFIGURATION_Lin_STORE', 'TDKcss_set ...

  5. html/css技巧总结

    .e-select .on{display:none} on为e-select的子元素,(之间有空格).e-select.on{display:block} 只有两种属性同时存在时才会起作用(之间无空 ...

  6. JS原型和原型链

        1 var decimalDigits = 2, 2 tax = 5; 3 4 function add(x, y) { 5 return x + y; 6 } 7 8 function su ...

  7. PHP: 异常exception

    异常最常见于SDK调用中,函数执行失败时抛出异常,顺带错误码和错误信息. 先来看下PHP的异常处理相关函数: public Exception::__construct() ([ string $me ...

  8. 使用SpringSecurity3用户验证(异常信息,验证码)

    1. 自定义user-service后,封装自定义异常信息返回 通常情况下,抛UsernameNotFoundException异常信息是捕捉不了,跟踪源码后发现 try { user = retri ...

  9. Codeforces Round #372 (Div. 2) C 数学

    http://codeforces.com/contest/716/problem/C 题目大意:感觉这道题还是好懂得吧. 思路:不断的通过列式子的出来了.首先我们定义level=i, uplevel ...

  10. Unable to open connection to supplicant on "/data/misc/wifi/sockets/wlan0"

    在调试android wifi UI 的时候,出现了 logcat:  Unable to open connection to supplicant on "/data/misc/wifi ...