"Y" and "M ' two people to a KFC (denoted by" @ "on the map) the minimum time and how much they need for the map to N*m. They take 11 minutes each step.
You can start with Y and M, respectively. BFS to save the time to each KFC and finally see which KFC's time and the smallest.
#include <map> #include <cstdio> #include <algorithm>using namespace std;const int N = 205;int x[] = {0, 0 ,-1, 1};int y[] = {-1, 1, 0, 0};p air<int, int> q[n * n];int dy[n][n], Dm[n][n], ans, N, M;char g[n][n];void BFS (in t r, int c, char k) {int (&d) [n][n] = (k = = ' Y ' dy:dm);//d is a reference to DY or dm int v[n][n] = {0}, le = 0, RI = 0, cr , CC; q[ri++] = Make_pair (R, c), v[r][c] = 1, d[r][c] = 0; while (Le < RI) {r = Q[le].first, c = q[le++].second; for (int i = 0; i < 4; ++i) {CR = R + x[i], CC = C + y[i]; if (CR >= 0 && CR < n && cc >= 0 && cc < m && v[cr][cc] = = 0 && G[CR][CC]! = ' # ') q[ri++] = Make_pair (CR, CC), d[cr][cc] = D[r][c] + 1, v[cr][cc] = 1; }}}int Main () {while (~scanf ("%d%d", &n, &m)) {ans = n * n; for (int i = 0; i < n; ++i) scanf ("%s", G[i]); For (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (g[i][j] = = ' Y ' | | g[i][j] = = ' m ') BFS (i, J, G[i][j]); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (g[i][j] = = ' @ ') ans = min (ans, dy I [j] + dm[i][j]); printf ("%d\n", ans * 11); } return 0;}
Find A-to
Problem Descriptionpass a year learning in Hangzhou, Yifenfei arrival hometown Ningbo at finally. Leave Ningbo One year, Yifenfei has many people to meet. Especially a good friend Merceki.
Yifenfei's home is on the countryside, but Merceki's home is in the center of the city. So Yifenfei made arrangements and Merceki to meet at a KFC. There is many KFC in Ningbo, they want to choose one and the total time to it is most smallest.
Now give your a Ningbo map, Both Yifenfei and Merceki can move up, down, left, right to the adjacent road by cost one minute S.
Inputthe input contains multiple test cases.
Each test case include, first, 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, both Yifenfei and Merceki to arrival one of the KFC. Sure there is always has a KFC that can let them meet.
Sample Input
4 4y.#@.....#. @.. M4 4y.#@.....#. @#. M5 5[email protected].#....#...@. m.#...#
Sample Output
668866
HDU 2612 Find A-A-(double BFS)