hdu4308 Saving Princess claire_(BFS)

题意就是给你一个图,从Y走到C,每次经过*都有一个固定的花费,Y是传送门,可以传送到任何其他的传送门,#是路障。输出最小的花费。

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
const int maxn=5010;
int to[4][2]={1,0,-1,0,0,1,0,-1};
int r,c,tot,ans,cost;
bool vis[maxn][maxn];
char mp[maxn][maxn];
struct node{
    int x,y;
    int cnt;
};
int stx,sty,enx,eny;
node p[maxn*maxn],now,ne;
int bfs(int sx,int sy){
    memset(vis,0,sizeof(vis));
    ne.x=sx;ne.y=sy;ne.cnt=0;
    vis[sx][sy]=1;
    queue<node> q;
    q.push(ne);
    while(!q.empty()){
        now=q.front();
    //    cout<<now.x<<" "<<now.y<<endl;
        q.pop();
        if(now.x==enx&& now.y==eny)
            return now.cnt-1;
        int nx,ny;
        for(int i=0;i<=3;i++){
            nx=now.x+to[i][0];
            ny=now.y+to[i][1];
            if(nx<=0 || ny<=0 || nx>r || ny>c || vis[nx][ny]==1)
                continue;
            if(mp[nx][ny]=='#')
                continue;
            vis[nx][ny]=1;
            if(mp[nx][ny]=='P'){
                for(int i=0;i<tot;i++){
                    if(nx==p[i].x&&ny==p[i].y)
                        continue;
                    ne.x=p[i].x;ne.y=p[i].y;
                    ne.cnt=now.cnt;
                    q.push(ne);
                }
                continue;
            }
            ne.x=nx;ne.y=ny;
            ne.cnt=now.cnt+1;
            q.push(ne);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d%d",&r,&c,&cost)){
        for(int i=1;i<=r;i++){
            scanf("%s",mp[i]+1);
        }
        tot=0;
        for(int i=1;i<=r;i++){
            for(int j=1;j<=c;j++){
                if(mp[i][j]=='Y')
                    {stx=i;sty=j;}
                else if(mp[i][j]=='C')
                    {enx=i;eny=j;}
                else if(mp[i][j]=='P')
                    {p[tot].x=i;p[tot++].y=j;}
            }
        }
        int tmp=bfs(stx,sty);
        if(tmp==-1)puts("Damn teoy!");
        else
            printf("%d\n",tmp*cost); 
    }        
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值