Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

===============

思路:经典DP动态规划入门问题,

定义状态转移方程f[i][j] = min(f[i-1][j],f[i][j-1])+grid[i][j]

初始状态函数,二维数组f[][]的第一行和第一列

========

code 如下:

class Solution{
public:
int minPathSum(vector<vector<int> > &grid){
if(grid.size()==) return ;
const int m = grid.size();
const int n = grid[].size(); int f[m][n];
f[][] = grid[][];
for(int i = ;i<m;i++){
//初始化状态方程第一lie
f[i][] = f[i-][]+grid[i][];
}
for(int i = ;i<n;i++){
//初始化状态方程第一hang
f[][i] = f[][i-]+grid[][i];
} //运用状态方程求解
for(int i = ;i<m;i++){
for(int j = ;j<n;j++){
if(f[i-][j]<f[i][j-]){
cout<<i-<<"-"<<j<<endl;
}else{
cout<<i<<"-"<<j-<<endl;
}
f[i][j] = min(f[i-][j],f[i][j-])+grid[i][j];
}
} return f[m-][n-];
}
};

64. Minimum Path Sum的更多相关文章

  1. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  2. 刷题64. Minimum Path Sum

    一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...

  3. 【LeetCode】64. Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  4. [LeetCode] 64. Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. LeetCode 64 Minimum Path Sum

    Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  6. C#解leetcode 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. LeetCode OJ 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. LeetCode 64. Minimum Path Sum(最小和的路径)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. 64. Minimum Path Sum(中等, 又做出一个DP题, 你们非问我开不开心,当然开心喽!^^)

    Given an m x n grid filled with nonnegative numbers, find a path from top left to bottom right which ...

随机推荐

  1. telnet命令使用示例

    导读 telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是Internet远程登录服务的标准协议和主要方式.它 ...

  2. jQuery 事件 - load() 方法

    例子:$("img").load(function(){ $("div").text("Image loaded"); }); 定义和用法 ...

  3. POJ 3461 裸的KMP

    直接贴代码吧 #include<cstdio> #include<cstring> ],T[]; ]; int n,m; void getfail() { f[] = ; f[ ...

  4. HDU-4003 Find Metal Mineral (树形DP+分组背包)

    题目大意:用m个机器人去遍历有n个节点的有根树,边权代表一个机器人通过这条边的代价,求最小代价. 题目分析:定义状态dp(root,k)表示最终遍历完成后以root为根节点的子树中有k个机器人时产生的 ...

  5. hdu3416 最短路+最大流

    题意:有 n 点 m 边,有出发点 A 到达点 B ,只允许走原图中的最短路,但每条边只允许被走一次,问最多能找出多少条边不重复的最短路 一开始做到的时候瞎做了一发最短路,WA了之后也知道显然不对,就 ...

  6. Visual Studio 2012 update3 安装后的问题及解决

    安装之后可能遇到的问题: 安装完时,打开Help Viewer时,出现了一个错误提示:”a content file required by the help viewer is missing or ...

  7. 在线音乐API的研究 (Part 2.1)

    本文转载于:http://www.cnblogs.com/osmondy/p/LyricApi.html 最近,在优化一个自己写的音乐播放器.主要目的是回顾.归纳,并希望能够写出一个属于自己的comm ...

  8. mysql optimization

    EXPLAIN 命令详解 http://www.cnblogs.com/gomysql/p/3720123.html http://www.cnblogs.com/Aiapple/p/5697229. ...

  9. 了解oracle数据库的情况

    1.了解你的数据库版本号 2.是否配置了DataGuard? SQL> select protection_mode, protection_level, remote_archive,data ...

  10. 关于CPU Cache -- 程序猿需要知道的那些事

    本文将介绍一些作为程序猿或者IT从业者应该知道的CPU Cache相关的知识 文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) 本文原文地址:http://ce ...