tjut 4308

#include"stdio.h"  
#include"string.h"  
#include"stdlib.h"  
#include"queue"  
#define N 5001  
using namespace std;  
  
int n,m,cost;  
char map[N][N];  
char flag[N][N];  
  
int s_x,s_y,e_x,e_y;  
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};  
struct A  
{  
    int x,y;  
}hehe[555];  
int tot;  
  
struct node  
{  
    int x,y;  
    int cost;  
    friend bool operator<(node n1,node n2)  
    {  
        return n2.cost<n1.cost;  
    }  
};  
  
int BFS()  
{  
    priority_queue<node>q;  
    node now,next;  
    int i,j;  
  
    for(i=0;i<n;i++)  
    for(j=0;j<m;j++)  
        flag[i][j]=0;  
    now.x=s_x;  
    now.y=s_y;  
    now.cost=0;  
    q.push(now);  
    flag[now.x][now.y]=1;  
    while(!q.empty())  
    {  
        now=q.top();  
        q.pop();  
        if(now.x==e_x && now.y==e_y)    return now.cost-cost;  
        for(i=0;i<4;i++)  
        {  
            next.x=now.x+dir[i][0];  
            next.y=now.y+dir[i][1];  
  
            if(next.x<0 || next.x>=n || next.y<0 || next.y>=m)  continue;  
            if(map[next.x][next.y]=='#')continue;  
            if(flag[next.x][next.y])    continue;  
  
            if(map[next.x][next.y]=='*')  
            {  
                next.cost=now.cost+cost;  
                q.push(next);  
                flag[next.x][next.y]=1;  
            }  
            else if(map[next.x][next.y]=='P')  
            {  
                if(flag[next.x][next.y])    continue;  
                int l;  
                for(l=0;l<tot;l++)  
                {  
                    next.x=hehe[l].x;  
                    next.y=hehe[l].y;  
                    next.cost=now.cost;  
                    q.push(next);  
                    flag[next.x][next.y]=1;  
                }  
            }  
        }  
    }  
    return -1;  
}  
int main()  
{  
    int i,l;  
    int ans;  
    while(scanf("%d%d%d",&n,&m,&cost)!=-1)  
    {  
        tot=0;  
        for(i=0;i<n;i++)  
        {  
            scanf("%s",&map[i]);  
            for(l=0;map[i][l];l++)  
            {  
                if(map[i][l]=='Y')      {s_x=i;s_y=l;}  
                else if(map[i][l]=='C') {e_x=i;e_y=l;map[i][l]='*';}  
                else if(map[i][l]=='P') {hehe[tot].x=i;hehe[tot].y=l;tot++;}  
            }  
        }  
  
        ans=BFS();  
        if(ans==-1) printf("Damn teoy!\n");  
        else        printf("%d\n",ans);  
    }  
    return 0;  
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值