HDU2425:Hiking Trip(简单bfs,优先队列实现)

题目: 传送门

题意很简单就不解释了,水题一道。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
typedef __int64  ll;
#define inf 0x3f3f3f3f
#include <math.h>
#include <queue>
using namespace std;
struct node
{
    int x,y,ans;
    bool operator<(const node &a)const //从小到大排序
    {
        return ans>a.ans;
    }
};
node st,ff;
int n,m,P,S,T,s,e,s2,e2,v[22][22];
char a[22][22];
int fx[]= {1,-1,0,0};
int fy[]= {0,0,1,-1};
int main()
{
    int sum,K=0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        priority_queue<node>q;
        while(!q.empty()) q.pop();
        sum=-1;
        scanf("%d%d%d",&P,&S,&T);
        for(int i=0; i<n; i++)
            scanf("%s",a[i]);
        scanf("%d%d%d%d",&s,&e,&s2,&e2);
        memset(v,0,sizeof(v));
        st.x=s,st.y=e,st.ans=0;
        v[s][e]=1;
        q.push(st);
        while(!q.empty())
        {
            ff=q.top();
            q.pop();
            if(ff.x==s2&&ff.y==e2)
            {
                sum=ff.ans;
                break;
            }
            for(int i=0; i<4; i++)
            {
                st.x=ff.x+fx[i];
                st.y=ff.y+fy[i];
                if(v[st.x][st.y]==0&&a[st.x][st.y]!='@')
                {
                    if(a[st.x][st.y]=='T')
                    {
                        st.ans=ff.ans+T;
                        v[st.x][st.y]=1;
                        q.push(st);
                    }
                    else if(a[st.x][st.y]=='.')
                    {
                        st.ans=ff.ans+S;
                        v[st.x][st.y]=1;
                        q.push(st);
                    }
                    else if(a[st.x][st.y]=='#')
                    {
                        st.ans=ff.ans+P;
                        v[st.x][st.y]=1;
                        q.push(st);
                    }
                }
            }
        }
        printf("Case %d: %d
",++K,sum);

    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhangmingcheng/p/4328457.html