poj3501Escape from Enemy Territory||hdu2337Escape from Enemy Territory

题目链接:

点我点我

点我点我

题目为:

Escape from Enemy Territory

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 2410   Accepted: 661

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

Source

Northwestern Europe 2007

这个题目是预处理+二分+bfs

思路是:

首先预处理出从敌人到图上每个点的最小距离。可以用bfs预处理出来,然后就是二分起点和终点。起点为0,终点为

dis [start.x][start.y],然后最后一个比较坑的就是刚刚在哈弗曼距离上的可以走,然后就是要判重,结果暴了内存。。

代码为:

#include<cstdio>
#include<algorithm>
#include<queue>
#include<iostream>
#include<cmath>
#include<cstring>
#define INF 0x3f3f3f3f
const int maxn=1000+10;
int vis[maxn][maxn];
int dis[maxn][maxn];
using namespace std;
int t,sum,n,m;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};

struct node
{
    int x,y,step;
};

node start,end;

struct Enmy
{
    int x,y;
}enmy[1000*1000];

bool judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m)
        return true;
    return false;
}

void bfs1()
{
    queue<node>Q;
    while(!Q.empty())  Q.pop();
    node current,next;
    for(int i=1;i<=sum;i++)
    {
        current.x=enmy[i].x;
        current.y=enmy[i].y;
        current.step=dis[enmy[i].x][enmy[i].y]=0;
        Q.push(current);
    }
    while(!Q.empty())
    {
        current=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=current.x+dx[i];
            next.y=current.y+dy[i];
            next.step=current.step+1;
            if(judge(next.x,next.y)&&next.step<dis[next.x][next.y])
            {
                dis[next.x][next.y]=next.step;
                Q.push(next);
            }
        }
    }
}

int bfs2(int mid)
{
    queue<node>Q;
    while(!Q.empty())  Q.pop();
    memset(vis,0,sizeof(vis));
    node current,next;
    Q.push(start);
    vis[start.x][start.y]=1;
    while(!Q.empty())
    {
       current=Q.front();
       Q.pop();
       if(current.x==end.x&¤t.y==end.y)
            return current.step;
       for(int i=0;i<4;i++)
       {
           next.x=current.x+dx[i];
           next.y=current.y+dy[i];
           next.step=current.step+1;
           if(judge(next.x,next.y)&&dis[next.x][next.y]>=mid&&!vis[next.x][next.y])
            {
                vis[next.x][next.y]=1;
                Q.push(next);
            }
       }
    }
    return -1;
}

int main()
{
   int u,v,le,ri,safe_distance,ans,ans1,up;
   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d%d",&sum,&n,&m);
       scanf("%d%d%d%d",&start.x,&start.y,&end.x,&end.y);
       start.step=0;
       for(int i=1;i<=sum;i++)
            scanf("%d%d",&enmy[i].x,&enmy[i].y);
       memset(dis,INF,sizeof(dis));
       bfs1();
       le=0;
       ri=dis[start.x][start.y];
       while(le<=ri)
       {
          int mid=(le+ri)/2;
          if(dis[start.x][start.y]<mid||dis[start.x][start.y]<mid)
             ri=mid-1;
          ans=bfs2(mid);
          if(ans!=-1)
          {
              ans1=ans;
              safe_distance=mid;
              le=mid+1;
          }
         else
            ri=mid-1;
        }
        printf("%d %d\n",safe_distance,ans1);
    }
    return 0;
}

poj3501Escape from Enemy Territory||hdu2337Escape from Enemy Territory,布布扣,bubuko.com

时间: 2024-10-20 02:30:21

poj3501Escape from Enemy Territory||hdu2337Escape from Enemy Territory的相关文章

[Oracle AR]Territory Flexfield

You can use the Territory Flexfield for recording and customized reporting on your territory information. Territory Flexfields are also displayed in the Transaction Detail and Customer Detail reports in Receivables. Receivables provides a default str

ZOJ Problem Set - 1013 Great Equipment ()

ZOJ Problem Set - 1013 Great Equipment Time Limit: 10 Seconds      Memory Limit: 32768 KB Once upon a time, there lived Catherine Ironfist, the Queen of Enroth. One day, she received the news of her father's death. So she sailed for Erathia to attend

codecombat之边远地区的森林31-44关代码分享

codecombat中国游戏网址: http://www.codecombat.cn/ 所有代码为javascript代码分享 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 31.为援兵坚持住 // 食人魔正在爬悬崖 // 为集结民兵组织保护足够长时间的农民. loop { var flag = this.findFlag(); var enemy = this.findNearestEnemy(); if (flag) { // 捡旗子 this.pickUpFla

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

第十篇 面向对象的程序设计

第十篇 面向对象的程序设计 阅读目录 一 面向对象的程序设计的由来 二 什么是面向对象的程序设计及为什么要有它 三 类和对象 3.1 什么是对象,什么是类 3.2 类相关知识 3.3 对象相关知识 3.4 对象之间的交互 3.5 类名称空间与对象/实例名称空间 3.6 小结 四 继承与派生 4.1 什么是继承 4.2 继承与抽象(先抽象再继承) 4.3 继承与重用性 4.4 组合与重用性 4.5 接口与归一化设计 4.6 抽象类 4.7 继承实现的原理(继承顺序) 4.8 子类中调用父类方法 五

雷电C++实现

#include<iostream> #include<windows.h> #include<conio.h> #include<time.h> #include<string> using namespace std; /*=============== all the structures ===============*/ typedef struct Frame { COORD position[2]; int flag; }Frame

站立会议的内容(第八次)

今天利用了软件工程课以及之后的晚自习时间,我们成功完成了对主窗体form.cs的编写 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using

codecombat之边远地区的森林23-30关及地牢40\41关代码分享

codecombat中国游戏网址: http://www.codecombat.cn/ 所有代码为javascript代码分享 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 23.Agrippa防守 loop { var enemy = this.findNearestEnemy(); if(enemy) { // 用 distanceTo 获取与敌人的距离. var distance = this.distanceTo(enemy); // 如果距离小于5米...

unity3d-游戏实战突出重围,整合游戏

结构图: 两个场景,一个是开始界面.一个是游戏界面: 脚本说明:依次是:敌人脚本,主角游戏,主菜单,工具 Enemy 1 using UnityEngine; 2 using System.Collections; 3 4 public class Enemy : MonoBehaviour 5 { 6 7 /*=============================== 8 * 敌人脚本 9 *==============================*/ 10 11 //敌人状态 12