(简单) HDU 2612 Find a way,BFS。
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.
就是求两个人到某一个KFC的最小值,这个题记得以前做的时候被坑惨了,要注意初始化为INF,以及说人的起始位置要表示为可通过。
代码如下:
#include<iostream>
#include<cstring> using namespace std; const int INF=1e7; short map1[][];
int que[],las,fir;
int ans[][];
int ans1[][];
int N,M;
int couKFC,Si,Sj,Ei,Ej; bool judge(int x,int y,int (*rem)[])
{
if(x<=||y<=||x>N||y>M)
return ; if(rem[x][y]!=INF)
return ; if(map1[x][y]==)
return ; return ;
} void bfs(int x,int y,int (*rem)[])
{
las=fir=;
int cou=;
int temp,t1,t2; que[las++]=x*+y;
rem[x][y]=; while(las-fir)
{
temp=que[fir++];
t1=temp/;
t2=temp%;
temp=rem[t1][t2]; if(map1[t1][t2]==)
++cou; if(cou>=couKFC)
return; --t1;
if(judge(t1,t2,rem))
{
rem[t1][t2]=temp+;
que[las++]=t1*+t2;
}
t1+=;
if(judge(t1,t2,rem))
{
rem[t1][t2]=temp+;
que[las++]=t1*+t2;
}
--t1;
--t2;
if(judge(t1,t2,rem))
{
rem[t1][t2]=temp+;
que[las++]=t1*+t2;
}
t2+=;
if(judge(t1,t2,rem))
{
rem[t1][t2]=temp+;
que[las++]=t1*+t2;
}
}
} int slove()
{
bfs(Si,Sj,ans);
bfs(Ei,Ej,ans1); int minn=INF; for(int i=;i<=N;++i)
for(int j=;j<=M;++j)
if(map1[i][j]==)
if(minn>ans[i][j]+ans1[i][j])
minn=ans[i][j]+ans1[i][j]; return minn*;
} int main()
{
ios::sync_with_stdio(false); char c; while(cin>>N>>M)
{
couKFC=; for(int i=;i<=N;++i)
for(int j=;j<=M;++j)
{
cin>>c;
ans[i][j]=ans1[i][j]=INF; switch(c)
{
case 'Y':
map1[i][j]=;
Si=i;
Sj=j;
break;
case 'M':
map1[i][j]=;
Ei=i;
Ej=j;
break;
case '.':
map1[i][j]=;
break;
case '#':
map1[i][j]=;
break;
case '@':
map1[i][j]=;
++couKFC;
break;
}
} cout<<slove()<<endl;
} return ;
}
(简单) HDU 2612 Find a way,BFS。的更多相关文章
- 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 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.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 ...
- HDU 2612 Find a way(找条路)
HDU 2612 Find a way(找条路) 00 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
随机推荐
- HDU 5652 India and China Origins 二分优化+BFS剪枝
题目大意:给你一个地图0代表可以通过1代表不可以通过.只要能从第一行走到最后一行,那么中国与印度是可以联通的.现在给你q个点,每年风沙会按顺序侵蚀这个点,使改点不可通过.问几年后中国与印度不连通.若一 ...
- bash和sh区别
在一般的linux系统当中(如redhat),使用sh调用执行脚本相当于打开了bash的POSIX标准模式(等效于bash的 --posix 参数),一般的,sh是bash的“子集”,不是子集的部分. ...
- linux的学习系列 3---目录
目录也是一个文件,它的唯一功能是用来保存文件及其相关信息.所有的文件,包括普通文件.设备文件和目录文件,都会被保存到目录中. 主目录 登录后,你所在的位置就是你的主目录(或登录目录),接下来你主要是在 ...
- aspx基础开始
<%@ Page Language="C#" Debug="true" trace="false" validateRequest=& ...
- Django: 配置和静态文件
运行django-admin.py startproject [project-name] 命令会生成一系列文件,在django 1.6版本以后的settings.py文件中有以下语句: # Buil ...
- 关于mysql 删除数据后物理空间未释放(转载)
转自 关于mysql 删除数据后物理空间未释放(转载) - NETDATA - 博客园http://www.cnblogs.com/shawnloong/archive/2013/02/07/2908 ...
- ubuntu下如何安装及使用 pysvn-workbench
网上对于 pysvn-workbench 的教程几乎没有,没办法,只好去官网自学了,现在能正常上传资料了,写点东西,以免今后忘了 安装方面:在新立得中查找svn-workbench,subversio ...
- merge_partition
set echo on time on timing on spool 01_merge_partition.log @/testdb/change/env/env_test.sql connect ...
- Counting Islands II
Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...
- 使用jquery模拟键盘事件,但window系统并不会真的响应事件,只是浏览器当前页面会响应而已
<!DOCTYPE html> <html> <head> <title>Demo</title> <meta http-equiv= ...