首页 > 其他 > 详细

HDU - 2612 - Find a way (BFS)

时间:2015-07-24 14:22:31      阅读:161      评论:0      收藏:0      [点我收藏+]

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6157    Accepted Submission(s): 2052


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
 

Author
yifenfei
 

Source
 



简单的BFS题,bfs来记录Y和M到每个点的最小步数,这里不过要注意可能会有的KFC不能到达


AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int n, m;

int mp[205][205];

int d[205][205];
int vis[205][205];

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};

struct node {
	int a;
	int b;
	int cnt;
	node (int x, int y, int z) {
		a = x;
		b = y;
		cnt = z;
	}
};

void bfs(int x, int y) {	//bfs遍历来存储M和Y到每个点的距离 
	memset(vis, 0, sizeof(vis));
	queue<node> que;
	que.push(node(x, y, 0));
	vis[x][y] = 1;
	while(!que.empty()) {
		node t = que.front();
		que.pop();
		for(int i = 0; i < 4; i ++) {
			int xx = t.a + dx[i];
			int yy = t.b + dy[i];
			if(xx >= 0 && xx <= n - 1 && yy >= 0 && yy <= m - 1 && mp[xx][yy] != 2 && !vis[xx][yy]) {
				que.push(node(xx, yy, t.cnt + 1));
				d[xx][yy] += t.cnt + 1;
				vis[xx][yy] = 1;
			}
		}
	}
}

int main() {
	while(scanf("%d %d", &n, &m) != EOF) {
		int yx, yy;
		int mx, my;
		char s[205];
		for(int i = 0; i < n; i ++) {
			scanf("%s", &s);
			for(int j = 0; j < m; j ++) {
				if(s[j] == '.') mp[i][j] = 1;
				else if(s[j] == '#') mp[i][j] = 2;
				else if(s[j] == '@') {
					mp[i][j] = 3;
				}
				else if(s[j] == 'Y') {
					mp[i][j] = 4;
					yx = i; yy = j;
				}
				else if(s[j] == 'M') {
					mp[i][j] = 4;
					mx = i; my = j;
				}
			}
		}
		
//		for(int i = 0; i < n; i ++, cout << endl) {
//			for(int j = 0; j < m; j ++) {
//				cout << mp[i][j] << " ";
//			}
//		}
		
		memset(d, 0, sizeof(d));
		bfs(yx, yy);
		bfs(mx, my);
		
		int ans = INF;
		for(int i = 0; i < n; i ++) {
			for(int j = 0; j < m; j ++) {
				if(mp[i][j] == 3) {
					if(d[i][j] != 0) {	//要注意还有可能有的KFC不能到达 
						ans = min(ans, d[i][j]);
					}
				}
			}
		}
		printf("%d\n", ans * 11);
	}
	return 0;
}








版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU - 2612 - Find a way (BFS)

原文:http://blog.csdn.net/u014355480/article/details/47039611

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!