hdu2612(bfs)

Find a way

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


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:  1254 1728 2102 1072 1175 
 题目意思:给定一张图,起点和终点,求经过'@'点的路径中的最短路径.
从起点和终点开始搜,分别记录两种最短路径,有一个坑(对我来说)就是没考虑到可能有从起点或者终点不能到达某个'@'点,所以需要初始化两个距离数组为无穷大.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 210
#define inf  0x3f3f3f3f3f3f
char Map[maxn][maxn];
int visit[maxn][maxn];
struct node
{
    int x,y,dist[2]={0};
};
struct point
{
    int x,y;
};
point ans[maxn*maxn];
int Max;
int dir[4][2]={0,-1,0,1,-1,0,1,0};
int DIS1[maxn][maxn];
int DIS2[maxn][maxn];
node start,End;
int n,m;
int cnt;
void init()
{
    Max=inf;
    cnt=0;
    memset(visit,0,sizeof(visit));
    for(int i=0;i<maxn;i++)
        for(int j=0;j<maxn;j++)
    {
        DIS1[i][j]=inf; DIS2[i][j]=inf;
    }
}
void bfs(node Point,int kk,int CNT)
{
     queue <node>  que;
      node aa;
      que.push(Point);
      visit[Point.x][Point.y]=1;
    while(!que.empty())
    {
       aa=que.front();
       que.pop();
       if(Map[aa.x][aa.y]=='@')
       {
           if(kk==0)
           {
               DIS1[aa.x][aa.y]=aa.dist[0];
             //  printf("start %d %d %d\n",aa.x,aa.y,DIS1[aa.x][aa.y]);
           }
           if(kk==1)
           {
               DIS2[aa.x][aa.y]=aa.dist[1];
             //  printf("End %d %d %d\n",aa.x,aa.y,DIS2[aa.x][aa.y]);
           }
           CNT--;
           if(CNT==0)
             return ;

       }
       for(int i=0;i<4;i++)
       {
            node bb;
           int next_x=aa.x+dir[i][0];
           int next_y=aa.y+dir[i][1];
           if(1<=next_x && next_x<=n && 1<=next_y && next_y<=m)
             if( (Map[next_x][next_y]!='#') && visit[next_x][next_y]==0)
           {
               bb.x=next_x; bb.y=next_y;
               bb.dist[kk]=aa.dist[kk]+11;
               visit[next_x][next_y]=1;
               que.push(bb);
           }
       }

    }
}
int main()
{
   // freopen("test.txt","r",stdin);
    while(~scanf("%d%d%*c",&n,&m))
    {
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",&(Map[i][1]));
            for(int j=1;j<=m;j++)
            {
              if(Map[i][j]=='Y')
              {
                  start.x=i; start.y=j;
                  start.dist[0]=0;
              }
              if(Map[i][j]=='M')
              {
                  End.x=i; End.y=j;
                  End.dist[1]=0;
              }
              if(Map[i][j]=='@')
              {
                  cnt++;
                  ans[cnt].x=i; ans[cnt].y=j;
              }
            }
        }
      /*  for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            printf("%c",Map[i][j]);
            printf("\n");
        }*/
        //cout<<cnt<<endl;
         bfs(start,0,cnt);
         memset(visit,0,sizeof(visit));
         bfs(End,1,cnt);
         for(int i=1;i<=cnt;i++)
        {
            int pp,qq;
            pp=ans[i].x;  qq=ans[i].y;
            Max=min(Max,DIS1[pp][qq]+DIS2[pp][qq]);
        }
        printf("%d\n",Max);
       // cout<<endl;
    }
    return 0;
}
View Code

 

posted on 2015-08-07 11:11  爱装逼的书呆子  阅读(193)  评论(0编辑  收藏  举报

导航