HDU1072BFS与双重优先判断

这个题其实可以不用优先队列,虽然是以用时最少为首,但是没有打怪啥的,所以最短路径即最短用时。

其次我一直在想只用一个vis来判断走没走过,对于普通的BFS来说还行,但是这个有一个重置时间的装置,一旦时间重置了,我再回到我原来不能准时到达的路线上时,说不定就能准时到达了,所以我再进行搜索时,把地图的每一个一开始搜过的点所剩时间记录了下来,我如果想要重复搜索,那么我所剩余的时间一定得比地图上记录的时间长才可以,并且这样并不会影响出队顺序,即最终的结果,只是这样能适合更多的情况了

bool is_move(int x,int y)
{
    if(x >= 0 && y >= 0 && x < n && y < m && (!vis[x][y] || mp[x][y].limittime < now.limittime - 1) && mp[x][y].x != 0)
    {
        return true;
    }
    return false;
}

#include <iostream>
#include <queue>
#include <memory.h>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int foot[4][2] = {0,1,0,-1,1,0,-1,0};
int n,m,beginx,beginy,endx,endy;
int vis[10][10];
struct node{
    int x;
    int y;
    int nowtime;
    int limittime;
    friend bool operator < (node a,node b)
    {
        return a.nowtime > b.nowtime;
    }
}now,nex,mp[10][10];
priority_queue<node> q;
bool is_move(int x,int y)
{
    if(x >= 0 && y >= 0 && x < n && y < m && (!vis[x][y] || mp[x][y].limittime < now.limittime - 1) && mp[x][y].x != 0)
    {
        return true;
    }
    return false;
}
bool bfs(int x,int y)
{
    now.x = x;
    now.y = y;
    now.limittime = 6;
    mp[x][y].limittime = 6;
    now.nowtime = 0;
    vis[x][y] = 1;
    while(!q.empty())
    {q.pop();}
    q.push(now);
    while(!q.empty())
    {
        now = q.top();
        if(now.x == endx && now.y == endy )
        {
            cout<<now.nowtime<<endl;
            return true;
        }
        q.pop();
        for(int i = 0;i < 4;i++)
        {

            int nextx = now.x + foot[i][0];
            int nexty = now.y + foot[i][1];
            if(is_move(nextx,nexty))
            {
                if(now.limittime - 1 > 0)
                {
                    vis[nextx][nexty] = 1;
                    nex.x = nextx;
                    nex.y = nexty;
                    nex.nowtime = now.nowtime + 1;
                    if(mp[nextx][nexty].x == 4)
                    {
                        nex.limittime = 6;
                    }
                    else
                    {
                        nex.limittime = now.limittime - 1;
                    }
                    mp[nex.x][nex.y].limittime = nex.limittime;
                    //cout<<nextx<<"  "<<nexty<<"  "<<nex.nowtime<<"  "<<nex.limittime<<"  "<<endl;
                    q.push(nex);
                }
            }
        }
    }
    return false;
}
int main()
{

    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
       scanf("%d%d",&n,&m);
       getchar();
       memset(vis,0,sizeof(vis));
        memset(mp,0,sizeof(mp));
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
            {
                scanf("%d",&mp[i][j].x);
                if(mp[i][j].x == 2)
                {
                    beginx = i;beginy = j;
                }
                if(mp[i][j].x == 3)
                {
                    endx = i;endy = j;
                }

            }
            getchar();
        }
        if(!bfs(beginx,beginy))
        {
            cout<<"-1"<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值