HDU 2612 Find a way(搜索)

31 篇文章 0 订阅

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.
#...#

输出

66

88

66

题意就是给出一个n*m的地图,Y和M为两个要去吃饭的人,找到任意一家@

满足时间最短,开始太菜了,把每个@都让两个起点搜索一遍,稳稳超时了

可以用一个三维的数组来记录,Y和M两者到达某一个@的最短时间,再寻找出

最短时间即可;改完有个小失误,memset没有加到if里,又超时一遍。。

#include<iostream>
#include<queue>
#include<string.h>
#include<stdio.h>
using namespace std;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
char map[1010][1010];
int vis[1010][1010];
int ans[1010][1010][2];   //[2]中0表示Y到某一@的时间,1表示m到某一@的时间
int n, m, ex, ey, flag;
struct node{
	int x, y, t;
};
void bfs(int x, int y, int t){
	queue<node>q;
	q.push({ x, y, t });
	while (!q.empty()){
		int x = q.front().x, y = q.front().y, t = q.front().t;
		q.pop();
		if (vis[x][y])continue;
		vis[x][y] = 1;
		if (map[x][y] == '@')ans[x][y][flag] = t;   //遇到任意一个@都记录到他的最短时间
		for (int i = 0; i < 4; i++)
		{
			int xx = x + dir[i][0];
			int yy = y + dir[i][1];
			if (xx <= n&&xx >= 1 && yy <= m&&yy >= 1 && map[xx][yy] != '#')
				q.push({ xx, yy, t + 1 });
		}
	}
}
int main(){
	int bx, by, sx, sy;
	while (cin >> n >> m){
		int MIN = 10000;
		memset(ans, 0, sizeof(ans));
		flag = 0;
		for (int i = 1; i <= n; i++)
			scanf("%s", map[i] + 1);
		for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= m; j++){
		    	if (map[i][j] == 'Y')       //开始搜索Y到任意一家@的时间
				{
					flag = 0, bfs(i, j, 0);
					memset(vis, 0, sizeof(vis));
				}
				if (map[i][j] == 'M')    //开始搜索M到任意一家@的时间
				{
					flag = 1, bfs(i, j, 0);
					memset(vis, 0, sizeof(vis));
    			}
			}
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++){
		    	if (map[i][j] == '@')
				{
					if (ans[i][j][1] != 0 && ans[i][j][0] != 0) //如果两者有任意一个为0则表明有人没法到达当前的@
						MIN = min(ans[i][j][1] + ans[i][j][0], MIN);//取最小的时间
				}
			}
		cout << MIN * 11 << endl;   //每一步要11分钟,别忘了乘以11
	}
	return 0;
}

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值