hdu2612——Find a way
来源:互联网 发布:淘宝哪种付款方式最快 编辑:程序博客网 时间:2024/04/29 20:55
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
简单的BFS 对两个点bfs就行
简单的BFS 对两个点bfs就行
#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };char mat[205][205];bool vis1[205][205];bool vis2[205][205];int ans1[205][205];int ans2[205][205];struct node{int x,y;int t;}temp1, temp2;queue< node >qu;int n, m;bool is_legal(int x, int y){ if( x < 0 || x >= n || y < 0 || y >= m || mat[x][y] == '#') return false; return true;}int bfs1(int sx, int sy){memset( vis1, 0, sizeof(vis1) );while( !qu.empty() )qu.pop();temp1.x = sx;temp1.y = sy;temp1.t = 0;vis1[temp1.x][temp1.y] = 1;ans1[temp1.x][temp1.y] = 0;qu.push(temp1);while( !qu.empty() ){temp1 = qu.front();qu.pop();for(int i = 0; i < 4; i ++){temp2.x = temp1.x + dir[i][0];temp2.y = temp1.y + dir[i][1];if( !is_legal(temp2.x, temp2.y) )continue;if(vis1[temp2.x][temp2.y] == 1)continue;vis1[temp2.x][temp2.y] = 1;temp2.t = temp1.t + 11;ans1[temp2.x][temp2.y] = temp2.t;qu.push(temp2);}}}int bfs2(int sx, int sy){memset( vis2, 0, sizeof(vis1) );while( !qu.empty() )qu.pop();temp1.x = sx;temp1.y = sy;temp1.t = 0;vis2[temp1.x][temp1.y] = 1;ans2[temp1.x][temp1.y] = 0;qu.push(temp1);while( !qu.empty() ){temp1 = qu.front();qu.pop();for(int i = 0; i < 4; i ++){temp2.x = temp1.x + dir[i][0];temp2.y = temp1.y + dir[i][1];if( !is_legal(temp2.x, temp2.y) )continue;if(vis2[temp2.x][temp2.y] == 1)continue;vis2[temp2.x][temp2.y] = 1;temp2.t = temp1.t + 11;ans2[temp2.x][temp2.y] = temp2.t;qu.push(temp2);}}}int main(){while( ~scanf("%d%d", &n, &m) ){int x1, y1, x2, y2, min_ans = 0x3f3f3f3f;for(int i = 0; i < n; i ++){scanf("%s", mat[i]);for(int j = 0; j < m; j ++){if(mat[i][j] == 'Y'){x1 = i;y1 = j;}else if(mat[i][j] =='M'){x2 = i;y2 = j;}}}bfs1(x1,y1);bfs2(x2,y2);for(int i = 0; i < n; i ++)for(int j = 0; j < m; j ++){if(mat[i][j] == '@' && vis1[i][j] && vis2[i][j]){min_ans = min(min_ans, ans1[i][j] + ans2[i][j]);}}printf("%d\n", min_ans);}return 0;}
0 0
- hdu2612——Find a way
- HDU2612 Find a way —— BFS
- Find a way hdu2612
- HDU2612---Find a way
- hdu2612---Find a way
- HDU2612 Find a way
- Hdu2612 Find a way
- HDU2612 Find a way
- HDU2612-Find a way
- HDU2612 find a way
- HDU2612-Find a way
- ACM-bfs之Find a way——hdu2612
- hdu2612——Find a way(BFS)
- HDU2612 (Find a way)
- [bfs]HDU2612 Find a way
- HDU2612 Find a way BFS
- HDU2612(Find a way)搜索-两次BFS
- hdu2612 Find a way( bfs )
- JS 的那些MVC 框架 の backbone
- thttpd源码解析 定时器模块
- 图解JVM在内存中申请对象及垃圾回收流程
- 延时
- 物体识别系统的流程
- hdu2612——Find a way
- ascll和unicode对应的char,wchar_t存储字符
- 深入理解最强桌面地图控件GMAP.NET --- SOSO地图
- CoreJava 8th V-1 P150 Java多态
- 《算法之道》精华 难解问题部分
- UVA 10891 Game of Sum
- 《算法之道》精华 经典算法部分
- linux下的wait()实现等待一个子进程和所有子进程退出
- Excel 的vlookup公式使用