POJ3501 Escape from Enemy Territory BFS

简单的BFS,先预处理一下每个点到enemy的·最近距离,然后二分BFS一下就可以了,还有就是二分要注意


Escape from Enemy Territory
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 2274   Accepted: 632

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

#include
#include
#include
#include
#include
#include

using namespace std;

#define INF 0x3f3f3f3f

int n,x,y;
int g[1100][1100];
int dist[1100][1100];
int ax[10100],ay[10100];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int sx,sy,ex,ey;

struct node
{
    int x,y;
    int dis;
}q[59999999];

int front,rear;

bool jud1(int xx,int yy,int dis)
{
    if(xx<0||xx>=x) return false;
    if(yy<0||yy>=y) return false;
    if(dist[xx][yy]<=dis) return false;
    return true;
}

void bfs1()
{
    node a;
    front=rear=0;
    for(int i=0;i=x) return false;
    if(yy<0||yy>=y) return false;
    if(dist[xx][yy]>1;
            if((d=bfs2(m))!=-1&&m>=ret1)
            {
                ret1=m;
                ret2=d;
                l=m+1;
            }
            else
                r=m-1;
        }
        printf("%d %d\n",ret1,ret2);
    }
    return 0;
}


你可能感兴趣的:(搜索)