hd 2612 Find a way (BFS)
来源:互联网 发布:网络销售金融产品 编辑:程序博客网 时间:2024/05/02 04:35
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10401 Accepted Submission(s): 3403
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<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#define INF 0x3f3f3fusing namespace std;int dis1[210][210]; //用于储存Y到每个KFC的最小步数int dis2[210][210]; //用于储存M到每个KFC的最小步数char map[210][210];int vis[210][210];int n ,m;int dx[4] = {1,0,-1,0};int dy[4] = {0,1,0,-1};int sx1,sx2,sy1,sy2;struct node{ int x; int y; int step;};void bfs(int a,int b,int p){ memset(vis,0,sizeof(vis)); node now,next; queue<node > q; now.x = a; now.y = b; now.step = 0; vis[now.x][now.y] = 1; q.push(now); while(!q.empty()) { next = q.front(); q.pop(); if(map[next.x][next.y] == '@') { if(p == 1) dis1[next.x][next.y] = next.step; else dis2[next.x][next.y] = next.step; } for(int i = 0 ; i < 4 ; i++) { now.x = next.x + dx[i]; now.y = next.y + dy[i]; if(now.x >= 0 && now.x < n && now.y >= 0 && now.y < m && map[now.x][now.y] != '#' && !vis[now.x][now.y]) { vis[now.x][now.y] = 1; now.step = next.step + 1; q.push(now); } } }}int main(){ while(cin >> n >> m) { int k = 0; memset(vis,0,sizeof(vis)); memset(map,'\0',sizeof(map)); for(int i = 0 ; i < n ; i++) cin >> map[i]; for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j++) { if(map[i][j] == 'Y') { sx1 = i; sy1 = j; } if(map[i][j] == 'M') { sx2 = i; sy2 = j; } } memset(dis1,INF,sizeof(dis1)); // 每次计算都必须将dis1和dis2数组初始化 bfs(sx1,sy1,1); memset(dis2,INF,sizeof(dis2)); bfs(sx2,sy2,2); int MIN = INF; for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j++) { if(dis1[i][j] != INF && dis2[i][j] != INF) //找到两个人都能到达的KFC MIN = min(dis1[i][j]+dis2[i][j],MIN); //找出两人到达KFC的和的最小值 } int temp = MIN * 11; cout << temp << endl; } return 0;}
0 0
- hd 2612 Find a way (BFS)
- 【HD 2612】Find a way(BFS)
- Find a way hd 2612
- 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)
- HDU 2612 Find a way (BFS)
- 【HDU 2612】Find a way(BFS)
- UVA 2612 Find a way(BFS)
- HDU 2612 Find a way (BFS)
- HDU 2612 Find a way(BFS)
- CSS 之 overflow
- 【ITOO虐心之旅】——纵你虐我千百遍,我仍视你为初恋
- 我给小楠讲历史—上下五千年
- 数据结构实验之二叉树二:遍历二叉树
- oracle sys/system用户的默认密码
- hd 2612 Find a way (BFS)
- 图论之并查集-find the most comfortable road
- tjut 4665
- 不敢死队问题
- 实现一个函数翻转字符串(将一个字符串的内容,逆序存储)。
- 【CDOJ 1334】 郭大侠与Rabi-Ribi
- 数据结构实验之栈五:下一较大值(一)
- NYOJ 436 sum of all integer numbers(坑人,递归)
- CSS-Border(边框)