(简单) HDU 2612 Find a way,BFS。
2024-08-27 21:14:34
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 ;
}
最新文章
- 三、基于hadoop的nginx访问日志分析--计算时刻pv
- java实现Haffman编码
- 黄永成-thinkphp讲解-个人博客讲解26集
- EF Core 1.0中使用Include的小技巧
- iOS开发工程师面试知识点汇总
- python datetime 时间日期处理小结
- C# 匿名方法及Lambda表达式
- 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
- Python学习6.1_函数参数及参数传递
- #1042 - Can&#39;t get hostname for your address
- Oracle EBS-SQL (INV-3):检查仓库库存价值明细.sql
- Professional C# 6 and .NET Core 1.0 - What’s New in C# 6
- 网络编程-day2
- Icehouse 创建Instance代码分析
- LVS DR模式搭建 keepalived lvs
- 阿里云服务器购买 发布web项目全过程
- poj 3026(BFS+最小生成树)
- linux jar 命令使用
- PHP获取数组长度的方法 函数参数的比较
- 【Android】Eclipse自己主动编译NDK/JNI的三种方法