题目链接 ZOJ链接

Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

 
Input
First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file.

 
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
 
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
 
Sample Output
13
 
题解:a是目标位置,r是起始位置,通过x需要多1s时间,如果走不到输出"Poor ANGEL has to stay in the prison all his life."。
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int n,m;
char mp[][];
int dir[][]= {{,},{,-},{,},{-,}};
int ex,ey;
int vis[][];
struct Node
{
int x,y;
int time;
} cp;
int bfs()
{
queue<Node> q;
while(!q.empty())q.pop();
q.push(cp);
while(!q.empty())
{
cp=q.front(),q.pop();
if(cp.x==ex&&cp.y==ey)return cp.time;
vis[cp.x][cp.y]=;
for(int i=; i<; i++)
{
Node np;
np.x=cp.x+dir[i][];
np.y=cp.y+dir[i][];
np.time=cp.time+;
if(mp[np.x][np.y]=='#')continue;
if(vis[np.x][np.y])continue;
if(np.x<||np.y<||np.x>=n||np.y>=m)continue;
if(mp[np.x][np.y]=='x')np.time++;
q.push(np);
}
}
return -;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
while(cin>>n>>m)
{
msp,msv;
for(int i=; i<n; i++)cin>>mp[i];
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
if(mp[i][j]=='a')ex=i,ey=j;
else if(mp[i][j]=='r')cp.x=i,cp.y=j;
}
cp.time=;
vis[cp.x][cp.y]=;
int ans=bfs();
if(ans==-)printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",ans);
}
return ;
}
Answer for ZOJ
在ZOJ里再一次交了自己的代码,就发现问题了,竟然MLE了,HDU的数据也太弱了吧。
这次用优先队列去做。
 
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//map,size
int n,m;
char mp[][];
//visited
bool vis[][];
//direction
int dir[][]={{,},{,-},{,},{-,}};
struct Node
{
int x,y;
int time;
friend bool operator <(const Node &a,const Node &b)
{
return a.time>b.time;
}
} cp,np; //current point,new point
int bfs()
{
//creat a priority queue
priority_queue<Node> q;
//clear
while(!q.empty())q.pop();
msv;
vis[cp.x][cp.y]=;
q.push(cp);
while(!q.empty())
{
cp=q.top(),q.pop();
//return answer
if(mp[cp.x][cp.y]=='r')
return cp.time;
for(int i=;i<;i++)
{
//new point,four direction
np.x=cp.x+dir[i][];
np.y=cp.y+dir[i][];
np.time=cp.time+;
//out of map
if(np.x<||np.x>=n||np.y<||np.y>=m)continue;
if(mp[np.x][np.y]=='#')continue;
if(vis[np.x][np.y])continue;
if(mp[np.x][np.y]=='x')np.time++;
vis[np.x][np.y]=;
q.push(np);
}
}
//can't find
return -;
}
int main()
{
while(cin>>n>>m)
{
for(int i=; i<n; i++)
cin>>mp[i];
//get start point
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
if(mp[i][j]=='a')
{
cp.x=i,cp.y=j;
cp.time=;
break;
}
}
//bfs
int ans=bfs();
if(ans==-)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
return ;
}

HDU 1242 Rescue(BFS),ZOJ 1649的更多相关文章

  1. hdu 1242 Rescue (BFS)

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

  2. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  3. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  4. hdu 1242 Rescue(BFS,优先队列,基础)

    题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...

  5. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  6. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

  7. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  8. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  9. HDU 1242 Rescue (BFS(广度优先搜索))

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

随机推荐

  1. 面试题-Java基础-Applet部分

    java applet是能够被包含在HTML页面中并且能被启用了java的客户端浏览器执行的程序.Applet主要用来创建动态交互的web应用程序.

  2. Unity3DGUI:GUILayout

    显示效果,注意GUILayout控件默认垂直布局,且在水平布局模块里控件大小默认按控件内容来显示,因此对于水平滑块HorizontalSlider来说需要自定义大小避免变形

  3. 如何让有物理键的手机在ActionBar始终显示更多菜单menu键

    仅作记录代码用,功能未能测试成功,在低版本上不存在 sHasPermanentMenuKey 属性,会出现 java.lang.NoSuchFieldException: sHasPermanentM ...

  4. CodeForces 671A Recycling Bottles

    暴力. 每个人找到一个入口,也就是从回收站到这个入口走的路程由人的位置到入口的路程来替代. 因此,只要找两个人分别从哪里入口就可以了.注意:有可能只要一个人走,另一人不走. #pragma comme ...

  5. FZU 2240 Daxia & Suneast's problem

    博弈,$SG$函数,规律,线段树. 这个问题套路很明显,先找求出$SG$函数值是多少,然后异或起来,如果是$0$就后手赢,否则先手赢.修改操作和区间查询的话可以用线段树维护一下区间异或和. 数据那么大 ...

  6. mysql 的事务

    $conn = mysql_connect('localhost','root','root') or die ("数据连接错误!!!");mysql_select_db('tes ...

  7. CentOS7中将Mysql添加为系统服务

    如果是自己通过tar包安装的Mysql,不会自动添加到系统服务中,可通过如下方式,自己添加. 先启动一下mysql ${mysql}/support-files/mysql.server start ...

  8. 如果有两个list<Object>只取出两个中不重复的(还可以优化,这里计数器没做好,暂时使用第三变量)

    import java.util.*; class test2{ public static void main(String[] args){ List<Integer> objList ...

  9. 【for陷阱】遍历的同时删除元素

    今晚,哦不,是昨晚了,想删除空行时,给for语句和列表坑得好惨!!! 一般来说,删除字符串的空行有以下几种常见的方法~(然而我竟然想不出来) 假设我们要把下面的字符串之间的空行给去掉 # coding ...

  10. SQL in优化将In转化为联合查询

    in查询有时候会非常影响性能,最好能转化为联合查询,但有的网友说sqlserver会自动将in转化为联合查询,但我实际遇到的有时候却不是这样.所以最好还是不要用in. 我自己的例子,用in的时候耗费了 ...