Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25379    Accepted Submission(s): 8243

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
 



Recommend
yifenfei   |   We have carefully selected several similar problems for you:  1254 1728 1072 1175 2579 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
这道题因为有两个人要走
所以此处对两个人
每一个KFC
进行分别的搜索
搜索到一个KFC对应的两个值相加最小
就直接输出
但是要注意一个问题
先上我的TLE代码
 //Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
struct node
{
int x,y,step;
};
queue<node> q;
int n,k,vis[][];
char map[][];
int dir[][]={{,},{-,},{,-},{,}};//四个方向
int bfs(int a,int b,int r1,int r2)
{
while(!q.empty())
q.pop();
int i;
q.push(node{a,b,});
vis[a][b]=;
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==r1&&t.y==r2)
return t.step;
for(i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
if(dx>=&&dy>=&&dx<n&&dy<k&&!vis[dx][dy]&&map[dx][dy]!='#')//正常搜索
{
vis[dx][dy]=;
q.push(node{dx,dy,t.step+});
}
}
}
return MMAX;//如果搜不到就返回一个不能使ans改变的值
}
int main()
{
int a1,b1,a2,b2,r1[],r2[];
while(scanf("%d%d",&n,&k)!=EOF)
{
int t=,ans=MMAX,temp=;
for(int i=;i<n;i++)
{
scanf("%s",&map[i]);
for(int j=;j<k;j++)
{
if(map[i][j]=='Y')
{
a1=i;
b1=j;
}
if(map[i][j]=='M')//求出两个点的位置作为起点
{
a2=i;
b2=j;
}
if(map[i][j]=='@')//如果是KFC放到数组里面
{
r1[t]=i;
r2[t]=j;
t++;
}
}
}
for(int i=;i<t;i++)//寻找能到的KFC的最小值
{
temp=;
memset(vis,,sizeof(vis));
temp+=bfs(a1,b1,r1[i],r2[i]);
memset(vis,,sizeof(vis));
temp+=bfs(a2,b2,r1[i],r2[i]);
ans=min(ans,temp);
}
printf("%d\n",ans*);
}
return ;
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

乍一看这个代码似乎还很有道理

然而

但是为什么会T呢

原因其实很简单

就是因为bfs算法时间复杂度较高

如果多次进行bfs的话

铁定超时

此处一个KFC就进行一次搜索

导致超时

于是笔者想出了一个较为不错的方法

只进行一次bfs

记录下所能到达的位置所需的步数

这样只有两次深搜即可

但是这里要注意一个小小的bug

就是KFC可能到不了

笔者因为这个WA了几次的

下面附上我精确注释

谁都能看懂的代码

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 //Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
struct node
{
int x,y;
}; int n,k,vis[][];
char map[][];
int dir[][]={{,},{-,},{,-},{,}};//四个方向
void bfs(int a,int b,int step[][])//暴力枚举能到的地方各点的步数
{
int i;
queue<node> q;
q.push(node{a,b});
vis[a][b]=;
while(!q.empty())
{
node t=q.front();
q.pop();
for(i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
if(dx>=&&dy>=&&dx<n&&dy<k&&!vis[dx][dy]&&map[dx][dy]!='#'&&!vis[dx][dy])//基本搜索
{
vis[dx][dy]=;
step[dx][dy]=step[t.x][t.y]+;
q.push(node{dx,dy});
}
}
}
}
int main()
{
int a1,b1,a2,b2,step1[][],step2[][];
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(step1,,sizeof(step1));
memset(step2,,sizeof(step2));
int t=,ans=MMAX;
for(int i=;i<n;i++)
{
scanf("%s",map[i]);
for(int j=;j<k;j++)
{
if(map[i][j]=='Y')
{
a1=i;
b1=j;
}
if(map[i][j]=='M')//把两个点记录下来
{
a2=i;
b2=j;
}
}
}
memset(vis,,sizeof(vis));
bfs(a1,b1,step1);
memset(vis,,sizeof(vis));
bfs(a2,b2,step2);
for(int i=;i<n;i++)//遍历所有元素,使得能走到KFC且为最小
{
for(int j=;j<k;j++)
{
if(map[i][j]=='@'&&step1[i][j]&&step2[i][j]&&(step1[i][j]+step2[i][j]<ans))//注意此处step为0代表KFC到不了
ans=step1[i][j]+step2[i][j];
}
}
printf("%d\n",ans*);
}
return ;
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

此时能够AC 时间为31ms

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Note:需要注意下bfs的使用次数即可

2018-11-16  00:25:12  Author:LanceYu

HDU 2612 Find a way 题解的更多相关文章

  1. HDU 2612 Find a way(找条路)

    HDU 2612 Find a way(找条路) 00 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)   Problem  ...

  2. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  3. BFS(最短路) HDU 2612 Find a way

    题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...

  4. HDU 2612 Find a way(双向bfs)

    题目代号:HDU 2612 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 M ...

  5. 题解报告:hdu 2612 Find a way(双bfs)

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  6. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  7. hdu 2612 Find a way

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Description Pass a year learning in H ...

  8. HDU 2612 Find a way bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...

  9. (广搜) Find a way -- hdu -- 2612

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others) ...

随机推荐

  1. Python面向对象 | 鸭子方法

    鸭子类型 如果看起来像.叫声像而且走起路来像鸭子,那么它就是鸭子’.python程序员通常根据这种行为来编写程序.例如,如果想编写现有对象的自定义版本,可以继承该对象,也可以创建一个外观和行为像,但与 ...

  2. 公告&留言板

    这里是公告&留言板.无意义的评论可能会被删除. 2019.10.4 感觉开学之后状态一直都布星啊,可能会在博客里总结一些前面学的东西. 2019.10.14 咕咕咕咕咕咕咕咕 2019.10. ...

  3. [LeetCode] 477. Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  4. mod35云掩膜产品用法

    在网上一直没找到一个明确说怎么用MOD35产品的,都是说去看用户手册,第一次看了过一段时间我又忘记怎么搞了,赶紧记下来. 而且现在才发现第一次自己搞的都弄错了. 简单的判断是否是云,只要读取mod35 ...

  5. pytorch_05_神经网络

    神经网络 一些神经元的输出会变成另外一些神经元的输入,一般以层来组织,最常见的是全连接神经网络,其中两个相邻层中每一个层的所有神经元与另一个层的所有神经元相连,每个层内部的神经元不相连. 一般的,N层 ...

  6. 【开源监控】Grafana介绍与安装

    Grafana介绍与安装 Grafana介绍 场景:由于业务场景,有多个组织机构.需要在某个组织结构下,完成对本机构下的系统的实时监控以及可视化展示.底层已经用zabbix对监控指标做了数据的采集. ...

  7. JavaIO学习:转换流

    转换流 1.涉及到的类 InputStreamReader:将InputStream转换为Reader OutputStreamWriter:将Writer转换为OutputStream 2.构造器 ...

  8. 人生苦短,我用Python(目录)

    目前Django分类的文章正在整顿中,届时将呈现更加丰富的内容,另外整体排版也将会更改,感谢支持!! 一.Python基础 二.并发编程 三.数据库 四.Web 五.Python Web 框架 六.爬 ...

  9. gitbub高效查找优秀项目

    in:name example   名字中带有example的项目 in:readme example 在readme文件带有example的项目 in:description example 描述里 ...

  10. winform窗体自适应大小

    1.添加一个类class AutoSizeFormClass { //(1).声明结构,只记录窗体和其控件的初始位置和大小. public struct controlRect { public in ...