poj3501Escape from Enemy Territory||hdu2337Escape from Enemy Territory

题目链接:

点我点我

点我点我

题目为:

Escape from Enemy Territory
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 2410 Accepted: 661

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 (xy) 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((x1y1), (x2y2)) = |x2 − x1| + |y2 − 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 NXY. 1 ≤ N ≤  10 000  is the number of enemy bases and 1 ≤ XY ≤  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 xiyi and xryr: the initial position of the commandos and the rendezvous point.

  • N lines each containing one pair of coordinates xy 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


这个题目是预处理+二分+bfs

思路是:

首先预处理出从敌人到图上每个点的最小距离。可以用bfs预处理出来,然后就是二分起点和终点。起点为0,终点为

dis [start.x][start.y],然后最后一个比较坑的就是刚刚在哈弗曼距离上的可以走,然后就是要判重,结果暴了内存。。

代码为:

#include<cstdio>
#include<algorithm>
#include<queue>
#include<iostream>
#include<cmath>
#include<cstring>
#define INF 0x3f3f3f3f
const int maxn=1000+10;
int vis[maxn][maxn];
int dis[maxn][maxn];
using namespace std;
int t,sum,n,m;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};

struct node
{
    int x,y,step;
};

node start,end;

struct Enmy
{
    int x,y;
}enmy[1000*1000];

bool judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m)
        return true;
    return false;
}

void bfs1()
{
    queue<node>Q;
    while(!Q.empty())  Q.pop();
    node current,next;
    for(int i=1;i<=sum;i++)
    {
        current.x=enmy[i].x;
        current.y=enmy[i].y;
        current.step=dis[enmy[i].x][enmy[i].y]=0;
        Q.push(current);
    }
    while(!Q.empty())
    {
        current=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=current.x+dx[i];
            next.y=current.y+dy[i];
            next.step=current.step+1;
            if(judge(next.x,next.y)&&next.step<dis[next.x][next.y])
            {
                dis[next.x][next.y]=next.step;
                Q.push(next);
            }
        }
    }
}

int bfs2(int mid)
{
    queue<node>Q;
    while(!Q.empty())  Q.pop();
    memset(vis,0,sizeof(vis));
    node current,next;
    Q.push(start);
    vis[start.x][start.y]=1;
    while(!Q.empty())
    {
       current=Q.front();
       Q.pop();
       if(current.x==end.x&¤t.y==end.y)
            return current.step;
       for(int i=0;i<4;i++)
       {
           next.x=current.x+dx[i];
           next.y=current.y+dy[i];
           next.step=current.step+1;
           if(judge(next.x,next.y)&&dis[next.x][next.y]>=mid&&!vis[next.x][next.y])
            {
                vis[next.x][next.y]=1;
                Q.push(next);
            }
       }
    }
    return -1;
}

int main()
{
   int u,v,le,ri,safe_distance,ans,ans1,up;
   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d%d",&sum,&n,&m);
       scanf("%d%d%d%d",&start.x,&start.y,&end.x,&end.y);
       start.step=0;
       for(int i=1;i<=sum;i++)
            scanf("%d%d",&enmy[i].x,&enmy[i].y);
       memset(dis,INF,sizeof(dis));
       bfs1();
       le=0;
       ri=dis[start.x][start.y];
       while(le<=ri)
       {
          int mid=(le+ri)/2;
          if(dis[start.x][start.y]<mid||dis[start.x][start.y]<mid)
             ri=mid-1;
          ans=bfs2(mid);
          if(ans!=-1)
          {
              ans1=ans;
              safe_distance=mid;
              le=mid+1;
          }
         else
            ri=mid-1;
        }
        printf("%d %d\n",safe_distance,ans1);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值