【HD 2612】Find a way(BFS)

Find a way

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

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
—————————————————————————————————————————————
注意:
1.本题用BFS .
2.注意起点的选取 .

下帖两个代码:
—————————————————————————————————————————————

错误代码:

以@为起点,由于@可以有多个,每一个@都需要运行一次bfs,所以毫无悬念的 Time Limit Exceeded.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
char map[205][205];
int vis[205][205];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m,cnt,ex1,ex2,ey1,ey2;
int sx[40000],sy[40000];
struct st{
    int x;
    int y;
    int time;
    friend bool operator<( st a,st b )
    {
        return a.time > b.time;
    }
};

int bfs(int x, int y)
{
    memset(vis,0,sizeof(vis));
    st ang,next;
    int flag = 0;
    int T1 = 0;
    int T2 = 0;
    ang.x = x;
    ang.y = y;
    ang.time = 0;
    vis[x][y] = 1;
    priority_queue<st>que;
    que.push(ang);
    while( que.size() )
    {
        ang = que.top();
        que.pop();
        if( ang.x == ex1 && ang.y == ey1 ){
            T1 = ang.time;
            flag++; 
        }
        if( ang.x == ex2 && ang.y == ex2 ){
            T2 = ang.time;
            flag++;
        }
        if( flag == 2 ){
            return (T1+T2)*11;
        }
        for( int k=0; k<4; k++ )
        {
            next.x = ang.x + dx[k];
            next.y = ang.y + dy[k];
            if( next.x>=0 && next.y>=0 && next.x<n && next.y<m && !vis[next.x][next.y] && map[next.x][next.y]!='#')
            {
                next.time = ang.time+1;
                vis[next.x][next.y] = 1;
                que.push(next);
            }
        }
    }
    return 0;
}

int main()
{
    while( ~scanf("%d%d",&n,&m) )
    {
        for( int i=0; i<n; i++ )
        {
            scanf("%s",map[i]);
        }
        cnt = 0;
        for( int i=0; i<n; i++ )
            for( int j=0; j<m; j++ )
            {
                if( map[i][j] == 'Y'){
                    ex1 = i;
                    ey1 = j;
                }
                if( map[i][j] == 'M'){
                    ex2 = i;
                    ey2 = j;
                }
                if( map[i][j] == '@'){
                    sx[cnt] = i;
                    sy[cnt] = j;
                    cnt++; 
                }
            }
        int ans = INF;
        for( int i=0; i<cnt; i++ )
        {
            int mid = bfs(sx[i],sy[i]);
            if( mid < ans)
            {
                ans = mid;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

——————————————————————————————————————————————

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
char map[205][205];
int vis[205][205];
int dis1[205][205];
int dis2[205][205];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m;
struct st{
    int x;
    int y;
    int time;
    friend bool operator<( st a,st b )
    {
        return a.time > b.time;
    }
};


void bfs1( int x,int y )
{
    st ang,next;
    memset(vis,0,sizeof(vis));
    for( int i=0; i<n; i++ )
        for( int j=0; j<m; j++ )
        {
            dis1[i][j] = INF;
        }
    ang.x = x;
    ang.y = y;
    ang.time = 0;
    vis[x][y] = 1;
    priority_queue<st>que1;
    que1.push(ang);
    while( que1.size() )
    {
        ang = que1.top();
        que1.pop();
        if( map[ang.x][ang.y] == '@' ){
            dis1[ang.x][ang.y] = ang.time*11;
        }
        for( int k=0; k<4; k++ )
        {
            next.x = ang.x + dx[k];
            next.y = ang.y + dy[k];
            if( next.x>=0 && next.y>=0 && next.x<n && next.y<m && !vis[next.x][next.y] && map[next.x][next.y]!='#')
            {
                next.time = ang.time+1;
                vis[next.x][next.y] = 1;
                que1.push(next);
            }
        }
    }
}
void bfs2( int x,int y )
{
    st ang,next;
    memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++ )
        for(int j=0; j<m; j++ )
        {
            dis2[i][j] = INF;
        }
    ang.x = x;
    ang.y = y;
    ang.time = 0;
    vis[x][y] = 1;
    priority_queue<st>que2;
    que2.push(ang);
    while( que2.size() )
    {
        ang = que2.top();
        que2.pop();
        if( map[ang.x][ang.y] == '@' ){
            dis2[ang.x][ang.y] = ang.time*11;
        }
        for( int k=0; k<4; k++ )
        {
            next.x = ang.x + dx[k];
            next.y = ang.y + dy[k];
            if( next.x>=0 && next.y>=0 && next.x<n && next.y<m && !vis[next.x][next.y] && map[next.x][next.y]!='#')
            {
                next.time = ang.time+1;
                vis[next.x][next.y] = 1;
                que2.push(next);
            }
        }
    }
}


int main()
{
    int sx1,sx2,sy1,sy2;
    while( ~scanf("%d%d",&n,&m) )
    {
        for( int i=0; i<n; i++ )
        {
            scanf("%s",map[i]);
        }
        for( int i=0; i<n; i++ )
            for( int j=0; j<m; j++ )
            {
                if( map[i][j] == 'Y'){
                    sx1 = i;
                    sy1 = j;
                }
                if( map[i][j] == 'M'){
                    sx2 = i;
                    sy2 = j;
                }
            }
        bfs1(sx1,sy1);
        bfs2(sx2,sy2);
        int ans = INF;
        for( int i=0; i<n; i++ )
        {
            for( int j=0; j<m; j++ )
            {
                int mid = dis1[i][j]+dis2[i][j];
                if( mid < ans )
                {
                    ans = mid;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值