HDU 2612 Find a way BFS
2014-08-18 20:21
465 查看
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3743 Accepted Submission(s): 1217
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.
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
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
Sample Output
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3743 Accepted Submission(s): 1217
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.
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
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 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
Sample Output
66 88 66
/* hdu 2612 BFS 两个人找到最近的KFC 即两次BFS 求得最后的结果 visit ans 数组记得要清空 要细心啊!!!! */ #include<iostream> #include<stdio.h> #include<queue> using namespace std; struct node{ int x,y,step; }; int n,m; int visit1[201][201],visit2[201][201],ans1[201][201],ans2[201][201]; int dir[4][2]={1,0,-1,0,0,-1,0,1}; char map[201][201]; int check(int x,int y) { if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#') return 1; return 0; } void BFS(int ans[201][201],int visit[201][201],int x,int y) { int i; queue<node> que; node start,end; start.x=x; start.y=y; start.step=0; ans[x][y]=0; visit[x][y]=1; que.push(start); while(!que.empty()) { start=que.front(); que.pop(); for(i=0;i<4;i++) { end.x=start.x+dir[i][0]; end.y=start.y+dir[i][1]; if(check(end.x,end.y)&&!visit[end.x][end.y]) { end.step=start.step+1; ans[end.x][end.y]=end.step; visit[end.x][end.y]=1; que.push(end); } } } } int main() { int i,j,min,x1,x2,y1,y2; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++) { scanf("%s",map[i]); for(j=0;j<m;j++) { if(map[i][j]=='Y') { x1=i;y1=j; } if(map[i][j]=='M') { x2=i;y2=j; } } } memset(visit1,0,sizeof(visit1)); memset(ans1,0,sizeof(ans1)); BFS(ans1,visit1,x1,y1); memset(visit2,0,sizeof(visit2)); memset(ans2,0,sizeof(ans2)); BFS(ans2,visit2,x2,y2); min=9999999; for(i=0;i<n;i++) for(j=0;j<m;j++) if(map[i][j]=='@'&&visit1[i][j]&&visit2[i][j]) min=min<(ans1[i][j]+ans2[i][j])?min:ans1[i][j]+ans2[i][j]; printf("%d\n",min*11); } return 0; }
相关文章推荐
- HDU 2612 Find a way bfs 难度:1
- HDU 2612--Find a way【BFS】
- BFS-HDU-2612-Find a Way
- HDU 2612 Find a way(BFS)
- hdu 2612 Find a way(bfs)
- HDU 2612 Find a way (BFS)
- HDU 2612 Find a way【bfs】
- HDU 2612 Find a way(两次bfs)
- hdu 2612(bfs)Find a way
- HDU 2612 -Find a way (注意细节的BFS)
- hdu 2612 Find a way(BFS)
- HDU 2612 Find a way(两次BFS)
- HDU 2612 Find a way(双BFS)
- hdu 2612:Find a way(经典BFS广搜题)
- N - Find a way HDU 2612 基础BFS
- HDU 2612 find the way 双向BFS
- HDU 2612 Find a way 多起点 多终点 BFS
- hdu 2612 Find a way(BFS)
- HDU 2612 Find a way(BFS)
- HDU - 2612 Find a way(15.10.10 搜索专题)bfs