URAL 1325--Dirt

本文介绍了一道算法题目,旨在寻找从起点到终点的最短路径,同时考虑最少的换鞋次数。通过优先队列实现,确保了换鞋次数最少且路径最短。

题目:

1325. Dirt

Time limit: 0.5 second
Memory limit: 64 MB
— Hello, may I speak to Petrov, please? Hello, my darling… You know, there was a little accident at our home… No, no, don't worry, your computer was not damaged. It is only a bit dirty there now. Well, I should say it's very dirty there and I'm at my Mom's now. Of course, I'll clean it… When? Well, maybe when I have my vacation. What? Well, when we are back from Turkey… the next vacation then. I'll stay at Mother's until then, and you may live here also. No, no, I don't insist, sure, you may stay at home if you wish so. I prepared boots for you, they are at the door. But please, don't make it worse, before you step on a clean floor, change your boots, put on your slippers, they are at the door also. Take them with you when you walk through the dirt. And when you walk on a clean floor, take the boots with you. You see, the dirt is in different places. OK, my love? Thank you!
It is not a great pleasure to change boots each time you get from a clean floor to a dirty floor and vice versa, it's easier to walk extra several meters. So it is necessary to find a way of getting from one place in the apartment to another with the minimal possible number of boots changes; and among these paths the shortest one must be found.
To begin with, it is natural to determine an optimal way of passing the Most Important Route: from the computer to the refrigerator.

Input

The first line of the input contains two integers M and N, which are dimensions of the apartment (in meters), 1 <= N, M <= 500. The two integers in the second line are the coordinates of the computer, and the third line contains the coordinates of the refrigerator. Each of the following M lines contains N symbols; this is the plan of the apartment. On the plan, 1 denotes a clean square, 2 denotes a dirty square, and 0 is either a wall or a square of impassable dirt. It is possible to get from one square to another if they have a common vertex. When you pass from a clean square to a dirty one or vice versa, you must change shoes. The computer and the refrigerator are not on the squares marked with 0.
The upper left square of the plan has coordinates (1, 1).

Output

You should output two integers in one line separated with a space. The first integer is the length of the shortest path (the number of squares on this path including the first and the last squares) with the minimal possible number of boots changes. The second number is the number of boots changes. If it is impossible to get from the computer to the refrigerator, you should output 0 0.

Sample

inputoutput
3 7
1 1
3 7
1200121
1212020
1112021
8 4

题意:从冰箱的地方去电脑的地方找到一条换鞋的次数最少并且在换鞋次数最少的基础上找到一条最短路。换鞋的规则如下:

1. 从干净的地方去脏的地方要换鞋,反之亦然。

2. 从脏的地方去脏的地方不用换鞋,干净去干净的地方也不用。


思路:题目涉及到两个优先级,首先是换鞋次数少的优先,如果换鞋次数一致那么路程短的优先。之前用优先队列,用isit数组标记入队的点不再入队,一直wa,后来发现会出现问题,具体原理现在还没找到,以后再去看看。后来单独用了一个结构体记录到某一点的最短换鞋及最短路,每次更新的时候再判断一下是否换鞋次数更少并且路最短,如果是及时该点之前入队过依然再入队,并且更新,这样更有保证。

实现:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;

const int MAX = 505;
const int L = 10000000000;

int _map[MAX][MAX];
int m, n;
int sx, sy, ex, ey;
int x[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int y[8] = {0, 1, 1, 1, 0, -1, -1, -1};

struct node {
    int dirty;
    int step;
    friend bool operator < (node n1, node n2) {
        if (n1.dirty != n2.dirty)
            return n1.dirty > n2.dirty;
        return n1.step > n2.step;
    }
    int xj, yj;
};

struct path {
    int p;
    int d;
}pa[MAX][MAX];

bool bfs() {
    priority_queue <node>q;
    node start;
    start.xj = sx;
    start.yj = sy;
    start.dirty = 0;
    start.step = 1;
    pa[sx][sy].p = 1;
    pa[sx][sy].d = 0;
    q.push(start);
    while(!q.empty()) {
        node head;
        node tail;
        head = q.top();
        if (head.xj == ex && head.yj == ey)
            return true;
        q.pop();
        for (int i = 0; i < 8; i++) {
            int xi = head.xj + x[i];
            int yi = head.yj + y[i];
            tail.xj = xi;
            tail.yj = yi;
            if (xi >= 1 && xi <= m && yi >= 1 && yi <= n && _map[xi][yi] != 0) {
                tail.step = head.step + 1;
                if (_map[xi][yi] != _map[head.xj][head.yj])
                    tail.dirty = head.dirty + 1;
                else
                    tail.dirty = head.dirty;
                if (pa[tail.xj][tail.yj].d > tail.dirty || pa[tail.xj][tail.yj].d == tail.dirty && pa[tail.xj][tail.yj].p > tail.step) {
                    pa[tail.xj][tail.yj].d = tail.dirty;
                    pa[tail.xj][tail.yj].p = tail.step;
                    q.push(tail);
                }
            }
        }
    }
    return false;
}

int main() {
    while (scanf("%d%d", &m, &n) != EOF) {
        scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
        getchar();
        char l;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                scanf("%c", &l);
                _map[i][j] = l - '0';
                pa[i][j].d = L;
                pa[i][j].p = L;
            }
            getchar();
        }
        if (bfs())
            printf("%d %d\n", pa[ex][ey].p, pa[ex][ey].d);
        else
            printf("0 0\n");
    }
}



内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果分析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各类时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值