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

来源:互联网 发布:北京网络微信投票公司 编辑:程序博客网 时间:2024/06/05 07:34
C - Escape from Enemy Territory
Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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

21 2 20 0 1 10 12 5 60 0 4 02 12 3

Sample Output

1 22 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