HDU2612Find a way经典广搜

 Find a way

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


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
 

 


Author
yifenfei
 

 


Source
奋斗的年代
 

 


Recommend
yifenfei   |   We have carefully selected several similar problems for you:  2717 1254 1728 2102 1072 



题意:Y和M要去KFC一起吃饭,@为KFC的位置,可以有多个,求出他们到达KFC耗费的最短时间(1个格子耗费11分钟)。
思路:用两次bfs(),分别求出Y和M到各KFC的最短路,相加之后,遍历出最短的总时间。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char map[205][205];
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
int visit[205][205];
int num[205][205],num1[205][205],num2[205][205];
int n,m;
struct nodes
{
    int x;
    int y;
};
nodes node1,node2,a,b;
void bfs(nodes node)
{
    queue<nodes> q;
    while(!q.empty())
        q.pop();
    node1.x=node.x;
    node1.y=node.y;
    num[node.x][node.y]=0;
    visit[node.x][node.y]=1;
    q.push(node1);
    while(!q.empty())
    {
        node1=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            node2.x=node1.x+dir[i][0];
            node2.y=node1.y+dir[i][1];
            if(node2.x>=0&&node2.x<=n&&node2.y>=0&&node2.y<=m&&!visit[node2.x][node2.y]&&map[node2.x][node2.y]!='#')//<=n,<=m
            {
                num[node2.x][node2.y]=num[node1.x][node1.y]+1;
                visit[node2.x][node2.y]=1;
                q.push(node2);
            }
        }
    }
    return ;
}
int main()
{

    while(~scanf("%d%d",&n,&m))
    {
        memset(visit,0,sizeof(visit));
        memset(num,0,sizeof(num));
        memset(num1,0,sizeof(num1));
        memset(num2,0,sizeof(num2));
        int min=100000000;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='Y')
                {
                    a.x=i;
                    a.y=j;
                }
                if(map[i][j]=='M')
                {
                    b.x=i;
                    b.y=j;
                }
            }

        }

        bfs(a);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                num1[i][j]=num[i][j];
            }
        }
        memset(visit,0,sizeof(visit));
        // memset(num,0,sizeof(num));  会全部更新一遍,所以不用初始化
        bfs(b);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                num2[i][j]=num[i][j];
            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                num[i][j]=num1[i][j]+num2[i][j];
                if(map[i][j]=='@'&&num[i][j]<min)
                    min=num[i][j];
            }
        }

        printf("%d\n",min*11);
    }
}


另一种方法

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct p
{
    int x,y;
    int t;
};p f,r;
int n,m,ans;
char map[205][205];
int v1[205][205],v2[2][205][205];
int yi[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void bfs(int x,int y,int k)
{
    queue<p>q;
    while (!q.empty()) q.pop();
    f.x=x;
    f.y=y;
    f.t=0;
    int i;
    q.push(f);
    while (!q.empty())
    {
        r=q.front();
        q.pop();
        f.t=r.t+1;
        for (i=0;i<4;i++)
        {
            f.x=r.x+yi[i][0];
            f.y=r.y+yi[i][1];
            if (f.x>0&&f.x<=n&&f.y>0&&f.y<=m&&map[f.x][f.y]!='#'&&!v1[f.x][f.y])
            {
                if (map[f.x][f.y]=='@') v2[k][f.x][f.y]=f.t;
                v1[f.x][f.y]=1;
                q.push(f);
            }
        }
    }
    return ;
}
int main()
{
    int i,j,x1,x2,y1,y2;
    while (~scanf("%d%d",&n,&m))
    {
        x1=y1=x2=y2=1;
        for (i=1;i<=n;i++)
        for (j=1;j<=m;j++)
        {
            scanf(" %c",&map[i][j]);
            if (map[i][j]=='Y') {x1=i;y1=j;}
            if (map[i][j]=='M') {x2=i;y2=j;}
        }
        memset(v1,0,sizeof(v1));
        memset(v2,0,sizeof(v2));
        v1[x1][y1]=1;
        bfs(x1,y1,0);
        memset(v1,0,sizeof(v1));
        v1[x2][y2]=1;
        bfs(x2,y2,1);
        ans=1000000;
        for (i=1;i<=n;i++)
        for (j=1;j<=m;j++)
        {
            if (v2[0][i][j]&&v2[1][i][j])
            ans=min(ans,v2[0][i][j]+v2[1][i][j]);
        }
        printf("%d\n",11*ans);
    }
}

 

posted @ 2015-08-22 15:41  Lincy*_*  阅读(152)  评论(0编辑  收藏  举报