POJ 3501 - Escape from Enemy Territory
来源:互联网 发布:caffe imagenet 编辑:程序博客网 时间:2024/05/29 19:46
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 andxr,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
题意:给一个地图,上面有的点是敌人的基地,给一个起始点和目标点,要求找出一条路,这条路上的每个点距离最近的敌人基地为limitDis,路径长度为pathLen,要求limitDis最大的前提下,pathLen最小。输出为limitDis,pathLen
先预处理出每个点最近的二分limitDis,然后bfs,看能不能到达目的地,并得到pathLen。
遇到的问题:
一,是在bfs的时候,continue写在了Q.pop()前面,导致了TLE。
二,二分没有写好
#include <stdio.h>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define S64I1(a) scanf(iform1, &(a))#define P64I1(a) printf(oform1, (a))#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int moveX[4] = {-1, 1, 0, 0};const int moveY[4] = {0, 0, -1, 1};const int maxn = 10000 + 20;const int maxXY = 1000 + 20;int enemy[maxn][2];int vis[maxXY][maxXY];int dis[maxXY][maxXY];int pathDis[maxXY][maxXY];int n, X, Y;int startX, startY, endX, endY;int maxEnemyDis;queue<int> Q;bool isOk(int x, int y) { return x >= 0 && x < X && y >= 0 && y < Y;}void init() { memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(dis)); while(!Q.empty()) Q.pop(); for(int i=0; i<n; i++) { Q.push(enemy[i][1] * X + enemy[i][0]); vis[enemy[i][0]][enemy[i][1]] = 1; } while(!Q.empty()) { int x = Q.front() % X; int y = Q.front() / X; Q.pop(); maxEnemyDis = max(maxEnemyDis, dis[x][y]); for(int i=0; i<4; i++) { int nx = x + moveX[i]; int ny = y + moveY[i]; if(isOk(nx, ny) && !vis[nx][ny]) { dis[nx][ny] = dis[x][y] + 1; Q.push(ny * X + nx); vis[nx][ny] = 1; } } }}bool bfs(int limtDis, int & minDis) { memset(vis, 0, sizeof(vis)); while(!Q.empty()) Q.pop(); Q.push(startX * Y + startY); vis[startX][startY] = 1; pathDis[startX][startY] = 0; int ttt = 0; while(!Q.empty()) { //if(++ttt > X * Y * 2) vis[-100][-100] = 23333; int x = Q.front() / Y; int y = Q.front() % Y; Q.pop(); // 写到下面去了 导致TLE if(dis[x][y] < limtDis) continue; if(x == endX && y == endY) { minDis = pathDis[x][y]; return true; } for(int i=0; i<4; i++) { int nx = x + moveX[i]; int ny = y + moveY[i]; if(isOk(nx, ny) && !vis[nx][ny] && dis[nx][ny] >= limtDis) { vis[nx][ny] = 1; //if(++ttt > X * Y * 2) vis[-100][-100] = 23333; pathDis[nx][ny] = pathDis[x][y] + 1; Q.push(nx * Y + ny); } } } return false;}int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d%d", &n, &X, &Y); scanf("%d%d%d%d", &startX, &startY, &endX, &endY); for(int i=0; i<n; i++) { scanf("%d%d", &enemy[i][0], &enemy[i][1]); } maxEnemyDis = 0; init(); int L = 0; int R = maxEnemyDis; // 这里... int limtDis = 0; int minDis = -1; while(L <= R) { int mid = (R + L) / 2; if(bfs(mid, minDis)) { if(mid > limtDis) { limtDis = mid; } L = mid + 1; } else { R = mid - 1; } } printf("%d %d\n", limtDis, minDis); } return 0;}
- POJ 3501 - Escape from Enemy Territory
- poj 3501 Escape from Enemy Territory 二分+bfs
- poj 3501 Escape from Enemy Territory(预处理&二分&bfs)
- POJ 3601 Escape from Enemy Territory (二分+BFS+预处理)
- hdu 2337 Escape from Enemy Territory
- Escape from Enemy Territory(二分+bfs)
- hdu 2337 Escape from Enemy Territory (预处理+二分+搜索)
- hdu 2337 Escape from Enemy Territory (presolve bfs +bfs+二分)
- hdu 2337 Escape from Enemy Territory (二分枚举+预处理+bfs)
- hdu2337 Escape From Enemy Territory---二分bfs+预处理
- hdu 2337 Escape from Enemy Territory (预处理+二分+宽搜)
- 3094Escape from Enemy Territory——zoj
- HDU 2337 Escape from Enemy Territory(BFS+二分优化)
- poj3501Escape from Enemy Territory||hdu2337Escape from Enemy Territory
- hdu2337Escape from Enemy Territory(预处理+二分+bfs)
- Learning from the enemy
- poj - 3855 - Blast the Enemy!
- hdu1813 Escape from Tetris
- leetcode:3Sum Closest
- js/jquery实现免费获取手机验证码倒计时效果
- 怎么混合显示图片和文本
- 九度online:题目1004:Median
- Android Fragment详解(三): 实现Fragment的界面
- POJ 3501 - Escape from Enemy Territory
- Hibernate注解配置一对多,以及按照列排序
- 【轻松玩蓝牙】序列之5:你中奖了么?
- myeclipse 中 TODO 注释的用法
- storm简介
- 利用winNet API实现相对url转为绝对url
- 【仿微信即时聊天】
- Linux内核启动过程总结(图文)
- HSIC Interface Description