Problem N HDU 2612 Find a way (两次BFS求最值)
2015-05-21 23:48
316 查看
N - Find a way
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 2612
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
题目大意:
这道题说的是,求出从Y开始到'@'和从M开始到'@'的最短的距离的和是多少。
解题思路:
一开始记录Y的位置,然后记录M的位置,求出Y到每个‘@’的距离,求出M到每个'@'的最短的距离
然后依次枚举每个@,求出两者和的最小值。
代码:
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 2612
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
题目大意:
这道题说的是,求出从Y开始到'@'和从M开始到'@'的最短的距离的和是多少。
解题思路:
一开始记录Y的位置,然后记录M的位置,求出Y到每个‘@’的距离,求出M到每个'@'的最短的距离
然后依次枚举每个@,求出两者和的最小值。
代码:
# include<cstdio> # include<iostream> # include<queue> # include<cstring> using namespace std; # define inf 99999999 # define MAX 233 struct node { int x,y; }; queue<node>Q; int x1,y11,x2,y2; char a[MAX][MAX]; int book[MAX][MAX][2]; int nxt[4][2] = {{1,0},{0,-1},{-1,0},{0,1} }; int n,m; int can_move ( int x,int y,int tag ) { if ( x>=0&&x<n&&y>=0&&y<m&&book[x][y][tag]==0&&a[x][y]!='Y'&&a[x][y]!='#'&&a[x][y]!='M' ) return 1; else return 0; } void init() { while ( !Q.empty() ) { Q.pop(); } } void bfs ( node start,int tag ) { init(); Q.push(start); while ( !Q.empty() ) { node now = Q.front(); Q.pop(); for ( int i = 0;i < 4;i++ ) { int tx = now.x+nxt[i][0], ty = now.y+nxt[i][1]; if ( can_move ( tx,ty,tag ) ) { book[tx][ty][tag] = book[now.x][now.y][tag]+1; node newnode; newnode.x = tx; newnode.y = ty; Q.push(newnode); } } } return; } int main(void) { while ( scanf("%d%d",&n,&m)!=EOF ) { for ( int i = 0; i < n;i++ ) scanf("%s",a[i]); for ( int i = 0;i < n;i++ ) { for ( int j = 0;j < m;j++ ) { if ( a[i][j]=='Y' ) { x1 = i; y11 = j; } if ( a[i][j]=='M' ) { x2 = i; y2 = j; } } } node start; start.x = x1; start.y = y11; bfs(start,0); start.x = x2; start.y = y2; bfs(start,1); int res = inf; for ( int i = 0;i < n;i++ ) { for ( int j = 0;j < m;j++ ) { if ( a[i][j]=='@' ) { if ( book[i][j][0]!=0&&book[i][j][1]!=0 ) { res = min(res,book[i][j][0]+book[i][j][1]); } } } } printf("%d\n",res*11); memset(book,0,sizeof(book)); } return 0; }
相关文章推荐
- 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--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)
- hdu2612——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 a way (注意细节的BFS)
- Find a way HDU - 2612 双向BFS
- hdu 2612:Find a way(经典BFS广搜题)
- HDU 2612 Find a way(BFS)
- BFS(最短路) HDU 2612 Find a way
- HDU 2612 Find a way(BFS)