zoukankan      html  css  js  c++  java
  • F

    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

    题目意思:
    有二个人Y和M,俩个人要到@(肯德基)初见面,求最短路径和;

    解法:
    也是一个使用BFS的题目
      1 #include <iostream>
      2 #include <cstring>
      3 #include <queue>
      4 using namespace std;
      5 
      6 const int MAX = 200 + 199;
      7 char Map[MAX][MAX];
      8 int visit[2][MAX][MAX];
      9 int dir[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
     10 int x1,y1;
     11 int x2,y2;
     12 int L,W;
     13 
     14 struct S
     15 {
     16     int x,y;
     17     int cont;
     18 };
     19 
     20 void bfs(int x,int y,int ti)
     21 {
     22     queue<S>P;
     23     S ss;
     24     ss.x = x;
     25     ss.y = y;
     26     ss.cont = 0;
     27     visit[ti][x][y] = 0;
     28     P.push(ss);
     29     //cout<<"  ti === "<<ti<<endl;
     30     while(!P.empty())
     31     {
     32         S t;
     33         t = P.front();
     34         P.pop();
     35         //cout<<"  ******** "<<t.x<<t.y<<endl;
     36         for(int i = 0;i < 4;i++)
     37         {
     38             S temp ;
     39             temp.x = t.x + dir[i][0];
     40             temp.y = t.y + dir[i][1];
     41             temp.cont = t.cont+1;
     42 
     43             if(temp.x < 1||temp.x > L||temp.y < 1||temp.y > W)
     44                 continue;
     45             //cout<<"temp.x = "<<temp.x<<"temp.y = "<<temp.y<<"temp.cont = "<<temp.cont<<endl;
     46             if(Map[temp.x][temp.y] !='#'&& visit[ti][temp.x][temp.y] == -1)
     47             {
     48                 visit[ti][temp.x][temp.y] = temp.cont;
     49                 P.push(temp);
     50             }
     51         }
     52 
     53     }
     54 
     55 }
     56 
     57 int main()
     58 {
     59     while(cin>>L>>W)
     60     {
     61         memset(Map,'0',sizeof(Map));
     62         memset(visit,-1,sizeof(visit));
     63 
     64 
     65         for(int i =1;i <= L;i++)
     66             for(int j = 1;j <= W;j++)
     67             {
     68                 cin>>Map[i][j];
     69                 if(Map[i][j] == 'Y')
     70                     x1 = i,y1 = j;
     71                 if(Map[i][j] == 'M')
     72                     x2 = i,y2 = j;
     73             }
     74         bfs(x1,y1,0);
     75         bfs(x2,y2,1);
     76 
     77 
     78   /*      for(int i =1;i <= L;i++)
     79         {
     80             for(int j = 1;j <= W;j++)
     81                 cout<<visit[0][i][j]<<"   ";
     82             cout<<endl;
     83         }
     84         cout<<endl;
     85         for(int i =1;i <= L;i++)
     86         {
     87             for(int j = 1;j <= W;j++)
     88                 cout<<visit[1][i][j]<<"   ";
     89             cout<<endl;
     90         }
     91 */
     92         long long min1 = L*W*4;
     93          for(int i =1;i <= L;i++)
     94             for(int j = 1;j <= W;j++)
     95             {
     96                 if(Map[i][j] == '@'&&visit[0][i][j] != -1&&visit[1][i][j] != -1)
     97                 {
     98                     int sum = visit[0][i][j] + visit[1][i][j];
     99                     min1 = sum < min1?sum:min1;
    100 
    101                 }
    102             }
    103         cout<<min1*11<<endl;
    104 
    105     }
    106 
    107     return 0;
    108 }
  • 相关阅读:
    codeforces1068——D.Array Without Local Maximums(计数DP+前缀和优化)
    codeforces1253——D. Harmonious Graph(并查集)
    LDUOJ——I. 买汽水(折半搜索+双指针)
    洛谷P3360 ——偷天换日(dfs读入+树形DP+01背包)
    洛谷P1270 ——“访问”美术馆(dfs读入+树形DP)
    LDUOJ——最小生成树(欧拉函数+思维)
    [LeetCode] 1551. Minimum Operations to Make Array Equal
    [LeetCode] 1553. Minimum Number of Days to Eat N Oranges
    [LeetCode] 161. One Edit Distance
    [LeetCode] 1260. Shift 2D Grid
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7227530.html
Copyright © 2011-2022 走看看