hdu 2425 (BFS)

点击打开链接


1Y,直接水过。。。

注意对应所需要的时间。。。。


#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
struct node
{
    int x,y,step;
    friend bool operator < ( node a,node b)
    {
        return a.step>b.step;
    }
}p,q;
int n,m;
int pp,s,t;
int f,ans;
int s1,s2,e1,e2;
int mark[21][21];
char map[21][21];
int dir[4][2]={1,0,0,1,-1,0,0,-1};
int fun(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&mark[x][y]==0&&map[x][y]!='@')
        return 1;
    return 0;
}
void bfs()
{
    int i,x,y;
    priority_queue<node>Q;
    p.x=s1;
    p.y=e1;
    p.step=0;
    mark[s1][e1]=1;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.top();
        Q.pop();
        if(p.x==s2&&p.y==e2)
        {
            if(ans>p.step)ans=p.step;
            f=1;
        }
        for(i=0;i<4;i++)
        {
            x=q.x=p.x+dir[i][0];
            y=q.y=p.y+dir[i][1];
            q.step=p.step;
            if(fun(x,y))
            {
                mark[x][y]=1;
                if(map[x][y]=='T')
                    q.step+=t;
                else if(map[x][y]=='.')
                    q.step+=s;
                else if(map[x][y]=='#')
                    q.step+=pp;
                Q.push(q);
            }
        }
    }
}    

int main()
{
    int i,j;
    int cnt;
    cnt=1;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        scanf("%d%d%d",&pp,&s,&t);
        getchar();
        for(i=0;i<n;i++)
            gets(map[i]);
        scanf("%d%d%d%d",&s1,&e1,&s2,&e2);
        f=0;ans=999999999;
        memset(mark,0,sizeof(mark));
        if(map[s2][e2]!='@')
            bfs();
        printf("Case %d: ",cnt++);
        if(f==0)printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}
   


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值