zoukankan      html  css  js  c++  java
  • HDU-2612

    Find a way

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 14087    Accepted Submission(s): 4485


    Problem Description
    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
    Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
    Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
     
    Input
    The input contains multiple test cases.
    Each test case include, first two integers n, m. (2<=n,m<=200). 
    Next n lines, each line included m character.
    ‘Y’ express yifenfei initial position.
    ‘M’    express Merceki initial position.
    ‘#’ forbid road;
    ‘.’ Road.
    ‘@’ KCF
     
    Output
    For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
     
    Sample Input
    4 4
    Y.#@
    ....
    .#..
    @..M
    4 4
    Y.#@
    ....
    .#..
    @#.M
    5 5
    Y..@.
    .#...
    .#...
    @..M.
    #...#
     
    Sample Output
    66
    88
    66

    求到Y和M的步数最少的@

    bfs搜索。

    附AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 struct node{
     5     int x,y,step;
     6 };
     7 
     8 char mp[210][210];
     9 int yx,yy,mx,my,n,m;
    10 int dir[4][2]={1,0,-1,0,0,1,0,-1};
    11 bool vis[210][210];
    12 int dist1[210][210],dist2[210][210];
    13 queue<node> Q;
    14 
    15 void bfs(node p,int dist[][210]){
    16     node q;
    17     memset(dist,0,sizeof(dist));
    18     memset(vis,0,sizeof(vis));
    19     Q.push(p);
    20     while(!Q.empty()){
    21         p=Q.front();
    22         Q.pop();
    23         for(int i=0;i<4;i++){
    24             q.x=p.x+dir[i][0];
    25             q.y=p.y+dir[i][1];
    26             q.step=p.step+1;
    27             if(q.x<n&&q.x>=0&&q.y<m&&q.y>=0&&mp[q.x][q.y]!='#'&&vis[q.x][q.y]==0){
    28                 vis[q.x][q.y]=1;
    29                 dist[q.x][q.y]=q.step;
    30                 Q.push(q);
    31             }
    32         }
    33     }
    34 }
    35 
    36 int main(){
    37     while(cin>>n>>m){
    38         for(int i=0;i<n;i++){
    39             for(int j=0;j<m;j++){
    40                 cin>>mp[i][j];
    41                 if(mp[i][j]=='Y'){
    42                     yx=i;
    43                     yy=j;
    44                 }
    45                 if(mp[i][j]=='M'){
    46                     mx=i;
    47                     my=j;
    48                 }
    49             }
    50         }
    51         int min=999999999;
    52         node p;
    53         p.x=yx;
    54         p.y=yy;
    55         p.step=0;
    56         bfs(p,dist1);
    57         p.x=mx;
    58         p.y=my;
    59         p.step=0;
    60         bfs(p,dist2);
    61         for(int i=0;i<n;i++){
    62             for(int j=0;j<m;j++){
    63                 if(mp[i][j]=='@'){
    64                     if(dist1[i][j]!=0&&dist2[i][j]!=0)
    65                     if(dist1[i][j]+dist2[i][j]<min)
    66                     min=dist1[i][j]+dist2[i][j];
    67                 }
    68             }
    69         }
    70         cout<<min*11<<endl;
    71     }
    72     return 0;
    73 } 
  • 相关阅读:
    函数
    大对象数据LOB
    【缓冲流、转换流、序列化流】
    【字节流、字符流】
    【File类、递归】
    【缓冲流、转换流、序列化流】
    vee-validate的使用
    微信小程序第三方授权登录
    新电脑搭建vue项目步凑
    移动端300ms延迟问题和点击穿透问题
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/6735752.html
Copyright © 2011-2022 走看看