Min Cost Path

 

Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function that returns cost of minimum cost path to reach (m, n) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell. Total cost of a path to reach (m, n) is sum of all the costs on that path (including both source and destination). You can only traverse down, right and diagonally lower cells from a given cell, i.e., from a given cell (i, j), cells (i+1, j), (i, j+1) and (i+1, j+1) can be traversed. You may assume that all costs are positive integers.

For example, in the following figure, what is the minimum cost path to (2, 2)?

The path with minimum cost is highlighted in the following figure. The path is (0, 0) –> (0, 1) –> (1, 2) –> (2, 2). The cost of the path is 8 (1 + 2 + 2 + 3).

http://www.geeksforgeeks.org/dynamic-programming-set-6-min-cost-path/

下面是递归法和动态规划法的C++程序:

int minCostPath(vector<vector<int>> &cost, int m, int n)
{
if (n < 0 || m < 0) return INT_MAX;
else if (m == 0 && n == 0) return cost[m][n];
else return cost[m][n] + min(minCostPath(cost, m-1, n-1),
min(minCostPath(cost, m-1, n), minCostPath(cost, m, n-1)));
} int minCostPathDP(vector<vector<int> > &cost)
{
int row = cost.size();
if (row < 1) return 0;
int col = cost[0].size(); vector<vector<int> > ta(2, vector<int>(col));
bool flag = false;
ta[!flag][0] = cost[0][0];
for (int i = 1; i < col; i++)
{
ta[!flag][i] = ta[!flag][i-1] + cost[0][i];
} for (int i = 1; i < row; i++)
{
ta[flag][0] = ta[!flag][0] + cost[i][0];
for (int j = 1; j < col; j++)
{
ta[flag][j] = min(min(ta[!flag][j],ta[flag][j-1]),
ta[!flag][j-1]) + cost[i][j];
}
flag = !flag;
}
return ta[!flag][col-1];
}

Geeks面试题:Min Cost Path的更多相关文章

  1. LeetCode算法题-Min Cost Climbing Stairs(Java实现)

    这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...

  2. [Swift]LeetCode746. 使用最小花费爬楼梯 | Min Cost Climbing Stairs

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  3. min cost max flow算法示例

    问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...

  4. Min Cost Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...

  5. Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

    题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...

  6. 746. Min Cost Climbing Stairs@python

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  7. LN : leetcode 746 Min Cost Climbing Stairs

    lc 746 Min Cost Climbing Stairs 746 Min Cost Climbing Stairs On a staircase, the i-th step has some ...

  8. LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11

    746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...

  9. 【Leetcode_easy】746. Min Cost Climbing Stairs

    problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...

随机推荐

  1. 高校区LAN局域网校内网组建实践经验

    项目描述: ●校区计算机网络组建与管理和维护. 主要内容: 1.电脑故障诊断与排除与维护. 2.修复局域网内的故障电脑. 3.局域网架设虚拟系统. 4.局域网升级. 5.局域网基础架构. 6.电脑系统 ...

  2. Lua------------------改善Unity编辑器对Lua文件的支持

    原创 2017年03月10日 18:44:22 标签: Unity / lua / 编辑器 952 当前版本的Unity(截至Unity5.5.x)中TextAsset类不支持后缀为lua的文件,将l ...

  3. Linux SD卡驱动开发(四) —— SD 控制器之真正的硬件操作

    前面对SD卡控制器有了一个主要的介绍.事实上SD控制器层更过的意义是为core层提供一种操作SD卡硬件的一种方法.当然不同的控制器对硬件控制的方法不尽同样,可是他们终于都能像core层提交一个统一的封 ...

  4. [转]Linux内核源码详解--iostat

    Linux内核源码详解——命令篇之iostat 转自:http://www.cnblogs.com/york-hust/p/4846497.html 本文主要分析了Linux的iostat命令的源码, ...

  5. Android学习笔记——保存数据到SQL数据库中(Saving Data in SQL Databases)

    知识点: 1.使用SQL Helper创建数据库 2.数据的增删查改(PRDU:Put.Read.Delete.Update) 背景知识: 上篇文章学习了保存文件,今天学习的是保存数据到SQL数据库中 ...

  6. koa-route模块

    const Koa = require('koa'); const route = require('koa-route'); const app = new Koa(); const about = ...

  7. todo:区块链????????

    2018年这个春节,人人都在讲btc.ico.区块链. 恶补了几天区块链的知识和文章,一总结就是:一头雾水,没有哪个vc.创业者.技术人员 讲明白区块链到底是什么,技术上如何实现,就是哪些研读过中本聪 ...

  8. SQLServer查看存储过程的方法

    使用 sp_helptext 查看存储过程的定义 在对象资源管理器中,连接到 数据库引擎实例,再展开该实例. 在工具栏上,单击“新建查询”. 在查询窗口中,输入下列语句.更改数据库名称和存储过程名称以 ...

  9. iis7错误提示An error occurred on the server when processing the URL...

    win7下面运行ASP程序总是出错,原来是站点配置的问题... 问题一:MS Jet引擎改变了临时目录的位置,但是又没有对临时目录的存取权限,导致数据库使用失败(因为sql问题,后改用access数据 ...

  10. MTK 预置apk

    一.如何将带源码的APK预置进系统? 1)     在 packages/apps 下面以需要预置的 APK的 名字创建一个新文件夹,以预置一个名为Test的APK 为例 2)     将 Test ...