HDU 2612 Find a way BFS 2次广搜

来源:互联网 发布:通过ip查mac地址命令 编辑:程序博客网 时间:2024/04/26 15:23

Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8395 Accepted Submission(s): 2703

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

找到一家离两人距离最近的肯德基店。
第1个人广搜一次 第2个人广搜一次 用一个三维数组来记录某个点离某个人的距离,最后在所有肯德基中找到离两人最近的一家。
广搜水题 感觉也不用解释太多

一组很有用的测试数据
5 5
Y..#@
…M#
…..
…..
@….

110

3维数组好像不能用memset,至少我现在不会,用memset初始化不了
= = 以后要注意这一点

直接上代码

#include <iostream>#include <cstring>#include <queue>using namespace std;int m,n;char edge[205][205];int visited[205][205];int dis[205][205][5];int flag;int dir[4][2]={0,1,1,0,0,-1,-1,0};struct node{    int x;    int y;        int step;};int judge(int x,int y){    if(x<1||y<1||x>m||y>n||edge[x][y]=='#'||visited[x][y]==1)return 0;    return 1;}void BFS(int x,int y){    queue<node>Q;    node cur,next;    visited[x][y]=1;    cur.x=x;    cur.y=y;    cur.step=0;    Q.push(cur);        while(!Q.empty())    {        cur=Q.front();        Q.pop();        dis[cur.x][cur.y][flag]=cur.step;    for(int i=0;i<4;i++)                    {            next.x=cur.x+dir[i][0];            next.y=cur.y+dir[i][1];            if(judge(next.x,next.y)==0)            continue;            visited[next.x][next.y]=1;            next.step=cur.step+1;            Q.push(next);        }    }}int main(int argc, char *argv[]){        int x1,y1,x2,y2;    int min;    while(cin>>m>>n)    {    for(int i=1;i<=m;i++)            for(int j=1;j<=n;j++)        {            cin>>edge[i][j];            if(edge[i][j]=='Y')            {x1=i;            y1=j;            }            else if(edge[i][j]=='M')            {                x2=i;                y2=j;            }        }    for(int i=0;i<205;i++)    for(int j=0;j<205;j++)    for(int k=0;k<2;k++)    dis[i][j][k]=999;    //memset(dis,99,sizeof(dis[0][0][0]));    memset(visited,0,sizeof(visited));    flag=0;    BFS(x1,y1);    memset(visited,0,sizeof(visited));    flag=1;    BFS(x2,y2);    min=9999;    for(int i=1;i<=m;i++)        for(int j=1;j<=n;j++)        {            if(edge[i][j]=='@')            {                if(min>(dis[i][j][0]+dis[i][j][1]))                min=dis[i][j][0]+dis[i][j][1];            }        }    cout<<11*min<<endl;    }    return 0;}
0 0
原创粉丝点击