HDU2612---(两次BFS)

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拼在一起,一个从起点开始搜索,一个从终点开始搜索

当两个BFS搜索的轨迹有重合的部分的时候,立即停止两个BFS,并根据当前重合点的与两个BFS已得到的信息来计算答案。

求2个点到KFC的距离之和,使其最小,可用2次BFS,

分别求出2个点到各个KFC的最短距离,

然后找出和最小的即可

利用队列来做BFS

#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
int dx[4]={1,0,-1,0};//设定SS的4个方向
int dy[4]={0,1,0,-1};
int n,m;
int vis[210][210],dist[210][210],distY[210][210],a[210][210];
void bfs(int x,int y){
	queue<int> q;
	int u=x*m+y;
	q.push(u);
	vis[x][y]=1;//表示设置的区域没有被访问过
	dist[x][y]=0;
	while(!q.empty()){
		u.front();//在一边排好队的最前面
		q.pop();//入队
		x=u/m;y=u%m;
		for(int i=0;i<4;i++){
			int nx=x+dx[i],ny=y+dy[i];//探头
			//如果找到目标,并且没有被访问过
			if(nx>=0&&ny>=0&&nx<n&&ny<n&&!vis[nx][ny]&&a[nx][ny]==0){
			//重新设置新的方向,不会往上s
			u=nx*m+ny;
			vis[nx][ny]=1;
			dist[nx][ny]=dist[x][y]+1;//往下加一层
			q.push(u);//新的值入队,直到不满足条件

			}
		}
	}

}
int main(){
	int i,j,Yx,Yy,Mx,My;
	char s[210][210];
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(a,0,sizeof(a));
		memset(vis,0,sizeof(vis));
		memset(dist,0,sizeof(dist));
		memset(distY,0,sizeof(distY);
		for(i=0;i<n;i++){
			scanf("%s",&s[i]);
			for(j=0;j<m;j++){
				if(s[i][j]=='#') a[i][j]=1;  //表示不能走
                if(s[i][j]=='Y') {Yx=i,Yy=j;}  //表示Y的起始位置
                if(s[i][j]=='M') {Mx=i,My=j;}//表示M的起始位置

			}
		}
		bfs(Yx,Yy);//让Y开始向下BFS
		printf("%d %d\n",Yx,Yy);
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(s[i][j]=='@')//如果到达目的地,
				distY[i][j]=dist[i][j];//就把这个坐标点的位置赋给Y,它的功能就完成了,就坐等M了
			}
			printf("%d\n",distY[i][j]);//打印
			//开始让Mss
			memset(a,0,sizeof(a));
		    memset(vis,0,sizeof(vis));
			memset(dist,0,sizeof(dist));'
			for(i=0;i<n;i++)
				for(j=0;j<m;j++)
					if(s[i][j]=='#')
					a[i][j]=1;//表示不能走
			bfs(Mx,My);//M开始向上BFS
			int min=1000000;
			for(i=0;i<n;i++)
				for(j=0;j<m;j++)
				//这个条件相当重要,此题能不能AC就取决于它,计算走过的最小值
				if(s[i][j]=='@'&&dist[i][j]+distY[i][j]<min&&dist[i][j]&&distY[i][j]){
					min=dist[i][j]+distY[i][j];
				}
			printf("%d\n",min*11);

		}
	}
}

利用数组来做,其实原理都差不多

#include <stdio.h>
#include <cstring>
#define Max 0x7f7f7f7f
using namespace std;
int visited1[205][205];
int visited2[205][205];
int ans1[205][205];
int ans2[205][205];
int x1,y1,x2,y2;
int n,m;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char map[205][205];
struct node
{
    int x;
    int y;
};
node path[80000];
void bfs(int x ,int y,int ans[205][205],int visited[205][205])
{
    memset(visited,0,sizeof(visited));
    memset(ans,Max,sizeof(ans));
    path[0].x=x;
    path[0].y=y;
    ans[x][y]=0;
    int head=0;
    int tail=1;
    visited[x][y]=1;
    while(head<tail)
    {
        x=path[head].x;
        y=path[head].y;
        for(int i=0;i<4;i++)
        {
            int xx=x+dir[i][0];
            int yy=y+dir[i][1];
            if(visited[xx][yy]==0 && xx>=0 && xx<n && yy>=0 && yy<m && map[xx][yy]!='#')
            {
                path[tail].x=xx;
                path[tail].y=yy;
                tail++;
                ans[xx][yy]=ans[x][y]+1;
                visited[xx][yy]=1;
            }
        }
        head++;
    }
}
int main()
{
    char ch;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&ch);
                map[i][j]=ch;
                if(ch=='Y')
                {
                    x1=i;
                    y1=j;
                }
                if(ch=='M')
                {
                    x2=i;
                    y2=j;
                }
            }
            getchar();
        }
        memset(visited1,0,sizeof(visited1));
        memset(ans1,Max,sizeof(ans1));
        bfs(x1,y1,ans1,visited1);
        memset(visited2,0,sizeof(visited2));
        memset(ans2,Max,sizeof(ans2));
        bfs(x2,y2,ans2,visited2);
        int min=Max;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(map[i][j]=='@'&& ans1[i][j]!=Max && ans2[i][j]!=Max)
                {
                    if(min>ans1[i][j]+ans2[i][j])
                    min=ans1[i][j]+ans2[i][j];
                }
            }
        }
        printf("%d\n",min*11);
    }

    return 0;
}
上一篇:布置theano(Ubuntu14.04 LTS)


下一篇:Markdown 基本入门使用