题解报告:hdu 2612 Find a way(双bfs)
Problem Description
Input
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
Sample Input
Sample Output
#include<iostream>
#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=;
int n,m,fx,fy,mx,my,mp1[maxn][maxn],mp2[maxn][maxn],dir[][]={{,-},{,},{-,},{,}};
char mp[maxn][maxn];
bool vis[maxn][maxn];
struct node{int x,y;}nod,tmp;//记录到当前坐标的最短距离
queue<node> que;
void bfs(int x,int y,int dis[][maxn]){//dis数组记录从出发点到达每个坐标位置的最短距离
while(!que.empty())que.pop();//清空
memset(vis,false,sizeof(vis));//标记为全部未访问状态
nod.x=x,nod.y=y;
vis[x][y]=true;
que.push(nod);
while(!que.empty()){
nod=que.front();que.pop();
for(int i=;i<;++i){
int nx=nod.x+dir[i][],ny=nod.y+dir[i][];
if(nx>=&&ny>=&&nx<n&&ny<m&&mp[nx][ny]!='#'&&!vis[nx][ny]){
dis[nx][ny]=dis[nod.x][nod.y]+;//到达邻接点的步数为到达上一个点的步数加1
vis[nx][ny]=true;
tmp.x=nx,tmp.y=ny;//要用临时变量来存储,不然会出错
que.push(tmp);
}
}
}
}
int main(){
while(cin>>n>>m){
for(int i=;i<n;++i){
for(int j=;j<m;++j){
cin>>mp[i][j];
if(mp[i][j]=='Y'){fx=i;fy=j;}//标记Y、M的坐标位置
if(mp[i][j]=='M'){mx=i;my=j;}
}
}
memset(mp1,,sizeof(mp1));//全部初始化为0
memset(mp2,,sizeof(mp2));
bfs(fx,fy,mp1);
bfs(mx,my,mp2);
int dist=;
for(int i=;i<n;++i)
for(int j=;j<m;++j)
if(mp[i][j]=='@'&&mp1[i][j]!=&&mp2[i][j]!=)
//如果是KFC即mp[i][j]=='@'并且mp1[i][j]和mp2[i][j]都不等于0,因为可能会达不到,所以两者都不能为0
dist=min(mp1[i][j]+mp2[i][j],dist);
cout<<dist*<<endl;
}
return ;
}
题解报告:hdu 2612 Find a way(双bfs)的更多相关文章
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- 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 - 2612 Find a way 【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...
- hdu 2612 Find a way(BFS)
题目链接:hdu2612 思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和 #include ...
- (简单) HDU 2612 Find a way,BFS。
Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...
- HDU - 2612 Find a way(BFS搜索)
题目: 链接 思路: 用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值. PS: 如果用‘Y’和‘M’点分别去搜每个 ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 题解报告:hdu 1398 Square Coins(母函数或dp)
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
- 题解报告:hdu 2069 Coin Change(暴力orDP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069 Problem Description Suppose there are 5 types of ...
- 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)
Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...
随机推荐
- Pull方式解析XML文件
package com.pingyijinren.test; import android.content.Intent; import android.os.Handler; import andr ...
- MySQL使用教程收集(语法教程/命令教程)
说明:现在市面上的教程除了基本语法外,都基本是五花八门的,最权威且最全面的解释应该上官网去查看. https://www.tutorialspoint.com/mysql/index.htm http ...
- 保持WCF服务端与客户端的长连接
背景 客户端与服务端使用WCF建立连接后:1.可能长时间不对话(调用服务操作):2.客户端的网络不稳定. 为服务端与客户端两边都写“心跳检测”代码?不愿意. 解决 设置inactivityTimeou ...
- html自动换行
对于div,p等块级元素 正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义的宽度之后自动换行html css 1.(IE浏览器)连续的英文字符和阿拉伯数 ...
- 苹果Macbook Air怎么安装Win7系统图解教程
下面开始我们在苹果Macbook Air上的Windows7之旅吧.
- 我的arcgis培训照片7
来自:http://www.cioiot.com/successview-553-1.html
- ajax请求锁屏功能
我们有时候在进行ajax请求的时候希望页面不允许点击,等请求结束之后才可以进行点击,那么可以写: $(".cloudos-container").ajaxStart($.block ...
- UGUI 实现Button长按效果(RepeatButton)
Tag:加入了一个延迟,在button按下状态一段时间后再開始 repeate using UnityEngine; using UnityEngine.Events; using UnityEngi ...
- [RK3288][Android6.0] 音频调试方法小结【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/70053135 Platform: ROCKCHIPOS: Android 6.0Kernel ...
- 【NOIP 2016】初赛-完善程序 & 参考答案
参考答案 感觉这两题目都挺好的~~ T1 交朋友 简单描述:有n个人依次进入教室,每个人进入会找一个身高绝对值相差最小的人交朋友(相同时更想和高的交朋友),求每个人交的朋友. Solution: So ...