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): 4854    Accepted Submission(s): 1650

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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N=220;
const int INF=0xfffffff;
int n,m;
int ytime[N][N],mtime[N][N];
int map[N][N],num[N][N],vis[N][N];
int temp;
void BFS(int x,int y )
{
	int xx,yy;
	queue<int>q;
	xx=x;
	yy=y;
	vis[xx][yy]=1;
	num[xx][yy]=1;//记录二者一共走的步数
	q.push(xx);
	q.push(yy);
	while(!q.empty())
	{
		xx=q.front();
		q.pop();
		yy=q.front();
		q.pop();
		if(map[xx][yy]==1)
		{
			if(temp==1)
			{
				ytime[xx][yy]=num[xx][yy]-1;//这里很重要
			}
			else
			{
				mtime[xx][yy]=num[xx][yy]-1;
			}
		}
		int a ,b;
		a=xx-1;b=yy;  //分四种情况分别讨论
		if(a>=1&&!vis[a][b]&&map[a][b]!=-1)
		{
			num[a][b]=num[xx][yy]+1;
			vis[a][b]=1;
			q.push(a);
			q.push(b);

		}
		a=xx;b=yy-1;
		if(b>=1&&!vis[a][b]&&map[a][b]!=-1)
		{
			num[a][b]=num[xx][yy]+1;
			vis[a][b]=1;
			q.push(a);
			q.push(b);

		}
		a=xx+1;b=yy;
		if(a<=n&&!vis[a][b]&&map[a][b]!=-1)
		{
			num[a][b]=num[xx][yy]+1;
			vis[a][b]=1;
			q.push(a);
			q.push(b);

		}
		a=xx;b=yy+1;
		if(b<=m&&!vis[a][b]&&map[a][b]!=-1)
		{
			num[a][b]=num[xx][yy]+1;
			vis[a][b]=1;
			q.push(a);
			q.push(b);

		}
	}
}
int main()
{
	int i,j,yx,yy,mx,my;
	char ch;
	while(~scanf("%d %d",&n,&m))
	{
		memset(map,0,sizeof(map));
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				cin>>ch;
				if(ch=='Y')
				{
					yx=i,
					yy=j;
				}
				else if(ch=='M')
				{
					mx=i;
					my=j;
				}
				else if(ch=='#')
				{
					map[i][j]=-1;
				}
				else if(ch=='@')
				{
					map[i][j]=1;
				}
			}
		}
		memset(ytime,0,sizeof(ytime));
 		memset(mtime,0,sizeof(mtime));
		memset(vis,0,sizeof(vis));
		memset(num,0,sizeof(num));
		temp=1; //标记Y和M的区别
		BFS(yx,yy);
		memset(vis,0,sizeof(vis));
		memset(num,0,sizeof(num));
		temp=0;
		BFS(mx,my);
		int MIN=INF;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				if(ytime[i][j]&&mtime[i][j]&&(ytime[i][j]+mtime[i][j])<MIN)
				{
					MIN=ytime[i][j]+mtime[i][j];
				}
			}
		}
		printf("%d\n",MIN*11);
	}
	return 0;
}
时间: 2024-11-04 17:42:41

hdoj-2612-find a way-双向BFS的相关文章

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): 5401    Accepted Submission(s): 1823 Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally.

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

Hdu1401-Solitaire(双向bfs)

Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.There are four identical pieces on the board. In one move it is allowed to

UVA-1604 Cubic Eight-Puzzle (双向BFS+状态压缩+限制搜索层数)

题目大意:立体的八数码问题,一次操作是滚动一次方块,问从初始状态到目标状态的最少滚动次数. 题目分析:这道题已知初始状态和目标状态,且又状态数目庞大,适宜用双向BFS.每个小方块有6种状态,整个大方格有9*6^8个状态.每个小方块用一位6进制数表示即可. 注意:状态转移时要谨慎,否则会出现意想不到的错误: 这道题的末状态有256(2^8)个,如果对搜索层数不加限制,即使双向BFS也会TLE的,当限制正向搜索15层逆向搜索15层至正向搜索27层反向搜索3层时都能AC(我下面贴出的程序是这样的),其

HDU1195 双向BFS(或BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 , 双向BFS或者直接BFS也可以过. 其实这道题只是单向BFS就可以过的,但是为了练算法,所以还是用了双向BFS来写. 算法: 先预处理一下,从1111到9999的所有点进行构图(由于是1~9的,所以除去含有0元素的数字),能进行一次变换变成的数字则表示两点之间连通.然后从初态与目态两个点进行BFS,如果有轨迹重合的就返回路程和. 这里注意双向BFS要一层一层的进行搜索,不然的话会产生错误,

POJ1915Knight Moves(单向BFS + 双向BFS)

题目链接 单向bfs就是水题 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 const int INF = 0x3f3f3f3f; 8 const int Max = 300 + 5; 9 struct Node 10 { 11 int

BFS、双向BFS和A*

BFS.双向BFS和A* Table of Contents 1. BFS 2. 双向BFS 3. A*算法 光说不练是无用的.我们从广为人知的POJ 2243这道题谈起:题目大意:给定一个起点和一个终点.按骑士的走法(走日字),从起点到终点的最少移动多少次 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2RraXJjaGhvZmY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/g

HDU 1043 Eight(双向BFS+康托展开)

http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用双向BFS来做. ①双向BFS 在单向BFS的基础上,多建一个从终止状态开始搜索的队列,当然这个时候需要两个vis[]辅助数组,分别记录两个队列的访问情况,当两个队列相遇时即可终止循环. ②康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[

POJ 1915-Knight Moves (单向BFS &amp;&amp; 双向BFS 比较)

题目链接:Knight Moves 研究了一下双向BFS,不是很难,和普通的BFS一样,双向BFS不过是从 起点和终点同时开始搜索,可减少搜索时间 当两条搜索路线相遇时,结束. 貌似有一年百度的招聘 笔试,就是双向BFS.... 下面,比较一下BFS 和 双向BFS的用时: BFS STL的queue可能会浪费一点时间 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstrin

第六章部分例题 双向bfs邻接表和邻接矩阵实现

Idealpath 双向bfs输出颜色,邻接矩阵实现 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <map> 6 #include <algorithm> 7 8 using namespace std; 9 10 const int maxn=10000; 11 const int inf=1