HDOJ 题目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): 4772    Accepted Submission(s): 1624

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

奋斗的年代

Recommend

yifenfei   |   We have carefully selected several similar problems for you:  1254 1175 2579 1983 1195

ac代码

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#define min(a,b) (a>b?b:a)
#define INF 0xfffffff
using namespace std;
int vis1[1010][1010],ans1[1010][1010],ans2[1010][1010],n,m,vis2[1010][1010];
char map[1010][1010];
struct s
{
	int x,y,step;
}a,temp;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int jud(struct s a,int vis[1010][1010])
{
	if(a.x<0||a.x>=n)
		return 0;
	if(a.y<0||a.y>=m)
		return 0;
	if(vis[a.x][a.y])
		return 0;
	if(map[a.x][a.y]=='#')
		return 0;
	return 1;
}
void bfs(int x,int y,int ans[1010][1010],int vis[1010][1010])
{
	a.x=x;
	a.y=y;
	a.step=0;
	vis[x][y]=1;
	ans[x][y]=a.step;
	queue<struct s>q;
	q.push(a);
	while(!q.empty())
	{
		a=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			temp.x=a.x+dx[i];
			temp.y=a.y+dy[i];
			if(!jud(temp,vis))
				continue;
			temp.step=a.step+1;
			ans[temp.x][temp.y]=temp.step;
			vis[temp.x][temp.y]=1;
			q.push(temp);
		}
	}
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i,j,x1,x2,y1,y2;
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='M')
				{
					x1=i;
					y1=j;
				}
				if(map[i][j]=='Y')
				{
					x2=i;
					y2=j;
				}
			}
		}
		memset(vis1,0,sizeof(vis1));
		memset(ans1,0,sizeof(ans1));
		bfs(x1,y1,ans1,vis1);
		memset(vis2,0,sizeof(vis2));
		memset(ans2,0,sizeof(ans2));
		bfs(x2,y2,ans2,vis2);
		int ans=INF;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='@'&&vis1[i][j]&&vis2[i][j])
				{
					//printf("%d %d\n",ans1[i][j],ans2[i][j]);
					ans=min(ans,ans1[i][j]+ans2[i][j]);
				}
			}
		}
		printf("%d\n",ans*11);
	}
}
时间: 2024-10-12 14:23:44

HDOJ 题目2612 Find a way(BFS)的相关文章

HDOJ 题目1372 Knight Moves(BFS)

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7551    Accepted Submission(s): 4513 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

HDOJ 题目分类

HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:    用STL中的next_permutation()1029:1032:1037:1039:1040:1056:1064:1065:1076:    闰年 1084:1085:1089,1090,1091,1092,1093,1094, 1095, 1096:全是A+B1108:1157:1196:1

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

HDOJ 题目4738 Caocao&#39;s Bridges(双联通,求桥)

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1752    Accepted Submission(s): 642 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. B

HDOJ题目3729 I&#39;m Telling the Truth(二分图)

I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1629    Accepted Submission(s): 805 Problem Description After this year's college-entrance exam, the teacher did a survey i

HDOJ 题目4349 Xiao Ming&#39;s Hope(找规律)

Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 1015 Problem Description Xiao Ming likes counting numbers very much, especially he is fond of co

HDOJ题目3309 Roll The Cube(BFS)

Roll The Cube Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 502    Accepted Submission(s): 181 Problem Description This is a simple game.The goal of the game is to roll two balls to two holes

HDOJ 题目1429 胜利大逃亡(续)(BFS)

New! 关于举办校第十五届程序设计竞赛暨2015省赛集训队选拔赛的通知 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5811    Accepted Submission(s): 2027 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次