http://acm.hdu.edu.cn/showproblem.php?pid=1242

感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了。

但是别人说要用优先队列来保证时间最优,我倒是没明白,步数最优跟时间最优不是等价的吗?就算士兵要花费额外时间,可是既然先到了目标点那时间不也一定是最小的?

当然用优先队列+ a去搜索r是最稳妥的。

 #include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std; struct maze
{
int x,y,t;
bool operator < (const maze a) const
{
return t>a.t;
}
};
int n,m,time;
bool flag;
char field[][];
int dir[][]={{-,},{,},{,},{,-}};
void bfs(maze s)
{
priority_queue<maze>que;
que.push(s);
while(!que.empty())
{
maze e=que.top();
que.pop();
for(int i=;i<;i++)
{
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
s.t=e.t+;
if(s.x>=&&s.x<n&&s.y>=&&s.y<m&&field[s.x][s.y]!='#')
{
if(field[s.x][s.y]=='r')
{
flag=;time=s.t;return;
}
else if(field[s.x][s.y]=='x') s.t++; //printf("%d %d %d\n",s.x,s.y,s.t);
field[s.x][s.y]='#';
que.push(s);
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
maze s;
while(~scanf("%d%d",&n,&m))
{
getchar();
for(int i=;i<n;i++)
{
scanf("%s",field[i]);
for(int j=;j<m;j++)
{
if(field[i][j]=='a')
{
s.x=i;
s.y=j;
s.t=;
}
}
}
//printf("%d %d\n",s.x,s.y);
flag=;
field[s.x][s.y]='#';
bfs(s);
if(flag) printf("%d\n",time);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
 
http://acm.hdu.edu.cn/showproblem.php?pid=2425
这题是给定了起始点和目标点,让你求起始点到目标点的最少花费。总共有3种点,每一种点的花费都不同。跟上面那体没有很大区别。
这题简直悲剧,提交错了5,6次,就是一点小错误还害得我对拍了很多次。以后一定要细心。
判断到达目标点的时候不要再循环里面判断,可能出错。
 #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct maze
{
int x,y,cost;
bool operator < (const maze a) const
{
return cost>a.cost;
}
};
int r,c;
int vp,vs,vt;
int sr,sc,tr,tc;
int dir[][]={{-,},{,},{,-},{,}};
char field[][];
int bfs()
{
priority_queue<maze>que;
maze s;
s.x=sr;s.y=sc;s.cost=;
field[s.x][s.y]='@';
que.push(s);
while(!que.empty())
{
maze e=que.top();
que.pop();
// printf("%d %d %d\n",s.x,s.y,s.cost);
if(e.x==tr&&e.y==tc) return e.cost;
for(int i=;i<;i++)
{
s=e;
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
if(s.x<||s.x>=r||s.y<||s.y>=c||field[s.x][s.y]=='@') continue;
if(field[s.x][s.y]=='#') s.cost+=vp;
else if(field[s.x][s.y]=='.') s.cost+=vs;
else if(field[s.x][s.y]=='T') s.cost+=vt;
field[s.x][s.y]='@';
que.push(s);
}
}
return -;
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("b.txt","w",stdout);
int j=;
while(~scanf("%d%d",&r,&c))
{
//printf("%d %d\n",r,c);
scanf("%d%d%d",&vp,&vs,&vt);
//printf("%d %d %d\n",vr,vs,vt);
getchar();
for(int i=;i<r;i++) scanf("%s",field[i]);
scanf("%d%d%d%d",&sr,&sc,&tr,&tc);
printf("Case %d: %d\n",j++,bfs());
}
return ;
}
 

hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)的更多相关文章

  1. hdu 2425 Hiking Trip

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...

  2. hdu 2425 Hiking Trip (bfs+优先队列)

    Problem Description Hiking in the mountains is seldom an easy task for most people, as it is extreme ...

  3. hdu 1242 Rescue

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

  4. HDU 1242 Rescue(优先队列)

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

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

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

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

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

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

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

  8. HDU 1242 rescue (优先队列模板题)

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

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

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

随机推荐

  1. 2014ACM/ICPC亚洲区北京站 上交命题

    A http://acm.hdu.edu.cn/showproblem.php?pid=5112 输入n个时刻和位置,问那两个时刻间速度最快. 解法:按照时间排序,然后依次求相邻两个之间的速度,速度= ...

  2. ```````````````辐射度 Radiometry

    solid angel --立体角 单位 sr  球面度 dω就是对solid angel的微分 4π代表一个球 我发现dω就是对半径为1的球的表面积的微分 所以4π代表一个球  这就是球的表面积.. ...

  3. Centos最小化安装后联网配置

    Centos最小化安装默认不开启网络,只需进行简单配置就可以上网了. 1.  查看/etc/sysconfig/network-scripts/下面的文件,这里会有一个ifcfg-en******(这 ...

  4. Lua require搜索路径指定方法

    在自己的lua文件中,如果使用到了自己写的C库或者第三方库,想让lua编译到自己指定的目录下寻找*.lua或*.so文件的时候,可以再自己的Lua代码中添加如下代码,可以指定require搜索的路径. ...

  5. 大漠推荐的教程:创建你自己的AngularJS -- 第一部分 Scopes

    创建你自己的AngularJS -- 第一部分 Scopes http://www.html-js.com/article/1863

  6. NumPy的array

    1.numpy包中的array数组,用于弥补列表可以存储任意的数据类型的不足,因为有时候我们需要存储某种类型的数据在数组中,这才是数组的本来内涵.我们通过向numpy.array()函数中传递pyth ...

  7. MVC中 ViewBag、ViewData和TempData区别

    MVC3中 ViewBag.ViewData和TempData的使用和区别 public dynamic ViewBag { get; } public ViewDataDictionary View ...

  8. Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...

  9. JS之类型转换

    一.显示类型转换 1.Boolean() (1).undefined/null/‘’   ==> false (2).任何对象(包括例如var obj = {} ) ==>  true ( ...

  10. SQL注入攻击

    SQL注入攻击是黑客对数据库进行攻击的常用手段之一.随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多.但是由于程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候, ...