uva 11573 - Ocean Currents(bfs+优先队列)

本文介绍了一种基于优先队列的最优路径规划算法,用于解决在特定水流条件下,船只如何以最少的能量消耗从起点到达终点的问题。通过实例输入输出展示了算法的有效性和效率。

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

Problem B: Ocean Currents

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 Specification

The lake is represented as a rectangular grid. The first line of input 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 rscsrdcd, giving the row and column of the starting point and the destination of the trip. Rows and columns are numbered starting with 1.

Sample Input

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

Output Specification

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.

Output for Sample Input

0
2
1

Ondřej Lhoták, Richard Peng

这题写了两种姿势,

第一种是每次到某点的距离更新后,把该点入队。出队时判断,如果出队距离大于当前求得的最短距离,则直接抛弃掉。否则,更新当前最短距离,然后以该状态拓展新的状态。

第二种是每次到某点的距离更新后,把该点入队,同时更新最短距离。出队时判断,如果出队距离大于当前求得的最短距离,则直接抛弃掉。否则,拓展新的状态。

第一种跑了2.95秒(差点Tle了),第二种1.05秒。这是为什么呢?因为第一种中入队的节点会更多!贴上第二种的代码。实际上两种写法只有一点点的差别,但时间上却会差很多!

#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1000 + 5;
const int INF = 1000000000;

int r, c;
char M[maxn][maxn];
int vis[maxn][maxn];

const int cx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int cy[] = {0, 1, 1, 1, 0, -1, -1, -1};

struct Node{
    int x, y, dis;
    Node(int x, int y, int dis){
        this -> x = x;
        this -> y = y;
        this -> dis = dis;
    }
    bool operator < (const Node& e) const{
        return e.dis < dis;
    }
};

bool check(int x, int y){return x > 0 && x <= r && y > 0 && y <= c;}

priority_queue<Node> pq;

int solve(int sx, int sy, int ex, int ey){
    for(int i = 0;i < maxn;i++){
        for(int j = 0;j < maxn;j++){
            vis[i][j] = INF;
        }
    }
    while(!pq.empty()) pq.pop();
    pq.push(Node(sx, sy, 0));
    while(!pq.empty()){
        Node top = pq.top();
        pq.pop();
        int x = top.x;
        int y = top.y;
        int dis = top.dis;
        if(dis > vis[x][y]) continue;
        vis[x][y] = dis;
        if(x == ex && y == ey) return dis;

        for(int i = 0;i < 8;i++){
            int tx = x+cx[i];
            int ty = y+cy[i];
            int dist = dis + (i==(M[x][y]-'0')?0:1);
            if(check(tx, ty) && dist < vis[tx][ty]){
                vis[tx][ty] = dist;
                pq.push(Node(tx, ty, dist));
            }
        }
    }
}

int main(){
    while(scanf("%d%d", &r, &c) != EOF){
        for(int i = 1;i <= r;i++){
            scanf("%s", M[i]+1);
        }
        int q;
        scanf("%d", &q);
        while(q--){
            int sx, sy, ex, ey;
            scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
            int ans = solve(sx, sy, ex, ey);
            printf("%d\n", ans);
        }
    }
    return 0;
}


### Copernicus 海洋洋流数据服务 哥白尼计划(Copernicus Programme)是一个由欧洲委员会和欧洲航天局共同支持的地球观测项目,旨在提供关于地球环境的各种数据和服务。其中,哥白尼海洋环境监测服务 (CMEMS, Copernicus Marine Environment Monitoring Service) 提供了丰富的海洋相关数据产品,包括海表面温度、盐度分布以及洋流速度等。 对于获取海洋洋流的数据或服务,可以通过 CMEMS 的公开接口访问实时和历史数据集。这些数据通常来源于卫星遥感技术与现场测量设备相结合的方式[^2]。以下是具体方法: #### 数据分类 CMEMS 将其提供的数据分为两类: 1. **全球再分析模型数据** 这些数据基于数值模拟生成,覆盖整个地球范围内的长期平均值及其变化趋势。 2. **近实时观测数据** 主要来自浮标阵列、漂流器网络以及其他传感器平台采集到的信息,在短时间内更新并发布给用户群体使用。 #### 获取方式 为了便于开发者和技术人员利用上述资源开发应用程序或者科学研究用途,官方提供了多种途径来下载所需资料: - **在线门户网站**: 用户可以直接登录至 [CMEMS 官网](https://resources.marine.copernicus.eu/) 注册账号后浏览可用选项; - **Web API 接口调用**: 支持通过编程手段自动化请求特定区域/时间段内的定制化成果文件;下面给出一段 Python 脚本作为演示如何借助 `requests` 库实现这一功能的例子: ```python import requests def fetch_cmems_data(username, password, product_id, dataset_id): url = f"https://my.cmems-du.eu/motu-web/Motu" params = { "service": product_id, "product": dataset_id, "date_min": "2023-01-01 00:00:00", "date_max": "2023-01-31 23:59:59", "lon_min": -30, "lon_max": 40, "lat_min": 30, "lat_max": 70, "depth_min": 0.493, "depth_max": 0.4942, "out_dir": "./output/", "auth_mode": "cas", "user": username, "pwd": password } response = requests.get(url, params=params) if response.status_code == 200: print("Data successfully fetched.") else: raise Exception(f"Failed to retrieve data ({response.status_code})") fetch_cmems_data('your_username', 'your_password', 'GLOBAL_ANALYSIS_FORECAST_PHY_001_024', 'global-analysis-forecast-phy-001-024') ``` 此脚本定义了一个函数用于向 CMEMS 发送 GET 请求以拉取指定参数下的物理场预测结果,并保存到本地目录下。注意实际部署前需替换掉占位符变量如用户名密码等内容。 此外值得注意的是,由于涉及到版权保护等原因并非所有类型的文档都能免费无限制地传播共享,请务必仔细阅读相应条款后再行动作决策依据[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值