HDU-2612-Find a way

Source: Internet
Author: User

HDU-2612-Find a way

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2612

Calculate the sum of the distance between the two points and KFC to minimize it. Use BFS twice to obtain the shortest distance between the two points and each KFC, and then find the minimum and minimum values.

# Include <stdio. h> # include <string. h> # include <stdlib. h> # define Max 0x7fffffint n, m; char map [250] [250]; int visit1 [250] [250]; int visit2 [250] [250]; int ans1 [250] [250]; int ans2 [250] [250]; struct cam {int X, Y;} list [80000]; int dir [4] [2] = {-}, {}, {0,-1}, {}; int go (int x, int y) {If (0 <= x & x <n & 0 <= Y & Y <M & map [x] [Y]! = '#') Return 1; return 0;} void BFS (int x, int y, int visit [250] [250], int ans [250] [250]) {int I, Head, tail; int XX, YY; memset (visit, 0, sizeof (visit); memset (ANS, 0, sizeof (ANS )); visit [x] [Y] = 1; ans [x] [Y] = 0; list [0]. X = x; list [0]. y = y; head = 0; tail = 1; while (Head <tail) {x = list [head]. x; y = list [head]. y; for (I = 0; I <4; I ++) {xx = x + dir [I] [0]; YY = Y + dir [I] [1]; If (go (XX, YY )&&! Visit [XX] [YY]) {list [tail]. X = xx; list [tail]. y = YY; ans [XX] [YY] = ans [x] [Y] + 1; visit [XX] [YY] = 1; tail ++ ;}} head ++ }}int main () {int I, j; int X1, Y1, X2, Y2; int min; while (scanf ("% d ", & N, & M )! = EOF) {for (I = 0; I <n; I ++) scanf ("% s", map [I]); for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {If (Map [I] [J] = 'y ') {x1 = I; Y1 = J;} else if (Map [I] [J] = 'M') {X2 = I; y2 = J ;}} memset (visit1, 0, sizeof (visit1); memset (ans1, 0, sizeof (ans1); BFS (x1, Y1, visit1, ans1); memset (visit2, 0, sizeof (visit2); memset (ans2, 0, sizeof (ans2); BFS (X2, Y2, visit2, ans2); min = max; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {If (Map [I] [J] = '@' & visit1 [I] [J] & visit2 [I] [J]) if (min> ans1 [I] [J] + ans2 [I] [J]) min = ans1 [I] [J] + ans2 [I] [J];} printf ("% d \ n", Min * 11);} return 0 ;}

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.