C语言BFS--Find a way(Hdu 2612)
程序员文章站
2022-05-25 09:47:53
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at...
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
大概题意:输入地图, # :表示墙 . :表示路 YM :表示两个人 @ :表示KFC. 两个人都要去同一家KFC,但是地图上有很多KFC,输出最短的路程和.(注:每步的路程是11)
思路:以两个人为基准点先后BFS,每次找到KFC的位置后,在新开的一个数组(ans)中对应的地方记录路程,最后遍历ans中最小的便是最短路程.
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
大概题意:输入地图, # :表示墙 . :表示路 YM :表示两个人 @ :表示KFC. 两个人都要去同一家KFC,但是地图上有很多KFC,输出最短的路程和.(注:每步的路程是11)
思路:以两个人为基准点先后BFS,每次找到KFC的位置后,在新开的一个数组(ans)中对应的地方记录路程,最后遍历ans中最小的便是最短路程.
#include #include char map[211][211]; //地图 int book[211][211]; //标记数组 int ans[211][211]; //记录距离的数组 int a[4][2]={1,0,-1,0,0,1,0,-1},m,n; struct Team { int x,y,s; }que[100000]; void bfs(int startx,int starty) { int head,tail; int tx,ty,i; head=tail=0; que[tail].x=startx; que[tail].y=starty; que[tail++].s=0; book[startx][starty]=1; while(head=n||ty<0||ty>=m||book[tx][ty]||map[tx][ty]=='#') continue; if(map[tx][ty]=='@') ans[tx][ty]+=que[head].s+1; //在ans记录路程. book[tx][ty]=1; que[tail].x=tx; que[tail].y=ty; que[tail++].s=que[head].s+1; } head++; } } int main() { int i,j,min=99; while(scanf("%d%d",&n,&m)!=EOF) { memset(ans,0,sizeof(ans)); memset(book,0,sizeof(book)); for(i=0;i