题目:

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

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 题意:题意大概是在一幅地图上面,一直两个人的位置,求这两个人到KFC的最小距离之和,最后乘以11得到所用最小时间和
(KFC用‘@’表示,墙用‘#’表示,两个人分别为M和Y,‘.’表示能通过)
分析:分别求用bfs求两个人到KFC的最短距离,(我将maxi设置成的40050,因为m<=200,n>=2,我用的40050AC了),将每一条
KFC的距离进行比较得出最短距离
AC代码:
#include<iostream>
#include<queue>
#include<string.h>
#include<string>
using namespace std;
struct knot
{
int x,y; //结点的横纵坐标
int t; //到达当前结点所走的步数
};
knot ai,temp; int s1[][]; //判断所在结点是否为遍历过
int n,m; //地图大小
int s2[][][];
int flag[][]={,,-,,,,,-}; //用于移动的方向
char a[][];
int mini(int a,int b)
{
return a>b?b:a;
}
void bfs(int ix,int iy,int ik) //广度优先查找
{
queue<knot>s; //建立一个装knot的队列
temp.x=ix; //temp作为中间变量,即主函数中需要查找的点
temp.y=iy;
temp.t=; //移动步数初始化为0;
s.push(temp);
while (!s.empty())
{
ai=s.front();
s.pop();
temp.t=ai.t+; //移动步数加1
for (int i=;i<;i++)
{
temp.x=ai.x+flag[i][]; //移动x坐标
temp.y=ai.y+flag[i][]; //移动y坐标
if (temp.x>=&&temp.x<n&&temp.y>=&&temp.y<m&&s1[temp.x][temp.y]==&&a[temp.x][temp.y]!='#') //判断还能不能朝着同一个方向继续前行
{
if (a[temp.x][temp.y]=='@')
s2[ik][temp.x][temp.y]=temp.t;
s1[temp.x][temp.y]=;
s.push(temp);
}
} }
}
int main()
{ int xm,ym,xy,yy; while (cin>>n>>m)
{ for (int i=;i<n;i++)
{
for (int j=;j<m;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,,sizeof(s1));
memset(s2,,sizeof(s2));
s1[xm][ym]=;
bfs(xm,ym,);
memset(s1,,sizeof(s1)); //在第一次进行bfs时,s1数据发生改变,因此在第二次bfs前需要清零;memset的头文件为#include<cstring>
s1[xy][yy]=;
bfs(xy,yy,);
for (int i=;i<n;i++)
for (int j=;j<m;j++)
if (s2[][i][j]&&s2[][i][j])
maxi=mini(maxi,s2[][i][j]+s2[][i][j]); //求最小距离
cout << maxi* << endl; //每一步需要11分钟
}
return ;
}

 

Find a way (广度优先搜索)的更多相关文章

  1. 图的广度优先搜索(BFS)

    把以前写过的图的广度优先搜索分享给大家(C语言版) #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 ...

  2. 广度优先搜索(BFS)

    定义 维基百科:https://en.wikipedia.org/wiki/Breadth-first_search 给定图G=(V,E)和一个可识别的源结点s,广度优先搜索对图G中的边进行系统性的探 ...

  3. 总结A*,Dijkstra,广度优先搜索,深度优先搜索的复杂度比较

    广度优先搜索(BFS) 1.将头结点放入队列Q中 2.while Q!=空 u出队 遍历u的邻接表中的每个节点v 将v插入队列中 当使用无向图的邻接表时,复杂度为O(V^2) 当使用有向图的邻接表时, ...

  4. ACM题目————图的广度优先搜索

    题目描述 图的广度优先搜索类似于树的按层次遍历,即从某个结点开始,先访问该结点,然后访问该结点的所有邻接点,再依次访问各邻接 点的邻接点.如此进行下去,直到所有的结点都访问为止.在该题中,假定所有的结 ...

  5. SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

    数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...

  6. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  7. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  8. HDU 1242 Rescue (BFS(广度优先搜索))

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  9. SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...

  10. 广度优先搜索BFS

    广度优先搜索可以形成一个广度优先搜索树 算法时间为O(V+E),两重循环 输入:图g,起点start(int) 需要的数据结构:队列Q.color数组(存放每个顶点的颜色) 算法过程: 1. 预处理: ...

随机推荐

  1. classification.py

    # -*- coding: utf-8 -*-"""View more, visit my tutorial page: https://morvanzhou.githu ...

  2. python中的变量对象小结2

    # .变量名和数据内容是分开存储的. # .数据保存在内存中的一个位置(地址). # .变量中保存着数据在内存中的地址. # 引用就是变量中记录数据的地址. #不可变变量,重新赋值时会重新开辟一个地址 ...

  3. 关于nginx配置的一个报错connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory)

    针对配置php的情况: linux服务器一般提示这个 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) ...

  4. python妹子图爬虫5千张高清大图突破防盗链福利5千张福利高清大图

    meizitu-spider python通用爬虫-绕过防盗链爬取妹子图 这是一只小巧方便,强大的爬虫,由python编写 所需的库有 requests BeautifulSoup os lxml 伪 ...

  5. linux 中的.so和.a文件

    Linux下的.so是基于Linux下的动态链接,其功能和作用类似与windows下.dll文件. 下面是关于.so的介绍: 一.引言 通常情况下,对函数库的链接是放在编译时期(compile tim ...

  6. [LC] 82. Remove Adjacent Repeated Characters IV

    Repeatedly remove all adjacent, repeated characters in a given string from left to right. No adjacen ...

  7. debian8.8安装sougou输入法

    传送门:http://www.cnblogs.com/ligongzi/p/6137601.html 亲测可用

  8. [LC] 270. Closest Binary Search Tree Value

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  9. CSS样式表---------第三章:样式属性

    三.样式属性 1.背景与前景 background-color:#90; ------------背景色,样式表优先级高. background-image:url(路径)-------------- ...

  10. Docker系列八: 数据卷

    什么是数据卷 生成环境中使用docker的过程中,往往需要对数据进行持久化,或者需要多个容器之间进行数据共享,这个就涉及到了容器数据管理 容器中管理数据主要有两种方式: 数据卷:容器内数据之间映射到本 ...