poj 3501 Escape from Enemy Territory 预处理+二分+bfs
给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标。 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离。
首先预处理出来地图上的所有点到敌人的最短距离, 然后二分距离, bfs就可以。
tle了好多次, 到网上搜题解, 看到别人是先把敌人的坐标都存到数组里最后在一起预处理, 而我是读一个点处理一个点, 改了以后才ac.......
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const int inf = ;
const double eps = 1e-;
const int mod = 1e9+;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int dis[][], x, y, n, x1, y1, x2, y2, vis[][];
pll point[];
struct node
{
int x, y, step;
node(){}
node(int x, int y, int step):x(x),y(y),step(step){}
}; void bfs() {
queue <node> q;
for(int i = ; i<n; i++) {
q.push(node(point[i].first, point[i].second, ));
dis[point[i].first][point[i].second] = ;
}
while(!q.empty()) {
node tmp = q.front(); q.pop();
for(int i = ; i<; i++) {
int tmpx = tmp.x+dir[i][];
int tmpy = tmp.y+dir[i][];
if(tmpx>=&&tmpx<x&&tmpy>=&&tmpy<y) {
if(dis[tmpx][tmpy]>tmp.step+) { //这里要注意
dis[tmpx][tmpy] = tmp.step+;
q.push(node(tmpx, tmpy, tmp.step+));
}
}
}
}
} int bin(int val) {
if(dis[x1][y1]<val)
return ;
queue <node> q;
mem(vis);
q.push(node(x1, y1, ));
vis[x1][y1] = ;
while(!q.empty()) {
node tmp = q.front(); q.pop();
if(tmp.x == x2 && tmp.y == y2)
return tmp.step;
for(int i = ; i<; i++) {
int tmpx = tmp.x+dir[i][];
int tmpy = tmp.y + dir[i][];
if(tmpx>=&&tmpx<x&&tmpy>=&&tmpy<y&&!vis[tmpx][tmpy]) {
vis[tmpx][tmpy] = ;
if(dis[tmpx][tmpy]>=val) {
q.push(node(tmpx, tmpy, tmp.step+));
}
}
}
}
return ;
} int main()
{
int t, a, b;
cin>>t;
while(t--) {
mem2(dis);
scanf("%d%d%d", &n, &x, &y);
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
for(int i = ; i<n; i++) {
scanf("%d%d", &a, &b);
point[i] = mk(a, b);
}
bfs();
int l = , r = dis[x1][y1], ans, ans1, ans2;
while(l<=r) {
int m = l+r>>;
ans = bin(m);
if(ans>) {
l = m+;
ans1 = m;
ans2 = ans;
} else {
r = m-;
}
}
printf("%d %d\n", r, ans2);
}
}
poj 3501 Escape from Enemy Territory 预处理+二分+bfs的更多相关文章
- poj 3501 Escape from Enemy Territory 二分+bfs
水题,不解释. #include<stdio.h> #include<math.h> #include<cstring> #include<algorithm ...
- hdu 2337 Escape from Enemy Territory
题目大意 给你一张nn*mm矩形地图.上面有些点上有敌营.给你起点和终点, 你找出一条最优路径.满足最优路径上的点离敌营的最近最短距离是所有路径最短的.若有多条找路径最短的一条. 分析 通过二分来确定 ...
- POJ 2723 Get Luffy Out(2-SAT+二分答案)
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8851 Accepted: 3441 Des ...
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- hdu-5652 India and China Origins(二分+bfs判断连通)
题目链接: India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- Poj 3233 Matrix Power Series(矩阵二分快速幂)
题目链接:http://poj.org/problem?id=3233 解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k ...
- loj 1150(spfa预处理+二分+最大匹配)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26864 思路:首先是spfa预处理出每个'G'到'H'的最短距离, ...
- poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16396 Accepted: 750 ...
- POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)
POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...
随机推荐
- D3学习教程
[ D3.js 入门系列 ] 入门总结 | OUR D3.JS http://www.ourd3js.com/wordpress/?p=396
- jquery ajax 参数可以序列化
<form> <input type="text" name="FirstName" value="Bill" /> ...
- Matlab基础知识
一.常用命令:普通的如cd.ls和linux下一样 clc:清除工作窗口中的所有显示内容 clf:清除图形窗口 whos:列出当前工作空间中所有变量,以及它们的名字.尺寸(比如一个矩阵或数组的行列维数 ...
- Arduino周边模块:执行部件(舵机、直流电机、步进电机)
Arduino周边模块:执行部件 Arduino周边模块:执行部件 嵌入式系统的构成 如今已经有各种各样的基于Arduino的嵌入式系统, 比如:智能小车.3D打印机.机器人,甚至还有基于Arduin ...
- Python进阶之匿名函数(关键词lambda)
匿名函数 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB"," ...
- jQuery相关知识
1.jQuery中$符号有何作用? $作为jQuery的别名,如$(document).ready() 即是 jQuery(document).ready() 2.jQuery选择器有哪几种? 基本选 ...
- sqlite3编译与查询
1.sqlite3 http://www.sqlite.org/ 下载 wget http://www.sqlite.org/2014/sqlite-amalgamation-3080403.zip ...
- 汇编写hello world
.global main main: mov ip, sp stmfd sp!, {fp, ip, lr} sub fp, ip, # ldr r0, =he mov lr, pc b printf ...
- MVC自学第四课
处理表单 前面的列子我们已经把表单提交给了HomeControllers类中的RsvpForm动作方法,只是在这个动作方法内,我们并没有做什么具体的逻辑处理,而是直接返回一个 “Thanks”的视图. ...
- MySQLdb callproc 方法
MySQLdb执行存储过程时就要调用 callproc 方法.它返回的是调用时的参数列表. MySQL 中存储过程的定如下: delimiter // create procedure proc_in ...