hdu 4308(bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4308

思路:就是一般的广搜,然后要注意的地方就是位置P了,一次把所有的都入队列,然后标志置为true。别的的话就是随便搞搞就行了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 #define inf 1<<30
 7 #define MAXN 5050
 8 struct Node{
 9     int x,y;
10     int cost;
11 };
12 int xx[MAXN],yy[MAXN];
13 bool mark[MAXN][MAXN];
14 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
15 char map[MAXN][MAXN];
16 int ans,cnt,n,m,w;
17 Node st,ed;
18 
19 void bfs(){
20     memset(mark,false,sizeof(mark));
21     queue<Node>Q;
22     Node p,q;
23     mark[st.x][st.y]=true;
24     Q.push(st);
25     while(!Q.empty()){
26         p=Q.front();
27         Q.pop();
28         if(p.x==ed.x&&p.y==ed.y){
29             ans=min(ans,p.cost);
30             continue;
31         }
32         for(int i=0;i<4;i++){
33             q.x=p.x+dir[i][0];
34             q.y=p.y+dir[i][1];
35             if(q.x<1||q.x>n||q.y<1||q.y>m||map[q.x][q.y]=='#')
36                 continue;
37             if(mark[q.x][q.y])continue;
38             if(map[q.x][q.y]=='P'){
39                 for(int k=1;k<=cnt;k++){
40                     q.x=xx[k],q.y=yy[k],q.cost=p.cost;
41                     mark[q.x][q.y]=true;
42                     Q.push(q);
43                 }
44             }else if(map[q.x][q.y]=='*'){
45                 q.cost=p.cost+w;
46                 mark[q.x][q.y]=true;
47                 Q.push(q);
48             }else {
49                 q.cost=p.cost;
50                 Q.push(q);
51             }
52         }
53     }
54 }
55 
56 
57 int main(){
58     while(~scanf("%d%d%d",&n,&m,&w)){
59         cnt=0,ans=inf;
60         for(int i=1;i<=n;i++){
61             scanf("%s",map[i]+1);
62             for(int j=1;j<=m;j++){
63                 if(map[i][j]=='Y')st.x=i,st.y=j,st.cost=0;
64                 else if(map[i][j]=='C')ed.x=i,ed.y=j,ed.cost=0;
65                 else if(map[i][j]=='P'){
66                     xx[++cnt]=i;
67                     yy[cnt]=j;
68                 }
69             }
70         }
71         bfs();
72         ans<inf?printf("%d\n",ans):puts("Damn teoy!");
73     }
74     return 0;
75 }
View Code

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值