HDU 2612 Find a way

Find a way

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 21   Accepted Submission(s) : 12

Font: Times New Roman | Verdana | Georgia

Font Size:

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

Author

yifenfei

Source

奋斗的年代
 
思路:双向BFS,从起点和终点同时广搜,但是必须搜索完全,否则不可以保证是最优解
 
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int
hashm[210][210];
int
hashy[210][210];
char
map[210][210];
int
sumy[210][210];
int
summ[210][210];
int
bfs[4][2] = {1,0,-1,0,0,1,0,-1};
struct
Node
{

    int
x,y;
    int
step;
};

int
yx,yy,mx,my;
int
n,m;
void
BFS()
{

  int
min = 1000000;
  queue < Node > qm,qy;
  Node topm,topy;
  topm.x = mx;topm.y = my,topm.step = 0;
  topy.x = yx,topy.y = yy,topy.step = 0;
  qm.push(topm);qy.push(topy);
  while
(!qm.empty() || !qy.empty())
  {

        Node tempm,tempy;
        tempm = qm.front();qm.pop();
        tempy = qy.front();qy.pop();
        //printf("the m %d %d have %d ",tempm.x,tempm.y,tempm.step);
        //printf("the y %d %d have %d ",tempy.x,tempy.y,tempy.step);
        if(map[tempm.x][tempm.y] == '@')
        {

            summ[tempm.x][tempm.y] = tempm.step;
            if
(sumy[tempm.x][tempm.y] != 0)
            {

                int
sum = summ[tempm.x][tempm.y] + sumy[tempm.x][tempm.y];
                if
(sum < min)
                   min = sum;
            }
        }

        if
(map[tempy.x][tempy.y] == '@')
        {

            sumy[tempy.x][tempy.y] = tempy.step;
            if
(summ[tempy.x][tempy.y] != 0)
            {

                int
sum = summ[tempy.x][tempy.y] + sumy[tempy.x][tempy.y];
                if
(sum < min)
                   min = sum;
            }
        }

       
        for
(int i = 0;i < 4;i ++)
        {

            int
mmx = tempm.x + bfs[i][0],mmy = tempm.y + bfs[i][1];
            int
mstep = tempm.step + 1;
            int
yyx = tempy.x + bfs[i][0],yyy = tempy.y + bfs[i][1];
            int
ystep = tempy.step + 1;
            if
(map[mmx][mmy] != '#' && hashm[mmx][mmy] == 0
            &&
mmx >= 1 && mmx <= n && mmy >= 1 && mmy <= m
            &&
map[mmx][mmy] != 'M' && map[mmx][mmy] != 'Y')
            {

                hashm[mmx][mmy] = 1;
                Node xinm;
                xinm.x = mmx;xinm.y = mmy;xinm.step = mstep;
                qm.push(xinm);
            }

            if
(map[yyx][yyy] != '#' && hashy[yyx][yyy] == 0
            &&
yyx >= 1 && yyx <= n && yyy >= 1 && yyy <= m
            &&
map[yyx][yyy] != 'M' && map[yyx][yyy] != 'Y')
            {

                hashy[yyx][yyy] = 1;
                Node xiny;
                xiny.x = yyx;xiny.y = yyy;xiny.step = ystep;
                qy.push(xiny);
            }
        }
       
    }

    printf("%d ",min * 11);
}
  
int
main()
{

    while
(~scanf("%d%d",&n,&m))
    {

        for
(int i = 1;i <= n;i ++)
           for
(int j = 1;j <= m;j ++)
            {

               hashm[i][j] = hashy[i][j] = sumy[i][j] = summ[i][j] = 0;
               scanf(" %c",&map[i][j]);
               if
(map[i][j] == 'M')
               {

                    mx = i;my = j;
                    hashm[i][j] = 1;
               }

               if
(map[i][j] == 'Y')
               {

                    yx = i;yy = j;
                    hashy[i][j] = 1;
               }
            }

         BFS();
    }
}
 
原文地址:https://www.cnblogs.com/GODLIKEING/p/3283547.html