题目大意

给你一张nn*mm矩形地图。上面有些点上有敌营。给你起点和终点, 你找出一条最优路径。满足最优路径上的点离敌营的最近最短距离是所有路径最短的。若有多条找路径最短的一条。

分析

通过二分来确定路径离敌营最短距离。然后bfs来验证。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define maxn 1010
#define MAXN 2005
#define MAXM 20000005
#define INF 100000000000000
#define oo 1000000007
using namespace std; typedef long long LL;
struct Point
{
int x,y,dist;
}p[maxn*maxn];
int mid,n,nn,mm,stx,sty,edx,edy;
int vist[maxn][maxn],vistdist[maxn][maxn];
int dirx[]={,-,,};
int diry[]={-,,,};
int max_dist,dist1,dist2;
void bfs1()
{
queue<Point> q;
while(!q.empty())
q.pop();
for(int i=;i<n;i++)
q.push(p[i]);
while(!q.empty())
{
Point tem=q.front();
q.pop(); for(int i=;i<;i++)
{
int newx=tem.x+dirx[i];
int newy=tem.y+diry[i];
if(newx>=&&newx<mm&&newy>=&&newy<nn&&!vist[newx][newy])
{
vist[newx][newy]=;
int newdist=tem.dist+;
vistdist[newx][newy]=newdist;
max_dist=max(max_dist,newdist);
Point pp;
pp.x=newx;
pp.y=newy;
pp.dist=newdist;
q.push(pp);
}
}
}
}
int bfs2()
{
queue<Point> q;
while(!q.empty())
q.pop();
Point pp;
pp.x=stx;
pp.y=sty;
pp.dist=;
q.push(pp);
memset(vist,,sizeof(vist));
if(vistdist[stx][sty]<mid)
return ; while(!q.empty())
{
Point tem=q.front();
q.pop();
if(tem.x==edx&&tem.y==edy)
{
dist1=mid;
dist2=tem.dist;
return ;
}
for(int i=;i<;i++)
{
int xx=tem.x+dirx[i];
int yy=tem.y+diry[i];
if(xx>=&&xx<mm&&yy>=&&yy<nn&&!vist[xx][yy]&&vistdist[xx][yy]>=mid)
{
vist[xx][yy]=;
Point pp;
pp.x=xx;
pp.y=yy;
pp.dist=tem.dist+;
q.push(pp);
} }
}
return ; }
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d",&n,&mm,&nn);
scanf("%d %d %d %d",&stx,&sty,&edx,&edy);
memset(vist,,sizeof(vist));
for(int i=;i<n;i++)
{
scanf("%d %d",&p[i].x,&p[i].y);
p[i].dist=;
vistdist[p[i].x][p[i].y]=;
vist[p[i].x][p[i].y]=;
}
max_dist=-;
bfs1();
int l=,r=max_dist;
while(l<=r)
{
mid=(l+r)/;
if(bfs2())
l=mid+;
else
r=mid-; }
printf("%d %d\n",dist1,dist2);
}
return ;
}

注意的是队列数组别开小了,还有就是插入队列的时候应该这样写

     Point pp;
pp.x=newx;
pp.y=newy;
pp.dist=newdist;
q.push(pp);

如果这样写就会CE

 q.push((Point){newx,newy,newdist});

hdu 2337 Escape from Enemy Territory的更多相关文章

  1. poj 3501 Escape from Enemy Territory 二分+bfs

    水题,不解释. #include<stdio.h> #include<math.h> #include<cstring> #include<algorithm ...

  2. poj 3501 Escape from Enemy Territory 预处理+二分+bfs

    传送门 给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标. 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离. 首先预处理出来地图上的所有点到敌人的最短距离, ...

  3. HDU 3533 Escape(大逃亡)

    HDU 3533 Escape(大逃亡) /K (Java/Others)   Problem Description - 题目描述 The students of the HEU are maneu ...

  4. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

  5. Hdu 3605 Escape (最大流 + 缩点)

    题目链接: Hdu 3605  Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是T ...

  6. HDU 3605 Escape(状压+最大流)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  7. HDU 3605 Escape 最大流+状压

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 2000/1000 MS (Java/Others)    ...

  8. hdu 3605 Escape 二分图的多重匹配(匈牙利算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  9. HDU 3533 Escape bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=3533 一道普通的bfs,但是由于代码实现出了bug还是拖了很久甚至对拍了 需要注意的是: 1.人不能经过炮台 2 ...

随机推荐

  1. ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置

    核心提示:大家一定经常在网络上看到很多网站的地址后缀都是用XX.HTML或者XX.ASPX等类似静态文件的标示来操作的吧,那么大家有怀疑过他真的是一个一个的静态生成的文件么,静态文件的生成的优缺有好有 ...

  2. 一模 (4) day2

    第一题: 题目大意:二进制数 n mod m 的结果是多少?  n 的长度(二进制数的位数)<=200 000:  m 的长度(二进制数的位数)<=20. 解题过程: 1.我的算法是直接高 ...

  3. php判断用户浏览器类型是否为微信浏览器

    PHP方法:利用PHP的“_SERVER ”数组“HTTP_USER_AGENT”项,获取该页面的用户代理的信息,来完成这个工作. [winows/chrome] 输出结果: Mozilla/5.0 ...

  4. jquery判断多选框是否选中

    if($("#xieyi").is(":checked")){ alert('选中'); }else{ alert('没有选中') }

  5. html中offsetTop、clientTop、scrollTop、offsetTop

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  6. 单人SVN提交bug

    The working copy "初识tableVIew" failed to commit files. fatal: Unable to create '/Users/zjj ...

  7. OpenCV之响应鼠标(四):在图像上绘制出矩形并标出起点的坐标

    涉及到两方面的内容:1. 用鼠标画出矩形.2.在图像上绘制出点的坐标 用鼠标绘制矩形,涉及到鼠标的操作,opencv中有鼠标事件的介绍.需要用到两个函数:回调函数CvMouseCallback和注册回 ...

  8. Android Studio 使用genymotion 模拟器运行app时 提示找不到任何设备

    原因是使用了genymotion 默认的Android toos .打开genymotion  选择设置  ADB  使用自己的SDKtools 选择Android Studio 使用的SDK位置就行 ...

  9. ODI 12.1.3发布,提升支持大数据的能力

    此次发布的ODI新版本,目的是更好的支持当前市场上的大数据平台. 大数据基因在不改变ODI工作效率的情况下,ODI增加了越来越多的数据源集成能力.ODI是在Oracle平台上标准的E-LT工具,事实上 ...

  10. ODI 12c 安装

    软件下载地址: http://www.oracle.com/technetwork/middleware/data-integrator/downloads/index.html 下载这个版本: Or ...