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. vs code 同步

    vs code 同步需要在github上配置好gist id, 将gist id添加至setting.json中, 然后再在localsetting中设置access token,  gist id ...

  2. webpack的一般性配置及说明

    1.webpack的常规配置 先给出一个示例: const path = require('path'); const HtmlWebpackPlugin = require('html-webpac ...

  3. CCF CSP/CCSP报名费优惠的方法以及常见疑问

    目录 1. 本文地址 2. 认证作用 2.1. 高校认可 2.2. 赛事认可 2.3. 企业认可 3. 报名费价格及获取优惠的方法 3.1. CCF CSP 3.2. CCF CCSP 4. 语言与I ...

  4. Taro -- 定义全局变量

    Taro定义全局变量 方法1:在taro中 getApp()只能取到一开始定义的值,并不能取到改变后的值 // app.js文件中 class App extends Component { cons ...

  5. python基础--局部变量与全局变量

    #全局变量作用于全局或整个程序中,程序执行完毕后销毁,局部变量作用在当前函数中,调用函数执行完毕及销毁 #如果函数的内容无global关键字,优先读取同名局部变量,如果没有同名局部变量,只能读取同名全 ...

  6. 前端VUE环境构建

    https://nodejs.org/en/ 安装node 安装淘宝npm镜像 D:\>cd cnpm D:\cnpm>npm install -g cnpm --registry=htt ...

  7. centos 6.5 安装 subversion

    安装subversion需要依赖apr.apr-util.sqlite,下载安装包,放在/usr/file目录 subversion-1.9.4.tar.gz apr-1.5.2.tar.gz apr ...

  8. [luogu] P3809 【模板】后缀排序 (SA)

    板子,照着题解打的倍增版. #include <iostream> #include <cstdio> #include <cstring> using names ...

  9. 【HDOJ6659】Acesrc and Good Numbers(dfs)

    题意:定义f(n,d)为数码d在1到n中出现的次数,其中d=0..9 如果f(d,k)=k,则称k是d好数 给定x和d,求不大于x的最大的d好数 x<=1e18 思路:考虑f的增长率主要和位数有 ...

  10. 前端通过axios和FormData实现文件上传功能遇到的坑

    使用element-ui中的upload上传组件,前端上传数据参数已经传过去了,但是后端 (java) 接不到数据 (null) [解决方案] html部分: <el-button type=& ...