hdu 2337 Escape from Enemy Territory

题目:

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

 

题意分析:

有多个敌人据点,在由起点到达终点的过程中,选择的路线要尽可能离敌人的据点远。在这个前提下,使路线的总长度最小。

明显,要用bfs。我自己也是看了别人的代码后才弄懂的。  二分距离(设为s)。在此距离的范围内(<=s)不能走。

AC代码:

 

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
typedef struct
{
    int x,y;
}point;
point a[10010];
int map[1010][1010];
int t,n,X,Y,sx,sy,ex,ey;
using namespace std;
int dis(int x,int y)
{
    return x-y>=0?x-y:y-x;
}
int bfs(int u)
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        if(dis(a[i].x,sx)+dis(a[i].y,sy)<=u)
            return 0;
        if(dis(a[i].x,ex)+dis(a[i].y,ey)<=u)
            return 0;
    }
    for(i=0;i
      
      
       
       =0&&j
       
       
         =0&&y1 
        
          =0&&j 
         
           =0&&y2 
          
            que; point now,temp; map[sx][sy]=0; now.x=sx; now.y=sy; que.push(now); while(!que.empty()) { temp=que.front(); que.pop(); if(temp.x==ex&&temp.y==ey) return 1; if((temp.x+1>=0&&temp.x+1 
           
             =0&&temp.y 
            
              =0&&temp.x-1 
             
               =0&&temp.y 
              
                =0&&temp.x 
               
                 =0&&temp.y+1 
                
                  =0&&temp.x 
                 
                   =0&&temp.y-1 
                  
                    >1; if(bfs(mid)) left=mid+1; else right=mid-1; } bfs(right); printf("%d %d\n",right+1,map[ex][ey]); } } return 0; } 
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值