Poj3501escape from enemy territory | hdu2337escape from enemy territory

Source: Internet
Author: User

Question link:

Click me

Click me

Subject:

Escape from enemy territory
Time limit:5000 Ms   Memory limit:65536 K
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 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?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 cocould 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 numbersN,X,Y. 1 ≤N≤ 10 000 Is the number of enemy bases and 1 ≤X,Y≤ 1 000 The size of the MAP: CoordinatesX, Y are on the map if 0 ≤X<X, 0 ≤Y<Y.

  • One line containing two pairs of coordinatesXI,YiAndXR,YR: The initial position of the commandos and the rendezvous point.

  • NLines each containing one pair of coordinatesX,YOf 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

21 2 20 0 1 10 12 5 60 0 4 02 12 3

Sample output

1 22 14

Source

Northwestern Europe 2007

This topic is preprocessing + binary + BFS

The idea is:

First, the minimum distance from the enemy to each vertex on the graph is obtained. You can use BFs to pre-process the data, and then the binary start point and end point. The start point is 0, and the end point is

Dis [start. x] [start. Y], and the last pitfall is that the distance from hafman can be taken, and then the judgment is heavy, and the memory is violent ..

Code:

#include<cstdio>#include<algorithm>#include<queue>#include<iostream>#include<cmath>#include<cstring>#define INF 0x3f3f3f3fconst 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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.