hdu 2337 Escape from Enemy Territory (预处理+二分+宽搜)

C - Escape from Enemy Territory
Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<string>
#define le_f le=(le+1)%500000;
#define ri_f ri=(ri+1)%500000;
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=1200+5;
const int MAXM=1200;
int done[MAXN][MAXM];
int n,X,Y,sx,sy,ex,ey,mid,le,ri;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node
{
	int x,y,w;
	node(int xx=0,int yy=0,int ss=0)
	{
		x=xx;y=yy;w=ss;
	}
}enemy[10500],q[520000];

int MAX(int a,int b){	return a>b?a:b;}
int MIN(int a,int b){	return a<b?a:b;}
bool is_ill(int x,int y)
{
	if(x<0||x>=X||y<0||y>=Y)return 1;	
	return 0;
}
void preslove()
{
	int mmid=mid-1;
	memset(done,0,sizeof(done));
	int i,j,k;
	for(j=1;j<=n;j++)
		for(i=enemy[j].x-mmid;i<=enemy[j].x+mmid;i++)
		{
			int a=enemy[j].y+(mmid-abs(enemy[j].x-i));
			int b=enemy[j].y-(mmid-abs(enemy[j].x-i));
			if(!is_ill(i,a))done[i][a]=1;
			if(!is_ill(i,b))done[i][b]=1;
		}
}

int bfs()
{
	int i;
	for(i=1;i<=n;i++)
		if(abs(enemy[i].x-sx)+abs(enemy[i].y-sy)<mid||abs(enemy[i].x-ex)+abs(enemy[i].y-ey)<mid)return -1;
	preslove();
	le=ri=0;
	q[ri]=node(sx,sy,0);
	ri_f;//预处理,相当于ri++;
	done[sx][sy]=1;
	while(le!=ri)
	{
		node e=q[le];le_f;	
			
		for(i=0;i<4;i++)
		{
			int curx=e.x+dx[i];
			int cury=e.y+dy[i];
			
			if(is_ill(curx,cury)||done[curx][cury])continue;
			if(curx==ex&&cury==ey)
				return e.w+1;
			
			q[ri]=node(curx,cury,e.w+1);
			ri_f;
			done[curx][cury]=1;
		}
	}
	return -1;
}
int main()
{
	//freopen("123.txt","r",stdin);
	int N,m,i;
	while(cin>>N)
	while(N--)
	{
		scanf("%d%d%d",&n,&X,&Y);
		scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
		for(i=1;i<=n;i++)
			scanf("%d%d",&enemy[i].x,&enemy[i].y);
		if(sx==ex&&sy==ey){printf("%d %d\n",0,0);}
		int le=0,ri=X+Y,ans;		
		while(le<ri)
		{
			mid=(le+ri)/2;
			if(bfs()==-1)ri=mid;
			else	
			{
				ans=mid;
				le=mid+1;
			}
		}
		printf("%d %d\n",ans,(mid=ans,bfs()));
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值