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。的更多相关文章

  1. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  2. HDU 2612 Find a way bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...

  3. HDU 2612 Find a way BFS,防止超时是关键

    之前我写的时候是:每找到一个‘@’就广搜一次,如果这样写有多少个‘@’就会广搜几次,这样就超时了.我队友告诉我应该打个表,这个方法确实不错.因为'Y'和'M'是唯一的,我通过这两个点分别广搜一次,对所 ...

  4. HDU 2612 (2次BFS,有点小细节)

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  5. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  6. BFS(最短路) HDU 2612 Find a way

    题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...

  7. 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 ...

  8. HDU 2612 Find a way(找条路)

    HDU 2612 Find a way(找条路) 00 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)   Problem  ...

  9. 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,最先 ...

随机推荐

  1. 响应式web设计的优化

    在响应式网页的设计过程中,其中也会涉及到一些用户体验方面的问题和弊端,需要进一步的优化. (1)减轻Javascript库负载 对于移动端来说,jQuery表现的太过厚重,而现在针对移动端的状态来说, ...

  2. POI获取Excel列数和行数的方法

    //获取指定行,索引从0开始 hssfRow=hssfSheet.getRow(1); //获取指定列,索引从0开始 hssfCell=hssfRow.getCell((short)6);//获取总行 ...

  3. 青蛙的约会<数论,extgcd>

    青蛙的约会 题意: 在一个圆上有一个零刻度点,公青蛙和母青蛙分别在A点和B点<不同的位 置>,他们每秒行走的距离分别是m和n,圆的周长是L.问题是这两个青 蛙能不能相遇,若能在什么时候相遇 ...

  4. CDockablePane 关闭的问题

    显示或者隐藏 当点击CDockablePane上的关闭按钮时,并不能将其关闭,知识将其隐藏了起来,如果需要重新显示或隐藏,则相关命令的响应函数如下: if(m_Panes.GetSafeHwnd()) ...

  5. display:block;inline;inline-block大总结

    总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).block元素通 ...

  6. JS-DOM操作应用

    父级.appendChild(子节点) 父级.insertBefore(子节点,在谁之前) <title>无标题文档</title> <script> window ...

  7. Oracle Sql优化之报表和数据仓库运算

    1.行转列:有两种写法,一种是case when end写法,另一种写法是pivot(oracle 11g新增) select job, then sal end) as sal10, then sa ...

  8. 微信小程序Server端环境配置

    主要内容:1. SSL免费证书申请步骤2. Nginx HTTPS 配置3. TLS 1.2 升级过程 微信小程序要求使用 https 发送请求,那么Web服务器就要配置成支持 https,需要先申请 ...

  9. shortcut to open a linux terminal

    1) alt+ f2 2) input "gnome-terminal" 3) press "enter"

  10. n皇后问题<dfs>

    n皇后问题指的是: n*n的国际象棋棋盘上摆放n个皇后,使其不能互相攻击, 即任意两个皇后都不能处于同一行.同一列或同一斜线上, 问有多少种摆法. 和一般n皇后问题不同的是,现在棋盘上有可能已经放了一 ...