Escape from Enemy Territory(二分+bfs)
2016-10-17 22:26
344 查看
Escape from Enemy Territory
Time Limit: 5000msMemory Limit: 65536KB
64-bit integer IO format: %I64d
Java class name: Main
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
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
#include <cstdio> #include <iostream> #include <string.h> #include <algorithm> #include <queue> using namespace std; int n, m, n_enemys; int stx, sty, endx, endy; struct Node{ int x; int y; int len; Node(int x = 0, int y = 0, int len = 0){ this ->x = x; this ->y = y; this ->len = len; } }; queue<Node> q; bool vis[1010][1010]; int length[1010][1010]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, -1, 0, 1}; void init_length() { while(!q.empty()){ Node a = q.front(); q.pop(); int x = a.x; int y = a.y; for(int i = 0; i < 4; i++){ int x2 = x + dx[i]; int y2 = y + dy[i]; if(length[x2][y2] == -1){ length[x2][y2] = a.len + 1; Node b(x2, y2, a.len+1); q.push(b); } } } } void init_vis(){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ vis[i][j]=false; } } } int judge(int len)//给定一个 距离敌人的最小距离,判断这个距离能不能到达终点 不能返回 -1 能的话返回长度 { if(length[stx][sty] < len || length[endx][endy] < len) return -1; init_vis(); while(!q.empty()) q.pop(); if(stx == endx && sty == endy) return 0; Node a(stx, sty, 0); vis[stx][sty] = true; q.push(a); while(!q.empty()){ Node top = q.front(); q.pop(); for(int i = 0; i < 4; i++){ int x = top.x + dx[i]; int y = top.y + dy[i]; if(x == endx && y == endy) return top.len + 1; if(x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y]){ vis[x][y] = true; if(length[x][y] >= len){ Node tmp(x, y, top.len + 1); q.push(tmp); } } } } return -1; } void output() { int left = 1; int right = n + m; int mid; int dis; while(left <= right){ mid = (left + right) >> 1; dis = judge(mid); if(dis == -1){ right = mid - 1; } else{ left = mid + 1; } } cout << right << " " << judge(right) << endl; } int main() { int T; cin >> T; while(T--){ cin >> n_enemys >> n >> m; cin >> stx >> sty >> endx >> endy; stx++, sty++, endx++, endy++; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ vis[i][j]=false; length[i][j]=-1; } } while(!q.empty()) q.pop(); for(int i = 1; i <= n_enemys; i++){ int x, y; cin >> x >> y; length[x+1][y+1] = 0; vis[x+1][y+1] = true; Node a(x+1, y+1, 0); q.push(a); } init_length(); output(); } return 0; }
相关文章推荐
- poj 3501 Escape from Enemy Territory 二分+bfs
- hdu 2337 Escape from Enemy Territory (二分枚举+预处理+bfs)
- poj 3501 Escape from Enemy Territory 二分+bfs
- hdu2337 Escape From Enemy Territory---二分bfs+预处理
- hdu 2337 Escape from Enemy Territory (presolve bfs +bfs+二分)
- HDU 2337 Escape from Enemy Territory(BFS+二分优化)
- POJ 3601 Escape from Enemy Territory (二分+BFS+预处理)
- hdu 2337 Escape from Enemy Territory (预处理+二分+宽搜)
- POJ 3501 - Escape from Enemy Territory
- hdu 2337 Escape from Enemy Territory
- HOJ 2979 Escape from Pyramids --------BFS求最小的步数
- [BZOJbegin][NOIP十连测热身赛b]escape(二分+bfs)
- 【bzoj 入门OJ】[NOIP 热身赛]Problem B: escape(二分答案+bfs)
- hdu1813 Escape from Tetris (IDA*)
- 【POJ3525】Most Distant Point from the Sea-半平面交+二分答案
- Learning from the enemy
- 【POJ】2253 Frogger 二分+bfs
- USACO Jan08 (COGS 174) 架设电话线 二分答案,缩点,BFS判断可行性
- Gulliver's Travels——4、Gulliver escapes from Lilliput
- Gulliver's Travels——9、Gulliver escapes from Brobdingnag