Java教程

最小代价路径 记录path

本文主要是介绍最小代价路径 记录path,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

mark

// 已知lnx+x^2 =0 在(0,1)范围内有解,用数值方法求解, 精度0.0001  
// 求二维矩阵从最左上角元素到最右下角元素的最短路径和,只能往右和往下,输出最  
//          短路径和对应的路径 
//         输入:map = [[2,3,1],[1,9,1],[6,4,2]] 
//         输出:2->3->1->1->2 

#include <iostream>
#include <vector>
using namespace std;

// 1
// fx = lnx + x^2
// fx’ = (1 / x) + 2 * x
// x(n+1) = x(n) - f(x)/ f(x)'
#include <math.h>
double newton_solve(double x){
    double x0 = x;
    double x1 = x0 - (log(x0) + pow(x0, 2)) / ((1 / x0) + 2 *x0);
    int max_iter = 1000;
    int iter = 0;
    
    while(iter < max_iter && abs(x0  - x1) > 1e-4){
        x0 = x1;
        x1 = x0 - (log(x0) + pow(x0, 2)) / ((1 / x0) + 2 *x0);
        iter += 1;
    }
    return x1;
}

// 2 
//
// [[2,3,1],
//  [1,9,1],
//  [6,4,2]]
vector<int> minPath(vector<vector<int> > map){
// int minPath(vector<vector<int> > map){
    int rows = map.size();
    int cols = map[0].size();
    vector<vector<int> > dp(rows, vector<int>(cols, 0));
    int res = 0;
    vector<vector<pair<int, int>>> path(rows, vector<pair<int, int>>(cols, {0, 0}));
    //vector<vector<int> > path(rows, vector<int>(cols, 0));
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < cols; j++){
            if(i == 0 && j == 0){
                dp[i][j] = map[i][j];
                path[i][j] = {-1, -1};
            }else if(i == 0){
                dp[i][j] += dp[i][j - 1] + map[i][j];
                path[i][j] = {i, j - 1};
            }else if(j == 0){
                dp[i][j] += dp[i - 1][j] + map[i][j];
                path[i][j] = {i - 1, j};
            }else{
                dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]) + map[i][j];
                if(dp[i - 1][j] < dp[i][j - 1]){
                    path[i][j] = {i - 1, j};
                }else{
                    path[i][j] = {i, j - 1};
                }
            }
        }
    }
//     for(auto a : path){
//         for(auto b : a){
//             cout << b.first << "," << b.second << endl;
//         }
//     }
    
    pair<int, int> tmp = path[rows - 1][cols - 1];
    vector<int> path_all;
    path_all.push_back(map[rows - 1][cols - 1]);
    
    while(tmp.first!=-1 && tmp.second!=-1){
        path_all.push_back(map[tmp.first][tmp.second]);
        // cout << "path_all.size:" << path_all.size() << endl;
        // for(auto a : path_all){
        //     cout << "a:" << a << endl;
        // }
        tmp = path[tmp.first][tmp.second];
        // cout << "tmp:" << tmp.first << "," << tmp.second << endl;
    }
    
    // for(auto a : path_all){
    //     cout << a << endl;
    // }
    // return dp[rows - 1][cols - 1];
    return path_all;
}
int main() {
    vector<vector<int> > nums = {{2, 3, 1}, {1, 9, 1}, {6, 4, 2}};
    // int res = minPath(nums);
    // cout << "res:" << res << endl;
    
    vector<int> res = minPath(nums);
    // cout << "res:" << res << endl;
    // for(auto a : res){
    //     cout << a << endl;
    // }
    for(int i = res.size() - 1; i>=0; i--){
        cout << res[i];
    }
    
    double val = newton_solve(0.5);
    cout << "val:" << val << endl;
}
这篇关于最小代价路径 记录path的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!