hdu 2425 (BFS)

来源:互联网 发布:原始dbc数据 编辑:程序博客网 时间:2024/06/03 19:39

点击打开链接


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;}   


原创粉丝点击