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 ;
}
 

最新文章

  1. Android开发之InstanceState详解
  2. CSS3 -web-box-shadow实现阴影效果
  3. Android 中的Resource
  4. Oracle10g完全卸载正确步骤
  5. Java笔记(一)&hellip;&hellip;概述
  6. hibernate篇章六--demo(Hibernate之第1解之-hibernate_demo_1)
  7. Cygwin下vim按方向键出现ABCD;
  8. 【PHP】PHP5.4.0版本号ChangeLog具体解释(上)
  9. 利用EntityFramework获得双色球数据库
  10. Unity3D_GUI (1)--按钮控件
  11. 【56】java本地文件File类详解
  12. external与static的用法
  13. Percona-Toolkit 之 pt-table-sync 总结
  14. 记一次SpringBoot 开发中所遇到的坑和解决方法
  15. 第三十一篇-TextInputLayout(增强文本输入)的使用
  16. python manage.py runserver指定端口和ip
  17. python中global和nonlocal用法的详细说明
  18. mongodb4.0.2 复制集主从部署
  19. 深入学习Python解析并解密PDF文件内容的方法
  20. code block自动生成makefile

热门文章

  1. [bzoj 2097]奶牛健美操
  2. html之colspan &amp;&amp; rowspan讲解
  3. WIN7设置wifi热点的方法
  4. 容器字段FieldContainer
  5. POJ 1459
  6. mvc5 _ViewStart.cshtml 模板页如何定义
  7. ***PHP preg_match正则表达式的使用
  8. Codeforces Round #335 (Div. 2) A. Magic Spheres 模拟
  9. 自动化 测试框架部署(python3+selenium2)
  10. tvm install