HDU2612 Find a way(双路BFS)
来源:互联网 发布:ubuntu 安装mysql5.7 编辑:程序博客网 时间:2024/04/24 07:57
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11673 Accepted Submission(s): 3792
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
简单的两路广搜,与普通广搜不同的只是调用函数两次而已,代码如下:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;char map[210][210];int visit[210][210];int step1[2][210][210];int next1[4][2]={0,1,0,-1,1,0,-1,0};int n,m;struct node{ int x,y,step;};int check(int x,int y)//定义外部函数判断边界以及是否可走{ if(map[x][y]!='#'&&x>=0&&x<n&&y>=0&&y<m) return 1; return 0;}void bfs(int a,int x,int y){ memset(visit,0,sizeof(visit)); queue<node>q; visit[x][y]=1;//起点标记为走过 node temp,point; temp.x=x; temp.y=y; temp.step=0; q.push(temp); while(!q.empty()) { temp=q.front(); q.pop(); if(map[temp.x][temp.y]=='@') step1[a][temp.x][temp.y]=temp.step; for(int i=0;i<4;i++) { point.x=temp.x+next1[i][0]; point.y=temp.y+next1[i][1]; if(!visit[point.x][point.y]&&check(point.x,point.y)) { visit[point.x][point.y]=1; point.step=temp.step+1; q.push(point); } } }}int main(){ while(~scanf("%d%d",&n,&m)) { int ax,ay,bx,by; for(int i=0; i<n; i++) { scanf("%s",map[i]); for(int j=0; j<m; j++) { if(map[i][j]=='Y') { ax=i; ay=j; } else if(map[i][j]=='M') { bx=i; by=j; } } } memset(step1,0x3f3f3f3f,sizeof(step1));//将数组中的所有元素都归为正无穷,方便求最小值 bfs(0,ax,ay);//Y为起点,广搜到每个@的距离 bfs(1,bx,by);//M为起点,广搜到每个@的距离 int step_num=0x3f3f3f3f;//定义为正无穷,为求两人最小步数和做准备 for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(map[i][j]=='@') { step_num=min(step_num,step1[0][i][j]+step1[1][i][j]); } } printf("%d\n",step_num*11); }}
0 0
- HDU2612 Find a way(双路BFS)
- hdu2612 Find a way( bfs )
- hdu2612 Find a way(BFS)
- [bfs]HDU2612 Find a way
- HDU2612 Find a way BFS
- BFS:HDU2612-Find a way(双向BFS)
- hdu2612 Find a way(2次bfs)
- hdu2612——Find a way(BFS)
- HDU2612(Find a way)搜索-两次BFS
- hdu2612 Find a way--BFS & 打表
- HDU2612 Find a way(双路广搜BFS)
- hdu2612-Find a way(两次bfs)
- HDU2612 Find a way —— BFS
- HDU2612 (Find a way)
- Find a way hdu2612
- HDU2612---Find a way
- hdu2612---Find a way
- HDU2612 Find a way
- extjs6.2 学习笔记2-本地化
- 精通 CSS+DIV 网页样式与布局 156
- linux通过局域网访问windows共享文件
- spring boot + activiti-rest 做接口服务屏蔽authentication
- JAVA concurrent包学习--CyclicBarrier
- HDU2612 Find a way(双路BFS)
- 再看泛型
- 精通 CSS+DIV 网页样式与布局 157
- 音视频框架-webrtc中的网络反馈与控制
- c++爬虫大众点评数据(未完成)
- php-控制成分
- 387. First Unique Character in a String 难度:easy
- Socket网络编程系列教程序
- php表单GET和POST的区别