Hdu-2612-find A (BFS)

Source: Internet
Author: User

Find A-toTime limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6157 Accepted Submission (s): 2052


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

Authoryifenfei
The age of Source struggle



Simple BFS problem, BFS to record Y and M to the minimum number of steps per point, but note that there may be some KFC can not reach


AC Code:

#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long# Define INF 0x7fffffffusing namespace Std;int N, m;int mp[205][205];int d[205][205];int vis[205][205];const int dx[4] = {-1 , 0, 1, 0};const int dy[4] = {0, 1, 0, -1};struct node {int a;int b;int cnt;node (int x, int y, int z) {a = X;b = Y;cnt = z;}}; void BFs (int x, int y) {//bfs stores the distance between M and y to each point memset (Vis, 0, sizeof (VIS));queue<node> Que;que.push (node (x, y, 0)); vi S[x][y] = 1;while (!que.empty ()) {node T = Que.front (); Que.pop (); for (int i = 0; i < 4; i + +) {int xx = t.a + Dx[i];int y y = t.b + dy[i];if (xx >= 0 && xx <= n-1 && yy >= 0 && yy <= m-1 && mp[xx][ YY] = 2 &&!vis[xx][yy]) {Que.push (noDe (xx, yy, t.cnt + 1));d [xx][yy] + = t.cnt + 1;vis[xx][yy] = 1;}}}  int main () {while (scanf ("%d%d", &n, &m)! = EOF) {int yx, Yy;int mx, my;char s[205];for (int i = 0; i < n; i + +) {scanf ("%s", &s), for (int j = 0; J < m; J + +) {if (s[j] = = '. ') mp[i][j] = 1;else if (s[j] = = ' # ') mp[i][j] = 2;else if (s[j] = = ' @ ') {mp[i][j] = 3;} else if (s[j] = = ' Y ') {mp[i][j] = 4;yx = i; yy = j;} else if (s[j] = = ' M ') {mp[i][j] = 4;mx = i; my = J;}}} for (int i = 0; i < n; i + +, cout << Endl) {//for (int j = 0; J < m; J + +) {//cout << mp[i][j] << "";//}//}memset (d, 0, sizeof (d)), BFS (Yx, yy), BFS (MX, my); int ans = inf;for (int i = 0; I < n; i + +) {for (int j = 0; J < m; J + +) {if (mp[i][j] = = 3) {if (d[i][j]! = 0) {///To note there is also the possibility that KFC cannot reach ans = min (ans, d[i][j]);}} }}printf ("%d\n", ans * 11);} return 0;}








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu-2612-find A (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.