HDU - 2612 Find a way(BFS搜索)
题目:
思路:
用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值。
PS:
如果用‘Y’和‘M’点分别去搜每个‘@’,这样妥妥的会超时。当时做这个题脑抽的一批……(烦!!!)
另外如果‘Y’和‘M’到不了‘@’这里的值为0,应该把这种情况排除。
代码:
//#include <bits/stdc++.h>
#include <queue>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
#define MAX 1000000000
#define mod 1000000000
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn = ;
char mp[maxn][maxn];
int dx[]={,,,-};
int dy[]={,-,,};
int n,m;
int vis[maxn][maxn],sum[maxn][maxn];
struct Node{
int x,y,cost;
}; bool isin(int x,int y){
return x>=&&x<n&&y>=&&y<m;
} void BFS(int x,int y){
memset(vis,,sizeof(vis));
vis[x][y] = ;
queue<Node>que;
que.push(Node{x,y,});
while(!que.empty()){
Node u = que.front();
que.pop();
for(int i=; i<; i++){
int tx = u.x+dx[i],ty = u.y+dy[i];
if(isin(tx,ty) && !vis[tx][ty] && mp[tx][ty]!='#'){
vis[tx][ty] = ;
sum[tx][ty] += u.cost+;
que.push(Node{tx,ty,u.cost+});
}
}
}
return;
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=; i<n; i++){
scanf("%s",mp[i]);
}
memset(sum,,sizeof(sum));
for(int i=; i<n; i++){
for(int j=; j<m; j++){
if(mp[i][j]=='Y'){
BFS(i,j);
}else if(mp[i][j]=='M'){
BFS(i,j);
}
}
}
int ans = ;
for(int i=; i<n; i++){
for(int j=; j<m; j++){
if(mp[i][j]=='@' && sum[i][j]!=){
ans = min(ans,sum[i][j]);
}
}
}
printf("%d\n",ans);
}
return ;
}
HDU - 2612 Find a way(BFS搜索)的更多相关文章
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 2612 - Find a way - [BFS]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...
- HDU 2612 Find a way bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...
- HDU 2602 Find a way BFS搜索
题意:找到总时间最少的KFC 分析:两遍BFS 找KFC比较一下 注:有些地方的KFC可能到达不了,wa了一次 #include <iostream> #include <cstdi ...
- HDU 2612 Find a way BFS,防止超时是关键
之前我写的时候是:每找到一个‘@’就广搜一次,如果这样写有多少个‘@’就会广搜几次,这样就超时了.我队友告诉我应该打个表,这个方法确实不错.因为'Y'和'M'是唯一的,我通过这两个点分别广搜一次,对所 ...
- HDU 2612 (2次BFS,有点小细节)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- 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 ...
随机推荐
- EA使用记录
1.Del键只能删除桌面上的图形,不能删除项目树中的图形:要同时从项目树中删除需要ctrl + del: 2.要找到桌面上的图形对应的项目树中的图 alt + g: 3.要设置默认的LINK样式,在菜 ...
- bzoj 4817: [Sdoi2017]树点涂色【树链剖分+LCT】
非常妙的一道题. 首先对于操作一"把点x到根节点的路径上所有的点染上一种没有用过的新颜色",长得是不是有点像LCT中的access操作?进而发现,如果把同一颜色的点连起来作为LCT ...
- Trie树的小应用——Chemist
题意(自己编的): 给你一篇文章,包含n个长度为Si的单词,然后给你m组询问,每次询问一个单词在这篇文章中作为单词前缀出现的次数.n <=10^6,m<=10^6,Si<=100. ...
- python爬虫抓取哈尔滨天气信息(静态爬虫)
python 爬虫 爬取哈尔滨天气信息 - http://www.weather.com.cn/weather/101050101.shtml 环境: windows7 python3.4(pip i ...
- layer 确认或取消后跳转
layer.open({ content: "下单成功" , btn: ['确定','取消'], style: 'width:80%', yes: function(index, ...
- Qt事件系统之四:定时器事件与随机数
一.定时器事件和随机数 QTimerEvent类用来描述一个定时器事件.对于一个QObject的子类,只需要使用int QObject::startTimer ( int interval)函数来开启 ...
- Lightoj 1090 - Trailing Zeroes (II)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q< ...
- android开发学习——android studio 引入第三方库的总结
http://www.jianshu.com/p/0c592fff5d89 总结的很溜
- css标签及属性
css标签及属性 HTML引入CSS的方法 1.嵌入式 <style type = “text/css”>要写的样式</style> 2.外联式 <link rel ...
- Angular广播/消息通知的接收与发送
一.在接收页:添加引用: private eventManager: JhiEventManager: 接收通知的方法: // 接收通知(新建.编辑.删除页发送过来的通知) // upmsMenuLi ...