当前位置:网站首页 > 编程语言 > 正文

广度优先搜索流程图(广度优先搜索图解)



题目:

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.
InputThe 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




























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

Sample Output

#include
#include
#include
#include
using namespace std;
struct knot
{
    int x,y;     //结点的横纵坐标
    int t;       //到达当前结点所走的步数
};
knot ai,temp;




























int s1[205][205];    //判断所在结点是否为遍历过
int n,m;             //地图大小
int s2[2][205][205];
int flag[4][2]={1,0,-1,0,0,1,0,-1};    //用于移动的方向
char a[205][205];
int mini(int a,int b)
{
    return a>b?b:a;
}
void bfs(int ix,int iy,int ik)   //广度优先查找
{
    queue s;      //建立 一个装knot的队列
    temp.x=ix;         //temp作为中间变量,即主函数中需要查找的点
    temp.y=iy;
    temp.t=0;          //移动步数初始化为0;
    s.push(temp);
    while (!s.empty())
    {
        ai=s.front();
        s.pop();
        temp.t=ai.t+1;        //移动步数加1
        for (int i=0;i<4;i++)
        {
            temp.x=ai.x+flag[i][0];        //移动x坐标
            temp.y=ai.y+flag[i][1];        //移动y坐标
            if (temp.x>=0&&temp.x =0&&temp.y









































































             //判断还能不能朝着同一个方向继续前行
            {
                if (a[temp.x][temp.y]==‘@‘)
                 s2[ik][temp.x][temp.y]=temp.t;
                 s1[temp.x][temp.y]=1;
                 s.push(temp);
            }
        }



















    }
}
int main()
{







     int xm,ym,xy,yy;

     while (cin>>n>>m)
     {

         for (int i=0;i
         {
             for (int j=0;j
              { cin>>a[i][j];
                  if (a[i][j]==‘M‘)       //确定M所在位置的坐标
                 xm=i,ym=j;
                  if (a[i][j]==‘Y‘)    //确定Y所在位置的坐标
                xy=i,yy=j;
             }























         }
         int maxi=;   //注意maxi初始化的位置,在输入一组地图以后maxi会发生改变
       memset(s1,0,sizeof(s1));
       memset(s2,0,sizeof(s2));
         s1[xm][ym]=1;
         bfs(xm,ym,1);
        memset(s1,0,sizeof(s1));  
















    //在第一次进行bfs时,s1数据发生改变,因此在第二次bfs前需要清零;memset的头文件为#include
         s1[xy][yy]=1;
         bfs(xy,yy,2);
         for (int i=0;i
             for (int j=0;j
                if (s2[1][i][j]&&s2[2][i][j])
                    maxi=mini(maxi,s2[1][i][j]+s2[2][i][j]);   //求最小距离
         cout << maxi*11 << endl;     //每一步需要11分钟
     }
    return 0;
}




























Find a way (广度优先搜索)

到此这篇广度优先搜索流程图(广度优先搜索图解)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • 打印机共享失败11b(打印机共享失败x709)2026-04-16 10:27:10
  • ubuntu 下载源码(ubuntu下载yum)2026-04-16 10:27:10
  • tpami(tpami期刊)2026-04-16 10:27:10
  • ip域名(ip域名解析)2026-04-16 10:27:10
  • win10 u盘系统启动盘制作(win10的u盘启动盘制作)2026-04-16 10:27:10
  • seatbelts翻译(seat belt翻译中文)2026-04-16 10:27:10
  • 拆包机视频(拆包机操作规程)2026-04-16 10:27:10
  • w25q256fv如何烧录(25q128烧录)2026-04-16 10:27:10
  • linux 目录删除(linux目录删除如何恢复)2026-04-16 10:27:10
  • max30102心率代码(max30102心率不准)2026-04-16 10:27:10
  • 全屏图片