HDU - 2612 - Find a way (BFS)

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
[email protected]
.#...
.#...
@..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;
}
时间: 2024-10-28 14:49:10

HDU - 2612 - Find a way (BFS)的相关文章

HDU 2612 Find a way(BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 题目大意:给你一张n*m的图,图上有两个点Y.M,和若干个点@,找出某个点@使Y.M到这里的距离之和最小,并求出距离. 解题思路:BFS,求出Y.M到各个@的最小距离存下来,最后枚举各个@找出最小距离和即可. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algor

HDU 1180——诡异的楼梯( BFS)

诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 8717    Accepted Submission(s): 2148 Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在竖

HDU 2717 Catch That Cow (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12615    Accepted Submission(s): 3902 Problem Description Farmer John has been

HDU 5876:Sparse Graph(BFS)

http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in G

HDU 3137 No Left Turns(BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3137 Problem Description ALL HEADS: You're a Knight of the Round Table? ROBIN: I am. LEFT HEAD: In that case I shall have to kill you. MIDDLE HEAD: Shall I? RIGHT HEAD: Oh, I don't think so. MIDDLE HEAD:

HDU 1728:逃离迷宫(BFS)

http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去.令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不

HDU 1180 诡异的楼梯(BFS)

诡异的楼梯 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的

HDU ACM 1043 Eight-&gt;广度优先搜索(BFS)+康托展开(全排列hash)实践

分析:经典的八数码问题,参考别人的代码写的,逆向广搜,把结果保存起来,之后在使用. #include<iostream> #include<queue> using namespace std; #define STATE_COUNT 363000 //因为9!=362880 int fact[]={1,1,2,6,24,120,720,5040,40320,362880}; //0到9的阶乘,用来计算hash值 char dir[4][2]={ {0,1},{0,-1},{1,0

HDU 5025 Saving Tang Monk(bfs)

Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujin