Escape from Enemy Territory(二分+bfs)

Escape from Enemy Territory

Time Limit: 5000ms
Memory Limit: 65536KB
64-bit integer IO format:  %I64d      Java class name:  Main
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)) = |x2 &#8722; x1| + |y2 &#8722; 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 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 and xr, 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

#include <cstdio>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;

int n, m, n_enemys;
int stx, sty, endx, endy;

struct Node{
   int x;
   int y;
   int len;
   Node(int x = 0, int y = 0, int len = 0){
       this ->x = x;
       this ->y = y;
       this ->len = len;
   }
};
queue<Node> q;
bool vis[1010][1010];
int length[1010][1010];

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};


void init_length()
{
    while(!q.empty()){
        Node a = q.front();
        q.pop();
        int x = a.x;
        int y = a.y;
        for(int i = 0; i < 4; i++){
            int x2 = x + dx[i];
            int y2 = y + dy[i];
            if(length[x2][y2] == -1){
                length[x2][y2] = a.len + 1;
                Node b(x2, y2, a.len+1);
                q.push(b);
            }
        }
    }
}

void init_vis(){
   for(int  i=1;i<=n;i++){
     for(int  j=1;j<=m;j++){
        vis[i][j]=false;
     }
   }
}

int judge(int len)//给定一个 距离敌人的最小距离,判断这个距离能不能到达终点  不能返回 -1 能的话返回长度
{
    if(length[stx][sty] < len || length[endx][endy] < len) return -1;
    init_vis();
    while(!q.empty()) q.pop();
    if(stx == endx && sty == endy) return 0;
    Node a(stx, sty, 0);
    vis[stx][sty] = true;
    q.push(a);
    while(!q.empty()){
        Node top = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            int x = top.x + dx[i];
            int y = top.y + dy[i];
            if(x == endx && y == endy) return top.len + 1;
            if(x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y]){
                vis[x][y] = true;
                if(length[x][y] >= len){
                    Node tmp(x, y, top.len + 1);
                    q.push(tmp);
                }
            }
        }
    }
    return -1;
}

void output()
{
    int left = 1;
    int right = n + m;
    int mid;
    int dis;
    while(left <= right){
        mid = (left + right) >> 1;
        dis = judge(mid);
        if(dis == -1){
            right = mid - 1;
        }
        else{
            left = mid + 1;
        }
    }
    cout << right << " " << judge(right) << endl;
}

int main()
{
    int T;
    cin >> T;
    while(T--){
        cin >> n_enemys >> n >> m;
        cin >> stx >> sty >> endx >> endy;
        stx++, sty++, endx++, endy++;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                vis[i][j]=false;
                length[i][j]=-1;
            }
        }
        while(!q.empty()) q.pop();
        for(int i = 1; i <= n_enemys; i++){
            int x, y;
            cin >> x >> y;
            length[x+1][y+1] = 0;
            vis[x+1][y+1] = true;
            Node a(x+1, y+1, 0);
            q.push(a);
        }
        init_length();
        output();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值