题目链接:
题目为:
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 (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 ≤
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:
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
这个题目是预处理+二分+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
poj3501Escape from Enemy Territory||hdu2337Escape from Enemy Territory
原文:http://blog.csdn.net/u014303647/article/details/36892619