ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU2612--find a way

2021-01-29 17:01:49  阅读:234  来源: 互联网

标签:-- result1 result2 next int MAXN way now find


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有