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 OutputFor 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 开始的时候想都没想就先找出所有的中间点,然后计算每个中间点,Y和M到他们的最短距离,然后求出最短的。
果然超时
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
int n,m,dx[]={,-,,},dy[]={,,,-},num,sum[maxn],vis[][];
char mapn[][];
struct point{
int x,y;
};
point p[maxn];
struct node{
int x,y,step;
};
void bfs(int a,int b,int c,int d,int cnt){
node p;
p.x = a,p.y = b,p.step = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
if(tmp.x == c && tmp.y == d){
sum[cnt] += tmp.step;
return;
}
for(int i=;i<;i++){
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if(xx> && xx<=n && yy> && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
vis[xx][yy] = ;
node tp;
tp.x = xx,tp.y = yy,tp.step = tmp.step + ;
q.push(tp);
}
}
}
}
int main()
{
while(cin >> n >> m){
num = ;
memset(sum,,sizeof(sum));
if(!n || !m){
break;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin >> mapn[i][j];
if(mapn[i][j] == '@'){
p[num].x = i,p[num].y = j,num++;
}
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == 'Y' || mapn[i][j] == 'M'){
for(int k=;k<num;k++){
memset(vis,,sizeof(vis));
bfs(i,j,p[k].x,p[k].y,k);
}
}
}
}
sort(sum,sum+num);
cout << sum[] * << endl;
}
return ;
}

后面是先计算好Y,M到每个点的最短距离,这样只需要循环Y,M的个数次数就行,不需再套上中间点个数的循环。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
#define inf 0x1f1f1f1f
int n,m,dx[]={,-,,},dy[]={,,,-},num,sum[][][],vis[][],z;
char mapn[][];
struct node{
int x,y,step;
};
void bfs(int a,int b){
memset(vis,,sizeof(vis));//在这里重置vis数组
node p;
p.x = a,p.y = b,p.step = ;
vis[p.x][p.y] = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
for(int i=;i<;i++){
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if(xx> && xx<=n && yy> && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
vis[xx][yy] = ;
node tp;
tp.x = xx,tp.y = yy,tp.step = tmp.step + ;
sum[tp.x][tp.y][z] = tp.step;
q.push(tp);
}
}
}
}
int main()
{
while(cin >> n >> m){
num = ;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
sum[i][j][] = inf;
sum[i][j][] = inf;//注意记录距离的sum数组一定要初始化为一个很大的数
}
}
if(!n || !m){
break;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin >> mapn[i][j];
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == 'Y'){
z = ;
bfs(i,j);
}
if(mapn[i][j] == 'M'){
z = ;
bfs(i,j);
}
}
}
int ans = inf;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == '@'){
ans = min(ans,sum[i][j][] + sum[i][j][]);
}
}
}
cout << ans* << endl;
}
return ;
}

[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612的更多相关文章

  1. [kuangbin带你飞]专题一 简单搜索 题解报告

    又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...

  2. [kuangbin带你飞]专题一 简单搜索(回顾)

    A - 棋盘问题 POJ - 1321 注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了. AC代码: #include<iostream> #include< ...

  3. [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple

    //Memory Time //2236K 32MS #include<iostream> using namespace std; ]; //保存每次mod n的余数 //由于198的余 ...

  4. [kuangbin带你飞]专题一 简单搜索 棋盘问题

    题来:链接https://vjudge.net/problem/OpenJ_Bailian-132 J - 棋盘问题 1.题目: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别. ...

  5. [kuangbin带你飞]专题一 简单搜索

            ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题   328 / 854 Problem B POJ 2251 Dungeon Ma ...

  6. [kuangbin带你飞]专题一 简单搜索 回顾总结

    第二题:bfs,忘了将queue清空. 第三题:bfs,记得使用vis数组,防止重复入队

  7. 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  8. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  9. Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

随机推荐

  1. RocketMQ中Producer消息的发送

    上篇博客介绍过Producer的启动,这里涉及到相关内容就不再累赘了 [RocketMQ中Producer的启动源码分析] Producer发送消息,首先需要生成Message实例: public c ...

  2. Thinkphp5.0快速入门笔记(1)

    学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. Composer安装和更新 C ...

  3. php 生成随机字符串,数字,大写字母,小写字母,特殊字符可以随意组合

    * 生成随机字符串* @param int       $length  要生成的随机字符串长度* @param string    $type    随机码类型:0,数字+大小写字母:1,数字:2, ...

  4. Intent 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android Intent 是一个消息传递对象,主要用于组建之间的通讯,例如:启动Activit ...

  5. Spring的数据库编程浅入浅出——不吹牛逼不装逼

    Spring的数据库编程浅入浅出——不吹牛逼不装逼 前言 上文书我写了Spring的核心部分控制反转和依赖注入,后来又衔接了注解,在这后面本来是应该写Spring AOP的,但我觉得对于初学者来说,这 ...

  6. 【CodeForces - 1200C】Round Corridor (数论gcd)

    Round Corridor  Descriptions Amugae位于一个非常大的圆形走廊中.走廊由两个区域组成.内部区域等于nñ扇区,外部区域等于m米部门.在相同区域(内部或外部)的每对扇区之间 ...

  7. XML学习(一)

    本文主要记录xml学习过程中的一些笔记,包括xml作用,语法以及解析. 1.HTML和XML的区别 1.1.HTML 名称:     HyperText Markup Languae(超文本标记语言) ...

  8. CPU中的cache结构以及cache一致性

    一. 引子 在多线程环境中,经常会有一些计数操作,用来统计线上服务的一些qps.平均延时.error等.为了完成这些统计,可以实现一个多线程环境下的计数器类库,方便记录和查看用户程序中的各类数值.在实 ...

  9. ABAP_增强点查找

    *&---------------------------------------------------------------------* *& Report Z_FIND_EN ...

  10. (17)ASP.NET Core EF基于数据模型创建数据库

    1.简介 使用Entity Framework Core构建执行基本数据访问的ASP.NET Core MVC应用程序.使用迁移(Migrations)基于数据模型创建数据库,你可以在Windows上 ...