HDU 1242 rescue

这题要注意 题目中的 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("%d\n",ans);  
    }  
    return 0;  
	
}

 

转载于:https://www.cnblogs.com/xiaoniuniu/p/4004208.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值