HDU 3690 bfs+哈希

本文介绍了一种寻找从起点到终点最短路径的方法,并通过具体的C++代码实现了该算法。文章考虑了各种限制条件,包括最大移动速度和有效路径范围等,确保找到的路径既高效又符合实际场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**可以想象任意一个最短步数形成的路径任意交换两个选择的向量形成的路径是不一样的,那么一定能找到在路径中离
l:(sx,sy)-> (tx,ty)直线最近的路径,所以只要找到这个路径即可,可以将所有背离起点终点和离 l 距离超过最大向量的点不入队,这样搜到
的点的个数极限就是(5000 + 5000)* sqrt(2)* 10 * 2 * sqrt(2) = 400000,然后写个hash记录即可:**

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long LL;
const int prime = 999997;
const int maxn = 17;
const int maxm = 400007;
int kase, fx, fy, tx, ty, n, idx;
int speed[17][2], head[prime], Maxs;
int a, b, c;

struct Node1{
    int x, y, step;
    Node1(){}
    Node1(int x, int y, int step) : x(x),y(y),step(step) {}
};

struct Node2{
    int x, y, Next;
}edge[maxm];

int Hash(int x, int y) {
    return (((x<<15)^y)%prime+prime)%prime;
}

bool addedge(int key, int x, int y) {
    for(int i = head[key]; i != -1; i = edge[i].Next) {
        if(edge[i].x == x && edge[i].y == y) return false;
    }
    edge[idx].x = x;
    edge[idx].y = y;
    edge[idx].Next = head[key];
    head[key] = idx++;
    return true;
}

bool isValid(int x, int y) {
    if(sqr(x-fx)+sqr(y-fy) <= Maxs) return true; //起点画圆
    if(sqr(x-tx)+sqr(y-ty) <= Maxs) return true; //终点画圆
    if((tx-fx)*(x-fx)+(ty-fy)*(y-fy) < 0) return false; //逆起点方向
    if((fx-tx)*(x-tx)+(fy-ty)*(y-ty) < 0) return false; //逆终点方向
    if(sqr((LL)a*x+b*y+c) <= (LL)Maxs*(sqr(a)+sqr(b)))return true; //点到对角线距离小于Maxs
    return false;
}

void bfs() {
    queue<Node1> Q;
    Node1 tmp(fx,fy,0);
    Q.push(tmp);
    addedge(Hash(fx,fy),fx,fy);
    while(!Q.empty()) {
        Node1 cur = Q.front(); Q.pop();
        if(cur.x == tx && cur.y == ty) {
            printf("%d\n", cur.step);
            return ;
        }
        for(int i = 0; i < n; ++i) {
            int xx = cur.x + speed[i][0];
            int yy = cur.y + speed[i][1];
            if(isValid(xx, yy) && addedge(Hash(xx,yy),xx,yy)) {
                tmp = Node1(xx, yy, cur.step+1);
                Q.push(tmp);
            }
        }
    }
    printf("IMPOSSIBLE\n");
}

int main() {
    scanf("%d", &kase);
    while(kase--) {
        idx = 0; Maxs = 0;
        memset(head, -1, sizeof(head));
        scanf("%d%d%d%d", &fx, &fy, &tx, &ty);
        scanf("%d", &n);
        for(int i = 0; i < n; ++i) {
            scanf("%d%d", &speed[i][0], &speed[i][1]);
            Maxs = max(Maxs, sqr(speed[i][0])+sqr(speed[i][1]));
        }
        a = ty-fy; b = fx-tx; c = fy*tx-fx*ty;
        bfs();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JalexDooo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值