ACM-bfs之Find a way——hdu2612

Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3387 Accepted Submission(s): 1104


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


这道题就是队列加BFS,我用的方法就是两个人都搜索一遍,

KFC数组初始化为-1,第一个人走到任何一个KFC,将步数加过去,

第二个人走到相应的也把步数加过去,

这样最后找最小的就可以了。

为什么用-1呢,因为如果输入下图:

4 5
@  .  # @ .
  .   .  Y #  .
@ # M #  .
@  .  .  # @

(为了观赏方便,我加了很多空格)
如果KFC初始化为0,则最后会输出0,
因为右面两个没有遍历到,步数和为0,找最小,肯定是0,
所以,初始化为-1,这样遇到没有遍历到的要跳过去

//Find a way


#include 
#include 
#include 
using namespace std;

int map[1001][1001];
bool vis[1001][1001];
int KFC[50001];
int dis[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int m,n,num;

// 坐标结构体
struct Coordinate
{
	int x,y,step;
};

// 判断是否界外或者撞墙或者已经走过
bool isforbid(int x,int y)
{
	if(x<0 || y<0 || x>=m || y>=n)	return 1;
	if(map[x][y]==-1 || vis[x][y]==1)	return 1;
	return 0;
}

void bfs(int x,int y)
{
	// 初始化
	memset(vis,0,sizeof(vis));
	queue  q;	
	Coordinate ft,next;
	int i;

	// 先压入一个值,避免空队列
	ft.x=x,ft.y=y,ft.step=0;
	vis[x][y]=1;
	q.push(ft);
	
	while(!q.empty())
	{

		ft=q.front();
		q.pop();

		// 如果搜索到任何一个KFC,都记录下来,并继续执行
		if(map[ft.x][ft.y]>0)	KFC[map[ft.x][ft.y]]+=ft.step;
		
		for(i=0;i<4;++i)
		{
			next.x=ft.x+dis[i][0];
			next.y=ft.y+dis[i][1];
			if(isforbid(next.x,next.y))	continue;

			next.step=ft.step+1;
			vis[next.x][next.y]=1;
			q.push(next);
		}
	}
}


int main()
{
	char c;
	int i,j,min;
	int yx,yy,mx,my;
	while(cin>>m>>n)
	{
		memset(KFC,-1,sizeof(KFC));
		num=1;

		// 输入时寻找Y点,M点坐标,并且将每一个KFC用1,2,3,4等字符代替
		for(i=0;i>c;
				if(c=='Y')	{yx=i;yy=j;map[i][j]=0;}
				else if(c=='M')	{mx=i;my=j;map[i][j]=0;}
				else if(c=='@')	map[i][j]=num++;
				else if(c=='#')	map[i][j]=-1;
				else	map[i][j]=0;
			}
		

		bfs(yx,yy);
		bfs(mx,my);
		
		
		
		
		// 遍历每个KFC中两人到达距离和,找最小
		min=999999999;
		for(i=1;iKFC[i]+1)?KFC[i]+1:min;
		}
		cout<


你可能感兴趣的:(ACM-搜索,漫漫刷题路)