[HDU](2612)Find a way ---bfs

来源:互联网 发布:linux man手册显示中文 编辑:程序博客网 时间:2024/06/03 15:41

Find a way

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


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
 

Author
yifenfei

AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;int vis[1505][1505];char mmap[1505][1505];int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};typedef struct Node{    int x;    int y;    int step;}node;node now,nex,startM,startY;node kk[1505];int StepY[1505][1505];int StepM[1505][1505];int n,m,k;void bfsY(){    memset(vis,0,sizeof(vis));    queue<node>qy;    vis[startY.x][startY.y] = 1;    qy.push(startY);    while(!qy.empty())    {        now = qy.front();        qy.pop();        if(mmap[now.x][now.y]=='@')        {            StepY[now.x][now.y] = now.step;            kk[k].x = now.x;            kk[k].y = now.y;            k++;            continue;        }        for(int i=0;i<4;i++)        {            nex.x = now.x+dir[i][0];            nex.y = now.y+dir[i][1];            if(nex.x>=0 && nex.x<n && nex.y>=0 && nex.y<m && mmap[nex.x][nex.y]!='#'&&!vis[nex.x][nex.y])            {                vis[nex.x][nex.y] = 1;                nex.step = now.step+1;                qy.push(nex);            }        }    }}void bfsM(){    memset(vis,0,sizeof(vis));    queue<node>qm;    vis[startM.x][startM.y] = 1;    qm.push(startM);    while(!qm.empty())    {        now = qm.front();        qm.pop();        if(mmap[now.x][now.y]=='@')        {            StepM[now.x][now.y] = now.step;        }        for(int i=0;i<4;i++)        {            nex.x = now.x+dir[i][0];            nex.y = now.y+dir[i][1];            if(nex.x>=0 && nex.x<n && nex.y>=0 && nex.y<m && mmap[nex.x][nex.y]!='#'&&!vis[nex.x][nex.y])            {                vis[nex.x][nex.y] = 1;                nex.step = now.step+1;                qm.push(nex);            }        }    }}int main(){    while(~scanf("%d %d",&n,&m))    {        memset(mmap,0,sizeof(mmap));        k = 0;        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                cin>>mmap[i][j];                if(mmap[i][j] == 'Y')                {                    startY.x = i;                    startY.y = j;                    startY.step = 0;                }                if(mmap[i][j] == 'M')                {                    startM.x = i;                    startM.y = j;                    startM.step = 0;                }            }        }        bfsY();        bfsM();        int This;        int Min = 999999;        for(int i=0;i<k;i++)        {            This = (StepY[kk[i].x][kk[i].y]+StepM[kk[i].x][kk[i].y])*11;            Min = min(Min,This);        }        printf("%d\n",Min);    }    return 0;}


原创粉丝点击