题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1242

题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时。

解题思路

如果没有特殊点,就是普通BFS。

由于特殊点的介入,所以BFS树的同一深度,各个点的值可能不同。所以使用优先队列,先取出值小的搜。

搜到的第一个符合条件的结果肯定是最小的,break。

注意有多个起点,所以先记录下所有起点,依次BFS找最小。

#include "cstdio"
#include "string"
#include "cstring"
#include "iostream"
#include "vector"
#include "queue"
using namespace std;
#define inf 1<<28
struct status
{
int x,y,dep;
status(int x,int y,int dep):x(x),y(y),dep(dep) {}
bool operator < (const status &a) const
{
return dep>a.dep;
}
};
int n,m,map[][],vis[][],dir[][]={-,,,,,-,,},ans;
void bfs(int x,int y)
{
priority_queue<status> Q;
Q.push(status(x,y,));
vis[x][y]=true;
bool flag=false;
while(!Q.empty())
{
if(flag) break;
status t=Q.top();Q.pop();
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][];
if(vis[X][Y]||X<||X>n||Y<||Y>n||!map[X][Y]) continue;
vis[X][Y]=true;
if(map[X][Y]==) {flag=true;ans=min(ans,t.dep+);};
if(map[X][Y]==) Q.push(status(X,Y,t.dep+));
else Q.push(status(X,Y,t.dep+));
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
while(cin>>n>>m&&n)
{
vector<status> f;
ans=inf;
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
if(tt[j]=='#') map[i][j+]=;
if(tt[j]=='.') map[i][j+]=;
if(tt[j]=='a') map[i][j+]=;
if(tt[j]=='x') map[i][j+]=;
if(tt[j]=='r') {map[i][j+]=;f.push_back(status(i,j+,));}
}
}
for(int i=;i<f.size();i++)
{
memset(vis,,sizeof(vis));
bfs(f[i].x,f[i].y);
}
if(ans!=inf) cout<<ans<<endl;
else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
}
11869491 2014-10-14 14:50:56 Accepted 1242 31MS 612K 1861 B C++ Physcal

最新文章

  1. OC中的多继承
  2. centos
  3. 虚拟机启动linux系统报错,此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态
  4. How to throw an error in MySql procedure?
  5. 【转】15个无比华丽的HTML5/CSS3动画应用
  6. JDBC第一天
  7. Photoshop 使用可选颜色
  8. [CTSC 2012][BZOJ 2806]Cheat
  9. ORA-01078、ORA-01565、ORA-17503、ORA-29701
  10. hihocoder 1049 后序遍历
  11. DOM_节点层次
  12. Asp.Net alert弹出提示信息的5种方法
  13. oracle 过程函数,包的区别和联系
  14. Keil C -WARNING L15: MULTIPLE CALL TO SEGMENT
  15. js框架漫谈
  16. js入门实例
  17. 由一次自建库迁移到阿里云RDS引发的性能问题。
  18. How hacker do IT: Tricks Tools and Techniques (翻译)
  19. xtrabackup备份
  20. svn文件夹解锁批处理

热门文章

  1. [POJ1383]Labyrinth
  2. myeclipse2014安装反编译插件
  3. 【Django】Django 如何使用 Django设置的日志?
  4. dubbo作为消费者注册过程分析
  5. 基础知识《五》---Java多线程的常见陷阱
  6. sharepoint bcs (bussiness connectivity services)
  7. iOS 使用Storyboard 和 xib时的一些知识
  8. springMVC的一些工具类
  9. myeclipse6.5中使用Alt+/不自动提示的修改
  10. php的socket通信(二)