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