POJ 3501 - Escape from Enemy Territory

Escape from Enemy Territory
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 2373   Accepted: 655

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)) = |x2x1| + |y2y1|. 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 andxr, 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

Northwestern Europe 2007



题意:给一个地图,上面有的点是敌人的基地,给一个起始点和目标点,要求找出一条路,这条路上的每个点距离最近的敌人基地为limitDis,路径长度为pathLen,要求limitDis最大的前提下,pathLen最小。输出为limitDis,pathLen

先预处理出每个点最近的二分limitDis,然后bfs,看能不能到达目的地,并得到pathLen。

遇到的问题:
一,是在bfs的时候,continue写在了Q.pop()前面,导致了TLE。
二,二分没有写好




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

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define S64I1(a) scanf(iform1, &(a))
#define P64I1(a) printf(oform1, (a))
#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int moveX[4] = {-1, 1, 0, 0};
const int moveY[4] = {0, 0, -1, 1};
const int maxn = 10000 + 20;
const int maxXY = 1000 + 20;
int enemy[maxn][2];
int vis[maxXY][maxXY];
int dis[maxXY][maxXY];
int pathDis[maxXY][maxXY];
int n, X, Y;
int startX, startY, endX, endY;
int maxEnemyDis;

queue Q;

bool isOk(int x, int y) {
    return x >= 0 && x < X && y >= 0 && y < Y;
}

void init() {
    memset(vis, 0, sizeof(vis));
    memset(dis, 0, sizeof(dis));
    while(!Q.empty()) Q.pop();
    for(int i=0; i X * Y * 2) vis[-100][-100] = 23333;
        int x = Q.front() / Y;
        int y = Q.front() % Y;
        Q.pop();    // 写到下面去了 导致TLE
        if(dis[x][y] < limtDis) continue;
        if(x == endX && y == endY) {
            minDis = pathDis[x][y];
            return true;
        }
        for(int i=0; i<4; i++) {
            int nx = x + moveX[i];
            int ny = y + moveY[i];
            if(isOk(nx, ny) && !vis[nx][ny] && dis[nx][ny] >= limtDis) {
                vis[nx][ny] = 1;
                //if(++ttt > X * Y * 2) vis[-100][-100] = 23333;
                pathDis[nx][ny] = pathDis[x][y] + 1;
                Q.push(nx * Y + ny);
            }
        }
    }
    return false;
}

int main() {
    int T;

    scanf("%d", &T);
    while(T--) {
        scanf("%d%d%d", &n, &X, &Y);
        scanf("%d%d%d%d", &startX, &startY, &endX, &endY);
        for(int i=0; i limtDis) {
                    limtDis = mid;
                }
                L = mid + 1;
            } else {
                R = mid - 1;
            }
        }
        printf("%d %d\n", limtDis, minDis);
    }

    return 0;
}







你可能感兴趣的:(ACM,搜索,二分)