hdu 2337 Escape from Enemy Territory (presolve bfs +bfs+二分)

题目大意:

地图上有很多敌人据点,主角想要离这些据点越远越好,但是又要到达终点。


思路:

既然是越远越好又要到达终点,当然我们需要枚举他到底能离据点多远。

这个时候想着用二分枚举这个距离。

然后我们用一个bfs预处理出地图中所有的店离据点的最近距离。(第一次写bfs的预处理。。。安静

接下来就是二分的过程了。


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

int sx,sy,tx,ty;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
bool vis[1005][1005];
int map[1005][1005];
int tot,n,m;
int maxdis,tans,ans;
struct Inf
{
    int x,y,dep;
};

bool ok(int xx,int yy)
{
    if(xx>=0&&xx<n&&yy>=0&&yy<m)return true;
    return false;
}
void debug()
{
    cout<<"---"<<endl;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            printf("%d ",map[i][j]);
        }
        puts("");
    }
    cout<<"---"<<endl;
}
void presolve()
{
    queue<Inf>Q;

    for(int i=1;i<=tot;i++)
    {
        Inf tmp;
        scanf("%d%d",&tmp.x,&tmp.y);
        tmp.dep=0;
        map[tmp.x][tmp.y]=0;
        Q.push(tmp);
    }

    Inf w,e;
    while(!Q.empty())
    {
        w=Q.front();
        Q.pop();

        for(int k=0;k<4;k++)
        {
            e=w;
            e.x+=dx[k];
            e.y+=dy[k];
            e.dep++;

            if(ok(e.x,e.y)&&map[e.x][e.y]==-1)
            {
                map[e.x][e.y]=e.dep;
                maxdis=max(maxdis,e.dep);
                Q.push(e);
            }
        }
    }
}

bool bfs(int dis)
{
    queue<Inf>Q;

    Inf w,e;

    w.x=sx;
    w.y=sy;
    w.dep=0;
    Q.push(w);

    while(!Q.empty())
    {
        w=Q.front();
        Q.pop();

        for(int k=0;k<4;k++)
        {
            e=w;
            e.x+=dx[k];
            e.y+=dy[k];
            e.dep++;

            if(ok(e.x,e.y)&&!vis[e.x][e.y]&&map[e.x][e.y]>=dis)
            {
                if(e.x==tx && e.y==ty)
                {
                 //   cout<<dis<<"---"<<endl;
                    tans=e.dep;
                    return true;
                }
                vis[e.x][e.y]=true;
                Q.push(e);
            }
        }
    }
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(map,-1,sizeof(map));
        maxdis=-1;ans=-1;

        scanf("%d%d%d",&tot,&n,&m);
        scanf("%d%d%d%d",&sx,&sy,&tx,&ty);

        presolve();
      //  debug();
        int bot=0,top=maxdis+10,mid;
        while(bot<top)
        {
            mid=(bot+top)>>1;
            if(map[sx][sy]<mid)
            {
                top=mid;
                continue;
            }
            memset(vis,false,sizeof vis);
            if(bfs(mid))
            {
                bot=mid+1;
                ans=max(ans,tans);
              //  printf("tans=%d \n",tans);
            }
            else top=mid;
        }
        printf("%d %d\n",bot-1,ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值