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. 1. AtomicInteger 、Unsafe 及 CAS方法的整理

    本文摘自: https://blog.csdn.net/fanrenxiang/article/details/80623884 http://ifeve.com/sun-misc-unsafe/ h ...

  2. 模块之re模块

    八.正则表达式 1.1首先我们先了解re模块与正则表达式的关系: re模块与正则表达式之间的关系 正则表达式不是python独有的 它是一门独立的技术所有的编程语言都可以使用正则,但是如果你想在pyt ...

  3. 使用Python和AWK两种方式实现文本处理的长拼接案例

    最近由于业务系统新需求的需要,我们平台需要将供应商G提供一类数据转换格式后提供给客户K.比较头疼是供应商G提供的数据都是在Windows下使用Excel存储的,而客户K先前与我们相关对接人员商定的数据 ...

  4. FTP服务器原理及配置

    控制连接 21端口  用于发送ftp命令 数据连接 20端口  用于上传下载数据 数据连接的建立类型: 1主动模式: 服务器主动发起的数据连接 首先由客户端的21 端口建立ftp控制连接 当需要传输数 ...

  5. python实现通过企业微信发送消息

    实现了通过企业微信发送消息,平时用于运维的告警还是不错的,相对于邮件来说,实时性更高,不过就是企业微信比较麻烦,此处不做过多解释. 企业微信api的详细请看:http://work.weixin.qq ...

  6. flashback table

    前提:开启回收站 查看回收站状态 SQL> show parameter recyclebin; NAME TYPE VALUE -------------------------------- ...

  7. CF dp 题(1500-2000难度)

    前言 从后往前刷 update 新增 \(\text{\color{red}{Mark}}\) 标记功能,有一定难度的题标记为 \(\text{\color{red}{红}}\) 色. 题单 (刷过的 ...

  8. springboot 整合Druid

    Druid连接池监控配置 1) 引入依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <depende ...

  9. Java技术中如何使用keepalived实现双机热备

    Keepalived简介 Keepalived是Linux下一个轻量级别的高可用解决方案.高可用(High Avalilability,HA),其实两种不同的含义:广义来讲,是指整个系统的高可用行,狭 ...

  10. 如何安装 mcrypt

    #cd libmcrypt-2.5.8 #./configure #make #make install 说明:libmcript默认安装在/usr/local3.安装mhash #tar -zxvf ...