其他分享
首页 > 其他分享> > HDU2612--find a way

HDU2612--find a way

作者:互联网

HDU2612–find a way

一、题目描述
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
二、解题思路
这道题采用广度优先搜索的方法解决,借助了队列的知识,从起点找@重点与迷宫问题相似,创建两个二维数组,进行两次广度优先搜索,把起点到终点的步数放在二维数组中,然后找到在同一个位置的终点步数最小的输出即可。
三、参考代码

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
#define inf 0x3f3f3f3f
#define MAXN 300
int visit[MAXN][MAXN];
char Map[MAXN][MAXN];
int result1[MAXN][MAXN];
int result2[MAXN][MAXN];
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int n,m;
struct node
{
    int x;
    int y;
    int step;
};
void BFS(int x,int y,int result[][MAXN])
{
    queue<node> que;
    node str;
    str.x=x;
    str.y=y;
    str.step=0;
    que.push(str);
    memset(visit,0,sizeof(visit));
    visit[x][y]=1;
    while(!que.empty())
    {
        node now=que.front();
        que.pop();
        if(Map[now.x][now.y]=='@')
        {
            result[now.x][now.y]=now.step;
        }
        for(int i=0; i<4; i++)
        {
            node next;
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.step=now.step+1;
            if(next.x>=1&&next.x<=n&&next.y>=1&&next.y<=m&&!visit[next.x][next.y]&&Map[next.x][next.y]!='#')
            {
                visit[next.x][next.y]=1;
                que.push(next);
            }
        }
    }
}
int main()
{
    while(cin>>n>>m)
    {
        int x1,y1,x2,y2;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
                cin>>Map[i][j];
        }
        memset(result1,0,sizeof(result1));
        memset(result2,0,sizeof(result2));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(Map[i][j]=='Y')
                {
                    x1=i;
                    y1=j;
                }
                if(Map[i][j]=='M')
                {
                    x2=i;
                    y2=j;
                }
            }
        }
        BFS(x1,y1,result1);
        BFS(x2,y2,result2);
        int MIN=inf;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(MIN>result1[i][j]+result2[i][j]&&result1[i][j]&&result2[i][j])
                    MIN=result1[i][j]+result2[i][j];
            }
        }
        cout<<MIN*11<<endl;
    }
    return 0;
}

四、总结
熟悉迷宫问题之后这道题就比较容易解决了,考虑辅助数组便于计算最终的结果。还有我个人感觉在这类题中使用深度优先搜索与广度优先搜索与数据结构书中用邻接表和邻接矩阵使用的两个搜索不太一样,邻接表和邻接矩阵中的两个搜索方法都有明显的特点,比如深度优先搜索类似于树的先序遍历,而广度优先搜索目的是为了尽可能先对横向进行搜索,在迷宫这类似的问题中,到目前为止我并没有观察到有数据结构书中两种搜索方法的特点,很有可能是因为我还是一名小白,欢迎大佬进行点评讲解,谢谢!

标签:--,result1,result2,next,int,MAXN,way,now,find
来源: https://blog.csdn.net/Jarvis223/article/details/113395631