天天看点

hdoj 2612 Find a wayFind a way

Find a way

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

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
  
  

  
  
   Y和M是朋友,两人要在‘@’处相见!该地区有至少一‘@’,找出一个‘@’,使两人相见并走的路最少!
  
  
    思路:用time[N][N]代表两人到每个‘@’处所用的总时间!
  
  
          找出最小值即可!
  
  
    代码:
  
      
          
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using  namespace std;
#include<queue>
#define  N 250
char  map[N][N];
int time[N][N],visit[N][N];
int mov[4][2]={1,0,-1,0,0,1,0,-1};
struct  node
{
    int x,y;
    int step;
}bg1,bg2;
int n,m;
int judge(int x,int y)
{
    return x>=0&&x<n&&y>=0&&y<m&&!visit[x][y]&&map[x][y]!='#';
}
void  Getp()
{
    char  s;
    int i,j;
    for(i=0;i<n;i++)
    {
        getchar();
        for(j=0;j<m;j++)
    {
        scanf("%c",&map[i][j]);
            s=map[i][j];
            if(s=='Y')
            {
                bg1. x=i;
                bg1. y=j;
                bg1. step=0;
            }
            if(s=='M')
            {
                bg2.x =i;
                bg2.y =j;
                bg2.step =0;
            }
    }
    }
}
void bfs(int mark)
{
    int i,j;
    node p,q;
    queue<node> Q;
    if(mark==1)
    Q.push(bg1);
    else
    Q.push(bg2);
    p=Q.front();
    visit[p.x][p.y]=1;
    while(!Q.empty())
    {
        q=Q.front();
        Q.pop();
        if(map[q.x][q.y]=='@')
        {
            if(mark==1)//第一个人到这里所用的时间!
            time[q.x][q.y]=q.step;
            else
            time[q.x][q.y]+=q.step;//加上第二个人的时间!
        }
        for(i=0;i<4;i++)
        {
            node t;
           t.x =q.x+mov[i][0];
           t.y =q.y+mov[i][1];
           t.step =q.step+1;
           if(judge(t.x,t.y))
           {
               visit[t.x][t.y]=1;
               Q.push(t);
           }
        }
    }
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        Getp();
        memset(time,0,sizeof(time));
        memset(visit,0,sizeof(visit));//注意每次把标记去掉!
        bfs(1);
        memset(visit,0,sizeof(visit));
        bfs(2);
        int t=999;
        for(i=0;i<n;i++)
        for(j=0;j<m;j++)
        {
            if(time[i][j]!=0)
            {
            if(t>time[i][j])
                t=time[i][j];
            }
        }
        printf("%d\n",t*11);
    }
    return  0;
}
           

继续阅读