zoukankan      html  css  js  c++  java
  • Find a way HDU

    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. 

    InputThe 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 
    OutputFor 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
    两次BFS
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include <deque>
    11 #include <iterator>
    12 #include<stack>
    13 struct lll
    14 {
    15     int x,y,bu;
    16 }Yqidian,Mqidian,s;
    17 char a[210][210],b[210][210],c[210][210];
    18 int b1[210][210],c1[210][210];
    19 int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    20 queue<lll> TM;
    21 int main()
    22 {
    23     int lenx,leny;
    24     int i,j;
    25     while(cin>>leny>>lenx)
    26     {
    27         memset(b1,0,sizeof(b1));
    28         memset(c1,0,sizeof(c1));
    29         for(i=0;i<leny;i++)
    30             for(j=0;j<lenx;j++)
    31         {
    32             cin>>a[i][j];
    33             if(a[i][j]=='Y')
    34             {
    35                 Yqidian.y=i;
    36                 Yqidian.x=j;
    37                 Yqidian.bu=0;
    38             }
    39              if(a[i][j]=='M')
    40             {
    41                 Mqidian.y=i;
    42                 Mqidian.x=j;
    43                 Mqidian.bu=0;
    44             }
    45             b[i][j]=a[i][j];
    46             c[i][j]=a[i][j];
    47         }
    48         TM.push(Yqidian);
    49         while(!TM.empty())
    50         {
    51             for(i=0;i<4;i++)
    52             {
    53                 s.x=TM.front().x+d[i][0];
    54                 s.y=TM.front().y+d[i][1];
    55                 s.bu=TM.front().bu+1;
    56                 if(s.x<0||s.y<0||s.x>=lenx||s.y>=leny)
    57                     continue;
    58                 if(b[s.y][s.x]=='#')
    59                     continue;
    60                 b1[s.y][s.x]=s.bu;
    61                 b[s.y][s.x]='#';
    62                 TM.push(s);
    63             }
    64             TM.pop();
    65         }
    66         TM.push(Mqidian);
    67         while(!TM.empty())
    68         {
    69             for(i=0;i<4;i++)
    70             {
    71                 s.x=TM.front().x+d[i][0];
    72                 s.y=TM.front().y+d[i][1];
    73                 s.bu=TM.front().bu+1;
    74                 if(s.x<0||s.y<0||s.x>=lenx||s.y>=leny)
    75                     continue;
    76                 if(c[s.y][s.x]=='#')
    77                     continue;
    78                 c1[s.y][s.x]=s.bu;
    79                 c[s.y][s.x]='#';
    80                 TM.push(s);
    81             }
    82             TM.pop();
    83         }
    84         int max1=100861;
    85         for(i=0;i<leny;i++)
    86         {
    87             for(j=0;j<lenx;j++)
    88             {
    89                 if(a[i][j]=='@'&&b1[i][j]!=0&&c1[i][j]!=0)
    90                 {
    91                     if(b1[i][j]+c1[i][j]<max1)
    92                     max1=b1[i][j]+c1[i][j];
    93                 }
    94             }
    95         }
    96         cout<<max1*11<<endl;
    97     }
    98     return 0;
    99 }
    View Code
  • 相关阅读:
    单例模式的懒汉式在多线程的问题
    String、StringBuffer、与StringBuilder的区别
    java网络编程(7)——利用tcp实现文件上传
    java网络编程(6)——实现一个服务器把小写转大写
    java网络编程(5)——Tcp
    java网络编程(4)——udp实现聊天
    数据库单表增量备份方案
    java网络编程(3)——UDP
    使用Docker快速搭建Tensorflow开发环境
    word2vec并行实现小记
  • 原文地址:https://www.cnblogs.com/dulute/p/7272661.html
Copyright © 2011-2022 走看看