HDU 2612
来源:互联网 发布:来肯在线进销存软件 编辑:程序博客网 时间:2024/06/05 11:57
#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<cstring>using namespace std;#define N 205#define inf 0x3f3f3f3fchar s[N][N];int vis[N][N];int sx,sy,ex,ey;int vx[]={0,0,1,-1};int vy[]={1,-1,0,0};int ans1[N][N],ans2[N][N];int n,m;struct point {int x,y,step;point (int xx,int yy,int ss){x=xx,y=yy,step=ss;}};void bfs(int judge){memset(vis,0,sizeof(vis));memset(ans1,inf,sizeof(ans1));memset(ans2,inf,sizeof(ans2));queue<point>q;q.push(point(sx,sy,0));while(!q.empty()){int ok=0;point top=q.front();q.pop();if(s[top.x][top.y]=='@'){ans1[top.x][top.y]=top.step;++ok;if(ok==judge)break;}for(int i=0;i<4;i++){int tx=top.x+vx[i];int ty=top.y+vy[i];if(tx<0||ty<0||tx>=n||ty>=m||vis[tx][ty]||s[tx][ty]=='#')continue;q.push(point(tx,ty,top.step+1));vis[tx][ty]=1;}}while(!q.empty())q.pop();memset(vis,0,sizeof(vis));q.push(point(ex,ey,0));while(!q.empty()){int ok=0;point top=q.front();q.pop();if(s[top.x][top.y]=='@'){ans2[top.x][top.y]=top.step;++ok;if(ok==judge)break;}for(int i=0;i<4;i++){int tx=top.x+vx[i];int ty=top.y+vy[i];if(tx<0||ty<0||tx>=n||ty>=m||vis[tx][ty]||s[tx][ty]=='#')continue;q.push(point(tx,ty,top.step+1));vis[tx][ty]=1;}}}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifwhile(~scanf("%d%d",&n,&m)){if(n==0&&m==0)break;int ans=inf;for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>s[i][j];if(s[i][j]=='Y'){sx=i,sy=j;}if(s[i][j]=='M'){ex=i,ey=j;}}}int sum=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(s[i][j]=='@'){sum++;}}}bfs(sum);for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(s[i][j]=='@'){ans=min(ans,ans1[i][j]+ans2[i][j]);}}}printf("%d\n",ans*11);}return 0;}
题意给你2个起点‘M’和‘Y’,求这两个点到‘@’这个字符总和距离最小,‘@’会有多个。先将‘@’的个数计数为sum。这样一看好像要调用sum次BFS,其实仔细分析可以知道每次调用BFS所有的最短路已经被出来了,不必多次调用了。那么我们可以用二维数组保存当前的最短路,如果遇到‘@’这个点,下标为x,y。就保存ans[]x[y]=step;这样只要进行2次BFS就可以求出'M'和'Y'到所有‘@’的最短路了
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7029 Accepted Submission(s): 2334
Problem 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.
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.
Input
The input contains multiple test cases.
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
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
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input
4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
Sample Output
668866
Author
yifenfei
Source
奋斗的年代
Recommend
yifenfei
0 0
- HDU 2612
- hdu 2612
- hdu 2612
- HDU 2612
- HDU 2612
- HDU 2612
- HDU 2612
- hdu 2612
- hdu 2612
- HDU 2612
- HDU 2612 两次bfs
- HDU-2612(双BFS)
- HDU 2612 两次BFS
- HDU 2612 BFS*2
- hdu 2612 Beat
- hdu 2612(dfs)
- hdu~2612(bfs)
- hdu-2612 两次bfs
- 侧滑菜单和viewpager冲突
- java集合类(容器)
- Spring注解注入
- 高斯消元模板[HDU2262]
- IOS网络笔记--地图内容2(正反向编码)
- HDU 2612
- OS X 10.11 cocoapods
- 第三十四篇:Quartz2D绘图
- LeetCode---Spiral Matrix
- git 配置自动补全
- 手游延迟问题
- iOS运行时编程(Runtime Programming)和Java的反射机制对比
- 学习笔记Cocos2d-x之内存管理
- C++邻接矩阵创建图及深度、广度遍历