HDU 4308 Saving Princess claire_

来源:互联网 发布:佟大为演技 知乎 编辑:程序博客网 时间:2024/05/22 07:44
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<queue>using namespace std;int n,m,c;char map[5010][5010];int cost[5010][5010],dx[4]={-1,1,0,0},dy[4]={0,0,-1,1},px[1000000],py[1000000],cnt,flag;struct ty{    int x,y,co;};const int INF=100000000;void bfs(int x,int y){    int i,j,k;    ty p,tp;    p.x=x;    p.y=y;    p.co=0;    queue<ty> q;    q.push(p);    while(!q.empty()){        p=q.front();        q.pop();        x=p.x;        y=p.y;    for(i=0;i<4;i++)    {        int tx=x+dx[i],ty=y+dy[i];        if(tx<n&&tx>=0&&ty>=0&&ty<m&&map[tx][ty]!='#')        {            if(map[tx][ty]=='*'&&cost[tx][ty]>p.co+c)            {                tp.x=tx;                tp.y=ty;                tp.co=p.co+c;                cost[tx][ty]=p.co+c;                q.push(tp);            }            else if(map[tx][ty]=='P')            {                for(j=0;j<cnt;j++)                    if(cost[px[j]][py[j]]>p.co)                    {                        tp.x=px[j];                        tp.y=py[j];                        tp.co=p.co;                        cost[px[j]][py[j]]=p.co;                        q.push(tp);                    }            }            else if(map[tx][ty]=='C'&&cost[tx][ty]>cost[x][y])            {                flag=1;                cost[tx][ty]=cost[x][y];            }        }    }    }}int main(){    int i,j,sx,sy,ex,ey;    while(scanf("%d%d%d",&n,&m,&c)!=EOF)    {        getchar();        flag=cnt=0;        for(i=0;i<n;i++)            gets(map[i]);        for(i=0;i<n;i++)            for(j=0;j<m;j++)            {                cost[i][j]=INF;                if(map[i][j]=='Y')                {                    sx=i;                    sy=j;                }                else if(map[i][j]=='C')                {                    ex=i;                    ey=j;                }                else if(map[i][j]=='P')                {                    px[cnt]=i;                    py[cnt++]=j;                }            }        cost[sx][sy]=0;        bfs(sx,sy);        if(!flag)            printf("Damn teoy!\n");        else            printf("%d\n",cost[ex][ey]);    }return 0;}

原创粉丝点击