HDU 2757 优先队列+广搜

本文介绍了一个关于在模拟的湖泊环境中,利用水流方向帮助船只以最小的能量消耗从起点到达终点的问题。通过建立网格模型并采用优先队列算法进行路径规划,实现了有效计算最低能量消耗的目的。
Ocean Currents


Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 904    Accepted Submission(s): 300




Problem Description
For a boat on a large body of water, strong currents can be dangerous, but with careful planning, they can be harnessed to help the boat reach its destination. Your job is to help in that planning.


At each location, the current flows in some direction. The captain can choose to either go with the flow of the current, using no energy, or to move one square in any other direction, at the cost of one energy unit. The boat always moves in one of the following eight directions: north, south, east, west, northeast, northwest, southeast, southwest. The boat cannot leave the boundary of the lake. You are to help him devise a strategy to reach the destination with the minimum energy consumption.
 


Input
The lake is represented as a rectangular grid. The first line of each test chunk contains two integers r and c, the number of rows and columns in the grid. The grid has no more than 1000 rows and no more than 1000 columns. Each of the following r lines contains exactly c characters, each a digit from 0 to 7 inclusive. The character 0 means the current flows north (i.e. up in the grid, in the direction of decreasing row number), 1 means it flows northeast, 2 means east (i.e. in the direction of increasing column number), 3 means southeast, and so on in a clockwise manner:


7 0 1
\|/
6-*-2
/|\
5 4 3


The line after the grid contains a single integer n, the number of trips to be made, which is at most 50. Each of the following n lines describes a trip using four integers rs, cs, rd, cd, giving the row and column of the starting point and the destination of the trip. Rows and columns are numbered starting with 1. 


Please process to the end of the data file.
 


Output
For each trip, output a line containing a single integer, the minimum number of energy units needed to get from the starting point to the destination.
 


Sample Input
5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
 


Sample Output
0
2
1
0
2
1
 


Source
University of Waterloo Local Contest 2009.02.08
 


Recommend
lcy


代码:
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;


int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};


int m,n;
char map[1005][1005];
int res[1005][1005];
int p;
int sr,sc,er,ec;


struct pnt
{
    int r;
    int c;
    int res;
    int num;
    pnt(int x,int y,int z,int ct):r(x),c(y),res(z),num(ct){}
    pnt(){}
    friend bool operator < (pnt p1,pnt p2)
    {
        if(p1.res!=p2.res)
        {
            return p1.res>p2.res;
        }
        else
        {
            return p1.num>p2.num;
        }
    }
};


int main()
{
    while(~scanf("%d%d",&m,&n))
    {
        for(int i=0;i<m;i++)
        {
            scanf("%s",map[i]);
        }
        scanf("%d",&p);
        for(int i=0;i<p;i++)
        {
            int cnt=1;
            memset(res,0x3f,sizeof(res));
            scanf("%d%d%d%d",&sr,&sc,&er,&ec);
            res[sr-1][sc-1]=0;
            priority_queue<pnt>q;
            q.push(pnt(sr-1,sc-1,0,1));
            while(!q.empty())
            {
                pnt pt=q.top();
                if(pt.r==er-1&&pt.c==ec-1)break;
                q.pop();
                int i;
                for(i=0;i<8;i++)
                {
                    int row=pt.r+dir[i][0];
                    int col=pt.c+dir[i][1];
                    if(row<0||col<0||row>=m||col>=n)continue;
                    if(i==map[pt.r][pt.c]-'0')
                    {
                        if(res[row][col]>res[pt.r][pt.c])
                        {
                            res[row][col]=res[pt.r][pt.c];
                            cnt++;
                            q.push(pnt(row,col,pt.res,cnt));
                        }
                    }
                    else
                    {
                        if(res[row][col]>pt.res+1)
                        {
                            res[row][col]=pt.res+1;
                            cnt++;
                            q.push(pnt(row,col,pt.res+1,cnt));
                        }
                    }
                }
            }
            printf("%d\n",res[er-1][ec-1]);
        }
    }
    return 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))。希望这个解法对你有所帮助!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值