http://acm.hdu.edu.cn/showproblem.php?pid=2612

Find a way

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

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:  2717 1254 1728 2102 1072
题意:分别从F,M出发去往某个@的最小距离。。
new note:这个码有bug但还是过了。。。没初始化ans数组inf
3 3
Y#@
.M#
..@
output: 66
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll ;
char a[][];
int n , m ;
int index[] , indey[];
int mx , my ;
int vis[][];
int dis[][] = {{ , } , {- , } , { , -} , { , }};
int step1[][][]; struct node
{
int x , y , step;
node(int x , int y , int step):x(x),y(y),step(step){}
node(){};
}; bool check(int x , int y)
{
if(x <= || y <= || x > n || y > m)
return false ;
if(!vis[x][y] && a[x][y] != '#')
return true ;
return false ;
} int bfs(int x , int y , int p)
{
queue<node>q;
node now , next ;
memset(vis , , sizeof(vis));
q.push(node(x , y , ));
vis[x][y] = ;
step1[p][x][y] = ;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i = ; i < ; i++)
{
next.x = now.x + dis[i][];
next.y = now.y + dis[i][];
next.step = now.step + ;
if(check(next.x , next.y))
{
vis[next.x][next.y] = ;
step1[p][next.x][next.y] = next.step ;
q.push(next);
}
}
}
return ; } int main()
{
while(~scanf("%d%d" , &n , &m))
{
getchar();
for(int i = ; i <= n ; i++)
{
for(int j = ; j <= m ; j++)
{
scanf("%c" , &a[i][j]);
if(a[i][j] == 'Y')
{
index[] = i;
indey[] = j;
}
else if(a[i][j] == 'M')
{
index[] = i;
indey[] = j;
}
}
getchar();
}
int ans = INF;
bfs(index[] , indey[] , );
bfs(index[] , indey[] , ); for(int i = ; i <= n ; i++)
{
for(int j = ; j <= m ; j++)
{
if(a[i][j] == '@')
{
ans = min(ans , step1[][i][j] + step1[][i][j]);
}
}
}
printf("%d\n" , ans*); } return ;
}

比赛后码:

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
const int N = 1e7 + ;
char s[][];
int vis[][];
int dir[][] = {{ , } , {- , } , { , } , { , -}};
int n , m ;
int ans[][][]; struct node{
int x , y , w;
}; void bfs(int t , int x , int y)
{
node now , next , last;
queue<node>q;
now.x = x , now.y = y , now.w = ;
q.push(now);
vis[x][y] = ;
while(!q.empty())
{
next = q.front();
q.pop();
if(s[next.x][next.y] == '@')
{
ans[t][next.x][next.y] = next.w ;
}
for(int i = ; i < ; i++)
{
int xx = next.x + dir[i][];
int yy = next.y + dir[i][];
int nw = next.w + ;
if(xx < || xx >= n || yy < || yy >= m)
{
continue ;
}
if(vis[xx][yy] || s[xx][yy] == '#')
{
continue ;
}
vis[xx][yy] = ;
last.x = xx , last.y = yy , last.w = nw ;
q.push(last);
}
}
} int main()
{ while(~scanf("%d%d" , &n , &m))
{
int x , y , x1 , y1 ;
memset(vis , , sizeof(vis));
memset(ans , INF , sizeof(ans));//没有考虑到达不了的@(但最少有一个@可达)
//到达不了的@应该为无穷大,否则为0则答案错误
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
cin >> s[i][j] ;
if(s[i][j] == 'Y')
{
x = i , y = j ;
}
if(s[i][j] == 'M')
{
x1 = i , y1 = j ;
}
}
}
bfs( , x , y);
memset(vis , , sizeof(vis));
bfs( , x1 , y1);
int mi = INF ;
for(int i = ; i < n ; i++)
{
for(int j = ; j < m ; j++)
{
if(s[i][j] == '@')
{
mi = min(mi , ans[][i][j] + ans[][i][j]);
}
}
}
cout << mi * << endl ; } return ;
}

bfs(双向bfs加三维数组)的更多相关文章

  1. UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)

    题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

  2. UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)

    题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...

  3. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  4. POJ 3126 Prime Path 解题报告(BFS & 双向BFS)

    题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...

  5. POJ1915 BFS&双向BFS

    俩月前写的普通BFS #include <cstdio> #include <iostream> #include <cstring> #include <q ...

  6. 双向BFS和启发式搜索的应用

    题目链接 P5507 机关 题意简述   有12个旋钮,每个旋钮开始时处于状态 \(1\) ~ \(4\) ,每次操作可以往规定方向转动一个旋钮 (\(1\Rightarrow2\Rightarrow ...

  7. 洛谷 P1379 八数码难题(map && 双向bfs)

    题目传送门 解题思路: 一道bfs,本题最难的一点就是如何储存已经被访问过的状态,如果直接开一个bool数组,空间肯定会炸,所以我们要用另一个数据结构存,STL大法好,用map来存,直接AC. AC代 ...

  8. BFS、双向BFS和A*

    BFS.双向BFS和A* Table of Contents 1. BFS 2. 双向BFS 3. A*算法 光说不练是无用的.我们从广为人知的POJ 2243这道题谈起:题目大意:给定一个起点和一个 ...

  9. HDOJ2579,BFS(三维数组标记)

    三维数组对于我这个萌萌的新手来说真是酷酷的,帅到不行,,, 三维数组前面还看到空间的哪一题HDOJ1240,这一题时间上的标记,更酷了!!! 不说废话了,贴一发代码: #include <std ...

随机推荐

  1. 基于谷歌开源的TensorFlow Object Detection API视频物体识别系统搭建自己的应用(四)

    本章主要内容是利用mqtt.多线程.队列实现模型一次加载,批量图片识别分类功能 目录结构如下: mqtt连接及多线程队列管理 MqttManager.py # -*- coding:utf8 -*- ...

  2. [Luogu2015]二叉苹果树(树形dp)

    [Luogu2015] 二叉苹果树 题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. ...

  3. pandas数据读取(DataFrame & Series)

    1.pandas数据的读取 pandas需要先读取表格类型的数据,然后进行分析 数据说明 说明 pandas读取方法 csv.tsv.txt 用逗号分割.tab分割的纯文本文件 pd.read_csv ...

  4. 在Node.js环境下使用Express创建Web项目实例

    序:如果你还不知道Node.js是什么,那么你可以先看看这篇:Node.js 究竟是什么?或者任何关于它的介绍. 一.安装Node.js 1.进入Node.js官网下载并安装 2.启动cmd输入命令查 ...

  5. hadoop中的一些术语介绍

    1.MR作业是客户端执行的一个工作单元:包括输入数据,MR的程序和配置信息. Hadoop将作业分成若干个任务task来执行,分为两种任务:map和reduce任务.这些任务运行在集群的节点上,并通过 ...

  6. RestTemplate 发送post请求

    springboot使用restTemplate post提交值 restTemplate post值 post提交有 FormData和Payload 两种形式: 第一种是formdata形式,在h ...

  7. python 按多维列表中的某一个元素位进行排序

    import os,re top = os.popen("tasklist") process_list = [] split_r = r"\s+" memor ...

  8. springboot-启动一段时间图片不能上传

    问题:[B2B]后台服务.PC服务.APP服务.仓储服务,启动一段时间图片不能上传. 原因:/tmp下以tomcat开头的目录被清理了. 处理方案:1.找到涉及服务器 注:后台服务.PC服务.APP服 ...

  9. JAVA中的反射机制 详解

    主要介绍以下几方面内容 理解 Class 类 理解 Java 的类加载机制 学会使用 ClassLoader 进行类加载 理解反射的机制 掌握 Constructor.Method.Field 类的用 ...

  10. 对https的研究

    HTTPS简介 超文本传输安全协议(英语:Hypertext Transfer Protocol Secure,缩写:HTTPS,常称为HTTP over TLS,HTTP over SSL或HTTP ...