概述
这题要注意 题目中的 each 这个字眼,所以有多个可能 'r'存在,所以从被救的人身上展开搜索比较简单.
一开始不知道要使用优先队列,所以一开始是WA.
优先队列的操作和队列相似,但是队列中的front()函数要用top()函数代替,其他的操作类似.
#include<iostream>
#include<queue>
#define maxn 205
char map[maxn][maxn];
int visit[maxn][maxn];
using namespace std;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int m,n;
bool ok(int i,int j)
{
return map[i][j]!='#'&& i>0 &&i<=n &&j>0 && j<=m ;
}
struct node
{
int x,y,time;
friend bool operator <(const node &a,const node &b)
{
return a.time>b.time ;
}
};
int bfs(int x,int y)
{
int i;
node st,ed;
priority_queue<node>que; //定义一个优先队列
memset(visit,0,sizeof(visit));
st.x=x;
st.y=y;
st.time=0;
visit[st.x][st.y]=1;
que.push(st);
while(!que.empty())
{
st=que.top();
que.pop();
if(map[st.x][st.y]=='r')
return st.time;
for(i=0;i<4;i++)
{
ed.x=st.x+dir[i][0];
ed.y=st.y+dir[i][1];
if(ok(ed.x,ed.y)&&!visit[ed.x][ed.y])
{
visit[ed.x][ed.y]=1;
if(map[ed.x][ed.y]=='x')
ed.time=st.time+2;
else
ed.time=st.time+1;
que.push(ed);
}
}
}
return -1;
}
int main()
{
//freopen("1242.txt","r",stdin);
int i,j;
int x,y,ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
cin>>map[i][j];
// scanf("%s",map[i]);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(map[i][j]=='a')
{
x=i;
y=j;
break;
}
ans=bfs(x,y);
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life.n");
else
printf("%dn",ans);
}
return 0;
}
转载于:https://www.cnblogs.com/xiaoniuniu/p/4004208.html
最后
以上就是痴情樱桃为你收集整理的HDU 1242 rescue的全部内容,希望文章能够帮你解决HDU 1242 rescue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复