HDOJ 4308 - Saving Princess claire_ 水BFS...

           题意:

                      给一个图..起点是'Y'..目标点是'C'...'*'是道路.过路费是cost...P是虫洞..可以不需要代价的瞬移...'#'是墙不能走过...问从起点到终点的最小代价...

           题解:

                     由于只给了r*c<=5000...开个一维的也行..对于(x,y).....在一维里的位置为y*c+x....然后就暴力BFS..找出解~


Program:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<time.h>
#include<map>
#include<algorithm>
#define ll long long
#define eps 1e-5
#define oo 10007
#define pi acos(-1.0)
#define MAXN 5005
using namespace std;
struct node
{
      int x,y;
}P[505];
char s[MAXN],T[MAXN]; 
int C[MAXN];
bool inqueue[MAXN];
queue<node> myqueue;
int main()
{      
      int r,c,i,x,k,num,cost; 
      while (~scanf("%d%d%d",&r,&c,&cost))
      {
              memset(C,0x3f,sizeof(C));
              node h,temp;
              num=0;
              for (i=0;i<r;i++)
              {
                       scanf("%s",s); 
                       for (x=0;x<c;x++) 
                       { 
                             T[i*c+x]=s[x];
                             if (s[x]=='Y') h.y=i,h.x=x,C[i*c+x]=0;
                             if (s[x]=='P') P[++num].y=i,P[num].x=x;
                       }
              }  
              while (!myqueue.empty()) myqueue.pop();
              memset(inqueue,false,sizeof(inqueue));
              myqueue.push(h);
              while (!myqueue.empty())
              {
                       h=myqueue.front();
                       myqueue.pop();
                       if (T[h.y*c+h.x]=='C') break;
                       inqueue[h.y*c+h.x]=false;
                       if (T[h.y*c+h.x]=='P')
                           for (i=1;i<=num;i++) 
                              if (C[P[i].y*c+P[i].x]>C[h.y*c+h.x])
                              {
                                     C[P[i].y*c+P[i].x]=C[h.y*c+h.x]; 
                                     if (!inqueue[P[i].y*c+P[i].x]) 
                                     {
                                            temp.y=P[i].y,temp.x=P[i].x;
                                            myqueue.push(temp);
                                            inqueue[P[i].y*c+P[i].x]=true;
                                     }
                              }
                       if (h.x>0 && T[h.y*c+h.x-1]!='#')
                       { 
                              if (T[h.y*c+h.x-1]=='*') k=cost;  else k=0;
                              if (C[h.y*c+h.x-1]>C[h.y*c+h.x]+k)
                              {
                                    C[h.y*c+h.x-1]=C[h.y*c+h.x]+k;
                                    if (!inqueue[h.y*c+h.x-1])
                                    {
                                          temp.x=h.x-1,temp.y=h.y;
                                          myqueue.push(temp);
                                          inqueue[h.y*c+h.x-1]=true;
                                    }
                              }
                       }
                       if (h.y>0 && T[(h.y-1)*c+h.x]!='#')
                       { 
                              if (T[(h.y-1)*c+h.x]=='*') k=cost;  else k=0; 
                              if (C[(h.y-1)*c+h.x]>C[h.y*c+h.x]+k)
                              {
                                    C[(h.y-1)*c+h.x]=C[h.y*c+h.x]+k;
                                    if (!inqueue[(h.y-1)*c+h.x])
                                    {
                                          temp.x=h.x,temp.y=h.y-1;
                                          myqueue.push(temp);
                                          inqueue[(h.y-1)*c+h.x]=true;
                                    }
                              }
                       }
                       if (h.x!=c-1 && T[h.y*c+h.x+1]!='#')
                       { 
                              if (T[h.y*c+h.x+1]=='*') k=cost;   else k=0;
                              if (C[h.y*c+h.x+1]>C[h.y*c+h.x]+k)
                              {
                                    C[h.y*c+h.x+1]=C[h.y*c+h.x]+k;
                                    if (!inqueue[h.y*c+h.x+1])
                                    {
                                          temp.x=h.x+1,temp.y=h.y;
                                          myqueue.push(temp);
                                          inqueue[h.y*c+h.x+1]=true;
                                    }
                              }
                       }
                       if (h.y!=r-1 && T[(h.y+1)*c+h.x]!='#')
                       { 
                              if (T[(h.y+1)*c+h.x]=='*') k=cost;  else k=0; 
                              if (C[(h.y+1)*c+h.x]>C[h.y*c+h.x]+k)
                              {
                                   C[(h.y+1)*c+h.x]=C[h.y*c+h.x]+k;
                                   if (!inqueue[(h.y+1)*c+h.x])
                                   {
                                         temp.x=h.x,temp.y=h.y+1;
                                         myqueue.push(temp);
                                         inqueue[(h.y+1)*c+h.x]=true;
                                   }
                              }
                       }
              }              
              if (T[h.y*c+h.x]!='C') printf("Damn teoy!\n");
                 else printf("%d\n",C[h.y*c+h.x]);
      }
      return 0;
}  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值