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. (转) IDirectSoundBuffer::SetVolume的参数与音量分贝的函数关系

    假如将播放器的控制音量切割成0-100的话,由于IDirectSoundBuffer::SetVolume(LONG lVolume)中参数的输入值是[-10000,0] MySetVolume( D ...

  2. Type cvc-complex-type.2.4.c: The matching wildcard is strict...

    这个问题困扰了我两次,分别说一下原因:1. 如网上一些网友所言,是在配置Spring的标签库的时候有拼写错误或者遗漏.下面贴一个标准3.0的吧: <?xml version="1.0& ...

  3. Objective-C语法之字符串NSString去掉前后空格或回车符(可以是NSCharacterSet类型的其它字符)

    main.m #import <Foundation/Foundation.h> #import "NSString+Trim.h" int main(int argc ...

  4. VS2013启动浏览器链接(BrowserLink),导致页面脚本错误和页面加载变慢

    页面脚本出错场景:

  5. 读取文件中"-1"的问题,要用int类型。

    char类型 栈空间,遇到-1结束循环. int类型则遇到-1不会结束循环. 4个字节的int. 每个汉字由两个负数组成. 映射过去后,-68变成了:256-68=188. int类型读过去变成了正数 ...

  6. iis重启的几种方法

    1. 通过“IIS管理器”重启在IIS服务器管理控制树中展开IIS节点,选择需要重新启动IIS服务的计算机,接着单击鼠标右键,选择“所有任务”->“重新启动IIS”. 2.通过“控制面板”-&g ...

  7. phoenix客户端连接hbase数据库报错:Traceback (most recent call last): File "bin/sqlline.py", line 27, in <module> import argparse ImportError: No module named argparse

    环境描述: 操作系统版本:CentOS release 6.5 (Final) phoenix版本:phoenix-4.10.0 hbase版本:hbase-1.2.6 现象描述: 通过phoenix ...

  8. scrapy 元素的相对xpath

  9. [原]IOS 后台发送邮件

    skpsmtpmessage 是ios第三方后台发送邮件库 https://github.com/jetseven/skpsmtpmessage.git -(void)statrUpLoad:(id) ...

  10. C# base和this的用法

    /** this关键字* this关键字引用类的当前实例* 注意:静态成员方法中不能使用this关键字,this关键字只能在实例构造函数.实例方法或实例访问器中使用*/ /** base关键字* ba ...