题目大意

给你一张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. 二模 (3) day2

    第一题: 题目大意:(难以概括,就不贴了把.) 解题过程: 1.担心被精度问题恶心,就把平均数的地方乘了N,这样只有最后计算的时候才会是小数.. 2.数组保存的时候蛋疼的 没改成double.结果全部 ...

  2. 线程系列3---ThreadLocal类研究

    2013-12-23 17:44:44 Java为线程安全提供了一些工具类,如ThreadLocal类,它代表一个线程局部变量,通过把数据放在ThreadLocal中就可以让每个线程创建一个该变量的副 ...

  3. win7 MS SQL SERVER 2000安装

    http://blog.chinaunix.net/uid-24398518-id-2156226.html MicrosoftInternetExplorer402DocumentNotSpecif ...

  4. Date的转换

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String str = sdf.format(一个date对 ...

  5. Http Framework

    http request clientVolley https://android.googlesource.com/platform/frameworks/volley聚划算用的litehttp h ...

  6. Jdk配置串在profile中

    JAVA_HOME=/home/will/appSource/jdk1.7.0_25PATH=$JAVA_HOME/bin:$PATHCLASSPATH=.:$JAVA_HOME/lib/dt.jar ...

  7. Palindrome Number ---- LeetCode 009

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  8. IIS发布错误

    发布程序时遇到的错误:

  9. Android 线程模型

    Android 线程模型 1. import android.os.Handler;  import android.os.Message; public class MainActivity ext ...

  10. iOS:测试机添加

    一,首先打开开发者首页:https://developer.apple.com/,点击Member Center二,点击certificates,Identifiers & Profiles三 ...