poj 3501 Escape from Enemy Territory(预处理&二分&bfs)
来源:互联网 发布:肖骁绝对是直男 知乎 编辑:程序博客网 时间:2024/06/05 09:28
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 (x, y) 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((x1, y1), (x2, y2)) = |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 N, X, Y. 1 ≤ N ≤
10 000 is the number of enemy bases and 1 ≤ X, Y ≤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 xi, yi and xr, yr: the initial position of the commandos and the rendezvous point.
N lines each containing one pair of coordinates x, y 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
21 2 20 0 1 10 12 5 60 0 4 02 12 3
Sample Output
1 22 14
Source
题意:
给你一张X*Y矩形地图。上面有些点上有敌营。给你起点和终点要你找出一条最优路径。满足最优路径上的点离敌营的最近最短距离是所有路径最短的。若有多条找路径最短的一条。
思路:
感觉有的dfs的迭代加深。通过二分来确定路径离敌营最短距离。然后bfs来验证。
详细见代码:
#include<algorithm>#include<iostream>#include<string.h>#include<sstream>#include<stdio.h>#include<math.h>#include<vector>#include<string>#include<queue>#include<set>#include<map>//#pragma comment(linker,"/STACK:1024000000,1024000000")using namespace std;const int INF=0x3f3f3f3f;const double eps=1e-8;const double PI=acos(-1.0);const int maxn=100010;typedef __int64 ll;int n,X,Y,sx,sy,ex,ey,mdis,head,tail,le,ri,ansd,anss;int vis[1010][1010],dis[1010][1010];int dx[4]={-1,0,1,0};int dy[4]={0,1,0,-1};struct yb{ int x,y,sp;} q[1010*1010];//开始队列开小了。wa了void prebfs()//预处理出每个地方离敌营的最短距离{ int i,x,y,nx,ny; head=0; tail=n; while(head<tail) { x=q[head].x; y=q[head].y; for(i=0;i<4;i++) { nx=x+dx[i]; ny=y+dy[i]; if(nx>=0&&nx<X&&ny>=0&&ny<Y&&!vis[nx][ny]) { vis[nx][ny]=1; dis[nx][ny]=q[head].sp+1; ri=max(ri,dis[nx][ny]);//离敌营最大距离 q[tail].x=nx; q[tail].y=ny; q[tail++].sp=q[head].sp+1; } } head++; }}bool bfs(){ int i,x,y,nx,ny; if(dis[sx][sy]<mdis) return false; memset(vis,0,sizeof vis); head=tail=0; q[tail].x=sx; q[tail].y=sy; q[tail++].sp=0; while(head<tail) { x=q[head].x; y=q[head].y; if(x==ex&&y==ey)//由于是bfs所以路径一定是最短的。 { ansd=mdis; anss=q[head].sp; return true; } for(i=0;i<4;i++) { nx=x+dx[i]; ny=y+dy[i]; if(nx>=0&&nx<X&&ny>=0&&ny<Y&&!vis[nx][ny]&&dis[nx][ny]>=mdis) { vis[nx][ny]=1; q[tail].x=nx; q[tail].y=ny; q[tail++].sp=q[head].sp+1; } } head++; } return false;}int main(){ int cas,i,j,x,y; scanf("%d",&cas); while(cas--) { scanf("%d%d%d",&n,&X,&Y); scanf("%d%d%d%d",&sx,&sy,&ex,&ey); memset(vis,0,sizeof vis); for(i=0;i<n;i++) { scanf("%d%d",&x,&y); q[i].x=x; q[i].y=y; q[i].sp=dis[x][y]=0; vis[x][y]=1; } ri=0; prebfs(); le=0; while(le<=ri)//二分最优路径离敌营的最短距离 { mdis=(le+ri)>>1; if(bfs()) le=mdis+1; else ri=mdis-1; } printf("%d %d\n",ansd,anss); } return 0;}
- poj 3501 Escape from Enemy Territory(预处理&二分&bfs)
- POJ 3601 Escape from Enemy Territory (二分+BFS+预处理)
- poj 3501 Escape from Enemy Territory 二分+bfs
- hdu 2337 Escape from Enemy Territory (二分枚举+预处理+bfs)
- hdu2337 Escape From Enemy Territory---二分bfs+预处理
- Escape from Enemy Territory(二分+bfs)
- hdu2337Escape from Enemy Territory(预处理+二分+bfs)
- POJ 3501 - Escape from Enemy Territory
- hdu 2337 Escape from Enemy Territory (presolve bfs +bfs+二分)
- hdu 2337 Escape from Enemy Territory (预处理+二分+搜索)
- hdu 2337 Escape from Enemy Territory (预处理+二分+宽搜)
- HDU 2337 Escape from Enemy Territory(BFS+二分优化)
- hdu 2337 Escape from Enemy Territory
- 3094Escape from Enemy Territory——zoj
- poj3501Escape from Enemy Territory||hdu2337Escape from Enemy Territory
- HDU 3533 Escape 预处理+bfs
- 【搜索】 HDU 3533 Escape BFS 预处理
- HDU - 3533----Escape(BFS+预处理)
- 【Ogre编程入门与进阶】第四章 Ogre的设计理念与特色
- 浙江大学研究生复试上机真题——1975:ZOJ
- java发邮件的实现
- oracle 单表备份
- 读取二进制可执行文件资源中包含的各项版本信息
- poj 3501 Escape from Enemy Territory(预处理&二分&bfs)
- linux高效率编程:epoll和多线程
- 危险代码:内存中的Java类和对象为何变得不安全—Part2
- 服务端程序线程运行信息管理器
- 物竞天择、百家争鸣——评OpenStack生态圈
- poj 1135 Domino Effect
- 危险代码:内存中的Java类和对象为何变得不安全—Part1
- 认识jQuery的两个API:attr和data
- ubuntu修改用户名称和计算机名称