Skip to content

Latest commit

 

History

History
175 lines (166 loc) · 3.91 KB

hdu2612.MD

File metadata and controls

175 lines (166 loc) · 3.91 KB

日期 2021 / 11 / 3

题目

[题目链接]https://acm.dingbacode.com/showproblem.php?pid=2612

Find a way

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

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

解题思路

这个题目我刚开始以为是双向bfs,然后想得非常复杂,后面才知道是普通的bfs只不过used数组求两遍,这个题目求的是到目的地的最短距离,我刚开始不知道为什么bfs可以求 最短距离而不用最短路算法,后来才意识到这里的图它相邻两个点的距离都是一样的所以可以用队列存并计算,这里我用的两个used数组来存到目的地的距离,然后相加求最小值,

代码演示

#include <bits/stdc++.h>

#define INF 0x7fffffff
#define rep(x, y, z) for (int x = y; x <= z; x++)
#define dec(x, y, z) for (int x = y; x >= z; x--)
#define format(a) memset (a, 0, sizeof(a))
#define swap(a, b) (a ^= b ^= a ^= b)
#define ll long long int
#define ull unsigned long long int 
#define uint unsigned int
const int maxn = 1e6 + 10;
using namespace std;


char map1[210][210];    
int vis[210][210];    
int used1[210][210];    
int used2[210][210];    
int n, m;
int sx1, sy1, sx2, sy2;
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };

struct node {
 int x;
 int y;
 int step;
};

void bfs(int x, int y, int s[][210]) {
 node a, b;
 queue<node> q;
 a.x = x;
 a.y = y;
 a.step = 0;
 q.push(a);
 while (!q.empty()) {
   a = q.front();
   q.pop();
   for (int i = 0; i < 4; i++) {
     b.x = a.x + dir[i][0];
     b.y = a.y + dir[i][1];
     if ((b.x < 0 || b.y < 0 || b.x >= n || b.y >= m || map1[b.x][b.y] == '#' || vis[b.x][b.y])) {
       continue;
     }
     b.step = a.step + 1;
     vis[b.x][b.y] = 1;
     if (map1[b.x][b.y] == '@') {
       s[b.x][b.y] = b.step;
     }
     q.push(b); 
  }
 }
}

int main(int argc, char *argv[]) {
 ios_base::sync_with_stdio(false);
 cin.tie(nullptr);
 while (cin >> n >> m) {
   for (int i = 0; i < n; i++) {
     cin >> map1[i];
   }
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       if (map1[i][j] == 'Y') {
   	  sx1 = i;
   	  sy1 = j;
   	}
   	if (map1[i][j] == 'M') {
   	  sx2 = i;
         sy2 = j;
   	}
     }
   }
   vis[sx1][sy1] = 1;
   bfs(sx1, sy1, used1);
   format(vis);
   vis[sx2][sy2] = 1;
   bfs(sx2, sy2, used2); 
   int min = INF;
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       if (min > used1[i][j] + used2[i][j] && used1[i][j] && used2[i][j]) {
   	  min = used1[i][j] + used2[i][j];
   	}
     }
   }
   cout << min * 11 << endl;
   format(used1);
   format(used2);
   format(vis);
 }
 return 0;
}