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. Git同步问题

    1. 无法合并不相关历史记录 git pull origin master --allow-unrelated-histories

  2. Dubbo源码学习总结系列七---注册中心

    Dubbo注册中心是框架的核心模块,提供了服务注册发现(包括服务提供者.消费者.路由策略.覆盖规则)的功能,该功能集中体现了服务治理的特性.该模块结合Cluster模块实现了集群服务.Dubbo管理控 ...

  3. websocket和基于swoole的简易即时通讯

    这里描述个基于swoole的websocket 匿名群聊 UI <!DOCTYPE html> <html> <head> <meta charset=&qu ...

  4. 10-基于TMS320C6678+XC7K325T的6U CPCI Full Camera Link图像处理平台

    基于TMS320C6678+XC7K325T的6U CPCI Full Camera Link图像处理平台 1.板卡概述 板卡由我公司自主研发,基于6UCPCI架构,处理板包含一片TI DSPTMS3 ...

  5. Taro -- 原生微信小程序转taro

    微信小程序转Taro  (转发https://nervjs.github.io/taro/docs/taroize.html) Taro 可以将你的原生微信小程序应用转换为 Taro 代码,进而你可以 ...

  6. 记一次在mac上源码编译curl,使其支持NSS的过程

    一.背景 在一次学习https原理的过程中,希望客户端指定特定的cipher suites来抓包分析SSL/TLS的握手过程,就想到了使用curl工具,而不是使用浏览器. 接下来使用man curl找 ...

  7. jquery 模态对话框传值,删除,新增表格行

    个人的练习代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  8. flask之模板之继承

    一:继承 基类模板base.html  中在进行挖坑 {% block 坑的名字%}{% endblock %} 子类模板test.html 中 通过 {% extends "base.ht ...

  9. JS原型和原型链(3)

    构造函数创建对象: function Person() { } var person = new Person(); person.name = 'Kevin'; console.log(person ...

  10. final、finally、 finalize区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11444366.html final final可以用来修饰类.方法.变量,分别有不同的意义,final ...