HDU2612 Find a way(双路BFS)

来源:互联网 发布:ubuntu 安装mysql5.7 编辑:程序博客网 时间:2024/04/24 07:57

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11673    Accepted Submission(s): 3792


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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
 

Sample Output
668866
 
简单的两路广搜,与普通广搜不同的只是调用函数两次而已,代码如下:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;char map[210][210];int visit[210][210];int step1[2][210][210];int next1[4][2]={0,1,0,-1,1,0,-1,0};int n,m;struct node{    int x,y,step;};int check(int x,int y)//定义外部函数判断边界以及是否可走{    if(map[x][y]!='#'&&x>=0&&x<n&&y>=0&&y<m)        return 1;    return 0;}void bfs(int a,int x,int y){    memset(visit,0,sizeof(visit));    queue<node>q;    visit[x][y]=1;//起点标记为走过    node temp,point;    temp.x=x;    temp.y=y;    temp.step=0;    q.push(temp);    while(!q.empty())    {        temp=q.front();        q.pop();        if(map[temp.x][temp.y]=='@')        step1[a][temp.x][temp.y]=temp.step;        for(int i=0;i<4;i++)        {            point.x=temp.x+next1[i][0];            point.y=temp.y+next1[i][1];            if(!visit[point.x][point.y]&&check(point.x,point.y))            {                visit[point.x][point.y]=1;                point.step=temp.step+1;                q.push(point);            }        }    }}int main(){    while(~scanf("%d%d",&n,&m))    {        int ax,ay,bx,by;        for(int i=0; i<n; i++)        {            scanf("%s",map[i]);            for(int j=0; j<m; j++)            {                if(map[i][j]=='Y')                {                    ax=i;                    ay=j;                }                else if(map[i][j]=='M')                {                    bx=i;                    by=j;                }            }        }       memset(step1,0x3f3f3f3f,sizeof(step1));//将数组中的所有元素都归为正无穷,方便求最小值        bfs(0,ax,ay);//Y为起点,广搜到每个@的距离        bfs(1,bx,by);//M为起点,广搜到每个@的距离        int step_num=0x3f3f3f3f;//定义为正无穷,为求两人最小步数和做准备        for(int i=0;i<n;i++)            for(int j=0;j<m;j++)        {            if(map[i][j]=='@')            {                step_num=min(step_num,step1[0][i][j]+step1[1][i][j]);            }        }        printf("%d\n",step_num*11);    }}


0 0