POJ 3501 - Escape from Enemy Territory

16 篇文章 0 订阅
5 篇文章 0 订阅
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 <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

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<int> 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<n; i++) {
        Q.push(enemy[i][1] * X + enemy[i][0]);
        vis[enemy[i][0]][enemy[i][1]] = 1;
    }
    while(!Q.empty()) {
        int x = Q.front() % X;
        int y = Q.front() / X;
        Q.pop();
        maxEnemyDis = max(maxEnemyDis, dis[x][y]);
        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] = dis[x][y] + 1;
                Q.push(ny * X + nx);
                vis[nx][ny] = 1;
            }
        }
    }
}

bool bfs(int limtDis, int & minDis) {
    memset(vis, 0, sizeof(vis));
    while(!Q.empty()) Q.pop();
    Q.push(startX * Y + startY);
    vis[startX][startY] = 1;
    pathDis[startX][startY] = 0;
    int ttt = 0;
    while(!Q.empty()) {
        //if(++ttt > 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<n; i++) {
            scanf("%d%d", &enemy[i][0], &enemy[i][1]);
        }
        maxEnemyDis = 0;
        init();
        int L = 0;
        int R = maxEnemyDis;
        // 这里...
        int limtDis = 0;
        int minDis = -1;
        while(L <= R) {
            int mid = (R + L) / 2;
            if(bfs(mid, minDis)) {
                if(mid > limtDis) {
                    limtDis = mid;
                }
                L = mid + 1;
            } else {
                R = mid - 1;
            }
        }
        printf("%d %d\n", limtDis, minDis);
    }

    return 0;
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值