HDU 4885 TIANKENG’s travel(几何bfs)

HDU 4885 TIANKENG’s travel

题目链接

题意:给定起点,终点,和一些加油站,要求路过最少的加油站到终点,两点距离必须小于L

思路:先建图,建图时把两点中间没有点,并且距离能到达的建一条长度1的边,那么问题就是如何判断中间没有点,先把所有点按x排序,然后每次找的时候,利用一个set存放当前已有向量,那么下次如果又出现肯定就是不能加入的点,利用set去搞,然后建完图就是简单的广搜了

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cstdlib>
#include <set>
#include <algorithm>
using namespace std;

const int N = 1005;

typedef long long ll;
typedef pair<int, int> pii;
#define MP(a,b) make_pair(a,b)

vector<int> g[N];
int t, n, vis[N];
ll l;

struct Point {
    ll x, y;
    int id;
} p[N];

bool cmp(Point a, Point b) {
    if (a.x != b.x) return a.x < b.x;
    return a.y < b.y;
}

ll gcd(ll a, ll b) {
    if (!b) return a;
    return gcd(b, a % b);
}

bool dis(Point a, Point b) {
    ll dx = a.x - b.x;
    ll dy = a.y - b.y;
    return dx * dx + dy * dy <= l * l;
}

void build() {
    sort(p, p + n + 1, cmp);
    for (int i = 0; i <= n + 1; i++) {
	set<pii> save;
	for (int j = i + 1; j <= n + 1; j++) {
	    if (dis(p[i], p[j])) {
		ll dx = p[i].x - p[j].x;
		ll dy = p[i].y - p[j].y;
		ll d = gcd(dx, dy);
		dx /= d; dy /= d;
		if (save.find(MP(dx, dy)) != save.end()) continue;
		save.insert(MP(dx, dy));
		g[p[i].id].push_back(p[j].id);
		g[p[j].id].push_back(p[i].id);
	    }
	}
    }
}

bool solve() {
    queue<int> Q;
    memset(vis, -1, sizeof(vis));
    Q.push(0);
    vis[0] = 0;
    while (!Q.empty()) {
	int now = Q.front();
	if (now == n + 1) {
	    printf("%d\n", vis[now] - 1);
	    return true;
	}
	Q.pop();
	for (int i = 0; i < g[now].size(); i++) {
	    int v = g[now][i];
	    if (vis[v] != -1) continue;
	    vis[v] = vis[now] + 1;
	    Q.push(v);
	}
    }
    return false;
}

int main() {
    scanf("%d", &t);
    while (t--) {
	scanf("%d%lld", &n, &l);
	scanf("%lld%lld", &p[0].x, &p[0].y);
	p[0].id = 0; p[n + 1].id = n + 1;
	scanf("%lld%lld", &p[n + 1].x, &p[n + 1].y);
	g[0].clear(); g[n + 1].clear();
	for (int i = 1; i <= n; i++) {
	    g[i].clear();
	    scanf("%lld%lld", &p[i].x, &p[i].y);
	    p[i].id = i;
	}
	build();
	if (!solve()) printf("impossible\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值