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. 配置好运行后Error creating context 'spring.root': Could not load type from string value

    在Webconfig文件的当前项目下引用相关项目

  2. Python Warning

    Python Warning 官方文档 概念 warning是内置的异常类,所有用户的warning应该继承于内置warning异常. 警告控制的两个阶段: 通过异常过滤器控制警告是否触发,通过fil ...

  3. NLP之CRF应用篇(序列标注任务)

    1.CRF++的详细解析 完成的是学习和解码的过程:训练即为学习的过程,预测即为解码的过程. 模板的解析: 具体参考hanlp提供的: http://www.hankcs.com/nlp/the-cr ...

  4. Spring security 知识笔记【内存角色授权】

    一.原有的配置文件中,增加注解@EnableGlobalMethodSecurity(prePostEnabled = true) 二.原有配置文件中,内存新建账号的时候添加角色 package El ...

  5. 阅读java编程思想之一切都是对象

    温故而知新--- 1. 用句柄操作对象 现实生活中,我们可以把遥控器当作句柄,电视机当作对象.当我们拿到句柄(遥控器)的时候,我们是可以控制对象的(电视机).比如说调节音量大小,色彩等.那么在程序里, ...

  6. BAT公司职级体系及薪水解密

    BAT公司职级体系及薪水解密 互联网圈有这么一句话:百度的技术,阿里的运营,腾讯的产品.那么代表互联网三座大山的BAT,内部人才体系有什么区别呢? 先谈谈腾讯的体系. 首先是腾讯. 1.职级: 腾讯职 ...

  7. LeetcCode 27:移除元素 Remove Element(python、java)

    公众号:爱写bug 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) ...

  8. 【MySQL】Mariadb安装

    Mariadb安装 1.解压 [root@oradb bin]# tar zxvf mariadb-10.3.18-linux-x86_64.tar.gz [root@oradb bin]# mv m ...

  9. THUPC2019/CTS2019/APIO2019自闭记

    自闭了,自闭选手不配拥有游记.

  10. 七雄Q传封包辅助技术探讨回忆贴

    前言 网页游戏2013年左右最火的类型最烧钱游戏,当年的我也掉坑了.为了边玩还满足码农精神我奋力的学习如何来做外挂.2013年我工作的第二个年头.多一半…介绍下游戏<七雄Q传>是北京游戏谷 ...