#hihocoder #1092 : Have Lunch Together

BFS

{
#ifdef DEBUG_
    ifstream fin("G:/1.txt");
#define cin fin
#endif
    int N,M;
    cin>>N>>M;
    FOR(i,0,N-1){
        cin>>map[i];
    }
    FOR(i,0,N-1)
        FOR(j,0,M-1){
            dis[i][j]=INF;
            if(map[i][j]=='H'){
                startX=i;
                startY=j;
                dis[i][j]=0;
            }
        }
        q.push(make_pair(startX,startY));
        inq[startX][startY]=true;
        pair<int,int>   tmp;
        while (!q.empty())
        {
            tmp=q.front();
            q.pop();
            FOR(i,0,3){//遍历周围四个位置
                int newx=tmp.first+pos[i][0],newy=tmp.second+pos[i][1];
                if(newx>=0&&newx<N&&newy>=0&&newy<N){
                    if(map[newx][newy]!='#'&&map[newx][newy]!='P'){
                        dis[newx][newy]=min(dis[newx][newy],dis[tmp.first][tmp.second]+1);
                        if(map[newx][newy]=='S')
                            inq[newx][newy]=true;
                        else if(!inq[newx][newy])
                            q.push(make_pair(newx,newy));
                            inq[newx][newy]=true;
                    }
                }
            }

        }
    int ans =INF;
    FOR(i,0,N-2){
        FOR(j,0,M-2){
            if(map[i][j]=='S'){
                if(map[i+1][j]=='S')
                    ans=min(ans,dis[i][j]+dis[i+1][j]);

                if (map[i][j+1]=='S' )
                    ans=min(ans,dis[i][j]+dis[i][j+1]);
            }
        }
    }
    if(ans==INF)
        cout<<"Hi and Ho will not have lunch."<<endl;
    else
        cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值