HDU 4308 Saving Princess claire_

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值