[BZOJbegin][NOIP十连测热身赛b]escape(二分+bfs)
2016-11-16 17:27
337 查看
题目描述
传送门题解
预处理每一个点到离它最近的敌人的距离。二分答案,判定的时候也用bfs,只有不在距离范围内的点才能走。
代码
#include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; #define N 1005 int sx[4]={0,0,1,-1}; int sy[4]={1,-1,0,0}; int n,x,y,x1,y1,x2,y2,inf,ans,sum,cnt; struct hp{int x,y;}pt[N*10]; int dis ,step ; queue <hp> q; bool check(int mid) { if (dis[x1][y1]<mid) return false; while (!q.empty()) q.pop(); q.push((hp){x1,y1}); memset(step,127,sizeof(step)); step[x1][y1]=0; while (!q.empty()) { hp now=q.front(); q.pop(); for (int i=0;i<4;++i) { int u=now.x+sx[i],v=now.y+sy[i]; if (u<0||v<0||u>x||v>y||dis[u][v]<mid||step[u][v]!=inf) continue; step[u][v]=step[now.x][now.y]+1; if (u==x2&&v==y2) { cnt=step[u][v]; return true; } q.push((hp){u,v}); } } return false; } void find() { int l=0,r=2000,mid; while (l<=r) { mid=(l+r)>>1; if (check(mid)) ans=mid,sum=cnt,l=mid+1; else r=mid-1; } } int main() { scanf("%d%d%d",&n,&x,&y); x--; y--; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); memset(dis,127,sizeof(dis));inf=dis[0][0]; for (int i=1;i<=n;++i) { scanf("%d%d",&pt[i].x,&pt[i].y); q.push((hp){pt[i].x,pt[i].y}); dis[pt[i].x][pt[i].y]=0; } while (!q.empty()) { hp now=q.front(); q.pop(); for (int i=0;i<4;++i) { int u=now.x+sx[i],v=now.y+sy[i]; if (u<0||v<0||u>x||v>y||dis[u][v]!=inf) continue; dis[u][v]=dis[now.x][now.y]+1; q.push((hp){u,v}); } } find(); printf("%d %d\n",ans,sum); }
相关文章推荐
- 【bzoj 入门OJ】[NOIP 热身赛]Problem B: escape(二分答案+bfs)
- [BZOJbegin][NOIP十连测热身赛]星球联盟(并查集)
- poj 3501 Escape from Enemy Territory 二分+bfs
- hdu 2337 Escape from Enemy Territory (二分枚举+预处理+bfs)
- hdu 2337 Escape from Enemy Territory (presolve bfs +bfs+二分)
- hdu2337 Escape From Enemy Territory---二分bfs+预处理
- [BZOJbegin][NOIP十连测第三场]平均数(二分+归并排序求逆序对)
- 【枚举】【二分答案】【分块答案】【BFS】【最大流】【Dinic】bzoj1189 [HNOI2007]紧急疏散evacuate
- poj 3501 Escape from Enemy Territory 二分+bfs
- Escape from Enemy Territory(二分+bfs)
- BZOJ 1514 _ [POI2006]ZAB-Frogs 单调队列+二分BFS
- 【BZOJ】1189: [HNOI2007]紧急疏散evacuate(二分+bfs+网络流)
- [BZOJ1082][SCOI2005]栅栏 二分+搜索减枝
- 【二分答案】【最大流】bzoj1305 [CQOI2009]dance跳舞
- bzoj4395(技巧BFS)
- bzoj 2756: [SCOI2012]奇怪的游戏(网络流+二分)
- BZOJ 2280 Poi2011 Plot 二分答案+随机增量法
- hdu3681 状压 bfs 二分
- bzoj 3393 && bzoj 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课(BFS)
- 【bzoj 十连测】[noip2016十连测第五场]Problem A: simple(bfs)