HDU 1242 Rescue (搜索)水题

题意:求a到r的最短路,,到#时的路的长度为2

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;
int n,m;
const int N = 209;
char map[N][N];
bool visit[N][N];
struct node
{
    int x,y,dis;
    bool operator<(const node t) const
    {
        return dis>t.dis;
    }
} st,en;
int dx[]= {0,0,-1,1};
int dy[]= {-1,1,0,0};
bool oor(int x,int y)
{
    if(x<0||x>=n) return false;
    if(y<0||y>=m) return false;
    return true;
}
int main()
{
    freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0; i<n; i++)
        {
            scanf("%s",map[i]);
            for(int j=0; j<m; j++)
                if(map[i][j]=='a')
                    st.x=i,st.y=j;
                else if(map[i][j]=='r')
                    en.x=i,en.y=j;
        }

        priority_queue<node> que;
        st.dis =0;
        while(!que.empty()) que.pop();
        memset(visit,false,sizeof(visit));
        que.push(st);
        node e,t;
        bool ou = false;
        while(!que.empty())
        {
            e= que.top();
            que.pop();
            if(e.x==en.x&&e.y==en.y)
            {
                printf("%d\n",e.dis);
                ou = true;
                break;
            }
            if(visit[e.x][e.y]) continue;
            visit[e.x][e.y] = true;
            for(int i=0; i<4; i++)
            {
                int tx = e.x+dx[i],ty = e.y+dy[i];
                if(!oor(tx,ty)||visit[tx][ty]||map[tx][ty]=='#') continue;
                if(map[tx][ty]=='x') t.dis = e.dis+2;
                else t.dis=e.dis+1;
                t.x = tx;
                t.y = ty;
                que.push(t);
            }
        }
        if(!ou)
        printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值