• 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
  • 相关阅读:
    未来行业
    百度网盘搜索方法
    JavaScript继承详解
    Win 7下破解Loadrunner 11(带中文版下载地址)
    NET下RabbitMQ实践[WCF发布篇]
    NET下RabbitMQ实践[示例篇]
    NET下RabbitMQ实践[配置篇]
    8个超炫的 Web 效果
    Windows下安装zip包解压版mysql
    键盘上相当于鼠标右键的快捷键和电脑快捷键大全
  • 原文地址:https://www.cnblogs.com/wally/p/3087262.html
Copyright © 2020-2023  润新知