hdu 4885 TIANKENG’s travel(bfs)
题目链接:hdu 4885 TIANKENG’s travel
题目大意:给定N,L,表示有N个加油站,每次加满油能够移动距离L,必须走直线,可是能够为斜线。然后给出sx,sy,ex,ey,以及N个加油站的位置,问说最少经过几个加油站,路过不加油也算。
解题思路:一開始以为经过能够不算,所以o(n2)的复杂度建图,然后用bfs求最短距离,结果被FST了。
将点依照x坐标排序,这样在建图是保证当前点为最左点,每次建立一条边的时候,将该边的斜率记录,下次有同样的斜率则不加边,斜率能够用两个整数表示,可是要注意化简成最简。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1005;
struct point {
int id;
ll x, y;
}p[maxn], s, e;
ll L;
int N, d[maxn];
vector<int> g[maxn];
set<pii> vis;
inline ll gcd (ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
inline bool cmp (const point& a, const point& b) {
return a.x < b.x;
}
inline ll dis (ll x, ll y) {
return x * x + y * y;
}
bool search (ll x, ll y) {
ll d = gcd(x, y);
if (d < 0)
d = -d;
x /= d; y /= d;
if (vis.find(make_pair(x, y)) != vis.end())
return true;
vis.insert(make_pair(x, y));
return false;
}
void addEdge (point a, point b) {
ll d = dis(a.x - b.x, a.y - b.y);
if (d <= L && !search(b.x - a.x, b.y - a.y)) {
g[a.id].push_back(b.id);
g[b.id].push_back(a.id);
//printf("%d %d %lld %lld\n", a.id, b.id, d, L * L);
}
}
void init () {
scanf("%d%lld", &N, &L);
scanf("%lld%lld%lld%lld", &p[0].x, &p[0].y, &p[1].x, &p[1].y);
p[0].id = 0;
p[1].id = 1;
N += 2;
L = L * L;
for (int i = 0; i < N; i++)
g[i].clear();
for (int i = 2; i < N; i++) {
scanf("%lld%lld", &p[i].x, &p[i].y);
p[i].id = i;
}
sort(p, p + N, cmp);
for (int i = 0; i < N; i++) {
vis.clear();
for (int j = i + 1; j < N; j++)
addEdge(p[i], p[j]);
}
}
void bfs () {
queue<int> que;
que.push(0);
memset(d, -1, sizeof(d));
d[0] = 0;
while (!que.empty()) {
int u = que.front();
que.pop();
if (u == 1) {
printf("%d\n", d[u]-1);
return;
}
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (d[v] == -1) {
d[v] = d[u] + 1;
que.push(v);
}
}
}
printf("impossible\n");
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
bfs();
}
return 0;
}
hdu 4885 TIANKENG’s travel(bfs)的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- 【HDU】4418 Time travel
http://acm.hdu.edu.cn/showproblem.php?pid=4418 题意:一个0-n-1的坐标轴,给出起点X.终点Y,和初始方向D(0表示从左向右.1表示从右向左,-1表示起 ...
- HDU 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
- HDU 4435 charge-station () bfs图论问题
E - charge-station Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU(1175),连连看,BFS
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...
- hdu - 1180 诡异的楼梯 (bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
随机推荐
- BZOJ 2463 谁能赢呢? (博弈论)
题解:简单博弈论 #include <cstdio> int main(){ int n; while(scanf("%d",&n),n!=0) if (n&a ...
- p2.js物理引擎学习
P2简介 P2是一款基于Javascript编写的HTML5 2D物理引擎,和Box2D.Nape等2D物理引擎一样,P2集成了各种复杂的物理公式和算法,可以帮助我们轻松的实现碰撞.反弹等物理现象的模 ...
- 实现一个简单的http请求工具类
OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...
- 重温 Win32 API ----- 截屏指定窗体并打印
朋友说在一个VC++6.0开发的项目中要增加打印窗体的功能,让帮忙写个代码供其调用. 这么老的IDE当然不想碰了,并且也不喜欢MFC笨拙不清晰的封装.所以决定採用纯Win32 API,然后用C++类简 ...
- 关闭Outlook的时候使之最小化
Outlook很搓的一点就是只有按‘最小化’按钮的时候才会最小化到托盘,而按‘关闭’按钮Outlook直接被关闭退出.然后经常发现没邮件,结果是因为客户端关掉了. 下面通过插件方式实现关闭后最小化到托 ...
- 解決 centos中-bash: vim: command not found
用centos 的主机的時候, 用 vim 时出现 -bash: vim: command not found. 只能使用 vi. 那么如何安裝 vim 呢? 输入 rpm -qa|grep vim ...
- [Just a feeling]
The possibility of enhancing one's knowledge is limitless. Graduation only marks a stage of one's ed ...
- projecteuler之58题Spiral primes
package com.android; public class SpiralPrimes { public static void main(String args[]) { long numPr ...
- [LeetCode]题解(python):026-Remove Duplicates from Sorted Array
题目来源: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 题意分析: 给定一个排好序的数组,去除重复的数,返回新 ...
- 字符串-06. IP地址转换(20)
#include<iostream> #include<string> #include<cmath> using namespace std; int main( ...