HDU 2612 Find a way【多起点多终点BFS/两次BFS】
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22390 Accepted Submission(s): 7304
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
【分析】:dis开了个三维数组,第三维表示不同的两次搜索,每次给第三维传递一个参数来表示哪一次。另外对于dis初始化的时候应初始化无穷大,因为有可能某个KFC根本就无法到达,影响最后求结果。因为@(终点)有多个,需要用数组来保存到每个点的最短路,用0、1区分两个不同起点。之前数组开小,显示的却是TLE,搞不懂HDU= =
【代码】:
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 500; //数组之前开小了...但是一直显示TLE而不是RE???我爆哭
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,1,-1};
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n,m,f;
char a[maxn][maxn];
int d[maxn][maxn][2];
int v[maxn][maxn];
struct node
{
int x,y,step;
}st,ed;
bool ok(int x,int y)
{
return x>=0 && y>=0 && x<n && y<m && !v[x][y] && a[x][y]!='#';
}
void bfs(int x,int y,int f)
{
queue<node> q;
ms(v,0); //每搜索一次之前都要清空vis
v[x][y]=1;
st.x=x,st.y=y,st.step=0;
q.push(st);
while(!q.empty())
{
st = q.front();
q.pop();
if(a[st.x][st.y]=='@')
d[st.x][st.y][f]=st.step;
for(int i=0;i<4;i++)
{
ed.x = st.x + dir[i][0];
ed.y = st.y + dir[i][1];
if(!ok(ed.x,ed.y)) continue;
ed.step = st.step + 1;
v[ed.x][ed.y] = 1;
q.push(ed);
}
}
}
int main()
{
//需要用数组保存到每个点的最短路,因为这里需要多次查询路径
while(~scanf("%d%d",&n,&m))
{
int ans=INF;
int x1,x2,y1,y2;
getchar();
rep(i,0,n)
{
scanf("%s",&a[i]);
}
rep(i,0,n)
{
rep(j,0,m)
{
if(a[i][j]=='Y')
{
x1=i,y1=j;
}
else if(a[i][j]=='M')
{
x2=i,y2=j;
}
}
}
ms(d,INF);
bfs(x1,y1,0);
bfs(x2,y2,1);
rep(i,0,n)
{
rep(j,0,m)
{
if(a[i][j]=='@') //对于多个KFC地点,求出两个人同时到达某一个KFC的最小步数的和,取最小值就可以了
{
ans = min(ans,d[i][j][0] + d[i][j][1]);
}
}
}
printf("%d\n",ans*11);
}
}
/*
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
*/
HDU 2612 Find a way【多起点多终点BFS/两次BFS】的更多相关文章
- HDU——2612Find a way(多起点多终点BFS)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU - 2612 Find a way 双起点bfs(路径可重叠:两个队列分别跑)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 1240 3维迷宫 求起点到终点的步数 (BFS)
题意,给出一个N,这是这个三空间的大小,然后给出所有面的状况O为空地,X为墙,再给出起始点的三维坐标和终点的坐标,输出到达的步数 比较坑 z是x,x是y,y是z,Sample InputSTART 1 ...
- hdu 2612 Find a way
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Description Pass a year learning in H ...
- HDU 2612 Find a way(找条路)
HDU 2612 Find a way(找条路) 00 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU 2612 Find a way bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- BFS(最短路) HDU 2612 Find a way
题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...
- HDU 2612 Find a way(双向bfs)
题目代号:HDU 2612 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 M ...
随机推荐
- 【bzoj1922】[Sdoi2010]大陆争霸 堆优化Dijkstra
题目描述 一张n个点m条边的图,通过每条边需要一定的时间.有一些限制条件,每个限制条件形如“x保护y”,表示到达y的最短时间不能小于到达x的最短时间(即如果在其之前到达,则需要等待至xd到达).问1到 ...
- arc068 E: Snuke Line
首先要知道 (m/1 + m/2 + ... + m/m) 约为 mlogm 还有一个比较明显的结论,如果一个纪念品区间长度大于d,那么如果列车的停车间隔小于等于d,则这个纪念品一定能被买到 然后把区 ...
- wmic的用法
原始文章链接:http://blog.sina.com.cn/s/blog_5fb265c70100w4d0.html 一.wmic的基本命令格式简析 经常看网上的相关资料的话,读者可能会对wmic有 ...
- 洛谷 P1415 拆分数列 解题报告
拆分数列 题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数. 如果有多组解,则输出使得最后一个 ...
- 自定义toolbar教程
1.写toolbar的布局文件 ,toolbar.xml <?xml version="1.0" encoding="utf-8"?> <Re ...
- JAVA中string.replace()和string.replaceAll()的区别及用法
乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样. public String r ...
- idea设置文件的编码格式
在打开某些类时会发现注释是乱码的,该如何解决idea的文件乱码呢?这就需要设置这个文件的合适编码格式: idea设置文件编码的两种方式分别如下: 第一种方式点击idea的右下角的图标如下图所示: 第二 ...
- Java多线程1:Java中sleep,wait,yield,join的区别
1.sleep()方法 在指定时间内让当前正在执行的线程暂停执行,但不会释放“锁标志”.不推荐使用. sleep()使当前线程进入阻塞状态,在指定时间内不会执行. 2.wait()方法 在其他线程调用 ...
- 单选按钮radio与文字如何对齐?
布局如下: <input type="radio" value="立即发送" name="a_1">立即发送 <input ...
- (转)C/S 与 B/S 区别
感谢:http://www.cnblogs.com/xiaoshuai/archive/2010/05/25/1743741.html C/S结构,即Client/Server(客户机/服务器)结构, ...