hdu 2337 Escape from Enemy Territory (预处理+二分+搜索)

在一次深入敌后的任务中,一小队特工完成任务后需要返回集合点。为了避开敌人,他们选择了一条能让他们远离任何敌方基地的路线。他们利用详细的地图规划了最佳撤离路径,确保在地图范围内,且与最近的敌方基地保持最大安全距离。通过二分查找算法确定最短路径长度,并使用BFS验证地图连通性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Escape from Enemy Territory

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 102    Accepted Submission(s): 53


Problem Description
A small group of commandos has infiltrated deep into enemy territory. They have just accomplished their mission and now have to return to their rendezvous point. Of course they don’t want to get caught even if the mission is already over. Therefore they decide to take the route that will keep them as far away from any enemy base as possible.

Being well prepared for the mission, they have a detailed map of the area which marks all (known) enemy bases, their current position and the rendezvous point. For simplicity, we view the the map as a rectangular grid with integer coordinates (x, y) where 0 ≤ x < X, 0 ≤ y < Y. Furthermore, we approximate movements as horizontal and vertical steps on this grid, so we use Manhattan distance: dist((x1, y1), (x2, y2)) = |x2 &#8722; x1| + |y2 &#8722; y1|. The commandos can only travel in vertical and horizontal directions at each step.

Can you help them find the best route? Of course, in case that there are multiple routes that keep the same minimum distance to enemy bases, the commandos want to take a shortest route that does so. Furthermore, they don’t want to take a route off their map as it could take them in unknown, dangerous areas, but you don’t have to worry about unknown enemy bases off the map.
 

Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with three positive numbers N, X, Y. 1 ≤ N ≤ 10 000 is the number of enemy bases and 1 ≤ X, Y ≤ 1 000 the size of the map: coordinates x, y are on the map if 0 ≤ x < X, 0 ≤ y < Y.

One line containing two pairs of coordinates xi, yi and xr, yr: the initial position of the commandos and the rendezvous point.

N lines each containing one pair of coordinates x, y of an enemy base.

All pairs of coordinates are on the map and different from each other.
 

Output
Per testcase:

One line with two numbers separated by one space: the minimum separation from an enemy base and the length of the route.
 

Sample Input
  
  
2 1 2 2 0 0 1 1 0 1 2 5 6 0 0 4 0 2 1 2 3
 

Sample Output
  
  
1 2 2 14
 

Source
 


题意:
找到一个值使得起点到终点连通,在地图中能走到一点的条件为该点与最近的enemy点(哈夫曼距离最近)的距离不小于该值。

思路:
思路很好想到为二分该值然后bfs验证连通性,由于数据比较大,所以每个点的最近enemy点需要预处理,其实一个很简单的bfs就能搞定了,自己做的时候还想多了...

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 10005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,ans,flag,cnt,tot,sum;
int sx,sy,ex,ey;
int le,ri,mid;
int x[MAXN],y[MAXN];
int mp[maxn][maxn];
int vis[maxn][maxn];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
struct Node
{
    int x,y;
}cur,now;
queue<Node>q;

void presolve()
{
    int i,j,t,nx,ny,tx,ty;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        nx=now.x,ny=now.y;
        for(i=0;i<4;i++)
        {
            tx=nx+dx[i];
            ty=ny+dy[i];
            if(tx<0||tx>=n||ty<0||ty>=m||mp[tx][ty]!=-1) continue ;
            mp[tx][ty]=mp[nx][ny]+1;
            cur.x=tx,cur.y=ty;
            sum++;
            if(sum==n*m) return ;
            q.push(cur);
        }
    }
}
bool bfs()
{
    int i,j,t,nx,ny,tx,ty;
    memset(vis,-1,sizeof(vis));
    while(!q.empty()) q.pop();
    cur.x=sx,cur.y=sy;
    if(mp[sx][sy]<mid) return false;
    vis[sx][sy]=0;
    q.push(cur);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        nx=now.x,ny=now.y;
        if(nx==ex&&ny==ey)
        {
            tot=vis[nx][ny];
            return true;
        }
        for(i=0;i<4;i++)
        {
            tx=nx+dx[i];
            ty=ny+dy[i];
            if(tx<0||tx>=n||ty<0||ty>=m||vis[tx][ty]!=-1) continue ;
            if(mp[tx][ty]<mid) continue ;
            vis[tx][ty]=vis[nx][ny]+1;
            cur.x=tx,cur.y=ty;
            q.push(cur);
        }
    }
    return false ;
}
void solve() // 二分找到不满足条件的最小值
{
    int i,j,t;
    le=0,ri=1000001;
    while(le<ri)
    {
        mid=(le+ri)>>1;
        if(bfs()) le=mid+1;
        else ri=mid;
    }
    ans=le-1;
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&cnt,&n,&m);
        scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
        while(!q.empty()) q.pop();
        memset(mp,-1,sizeof(mp));
        for(i=1; i<=cnt; i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            cur.x=x[i],cur.y=y[i];
            mp[x[i]][y[i]]=0;
            q.push(cur);
        }
        sum=cnt;
        presolve();
        solve();
        printf("%d %d\n",ans,tot);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值