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 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的更多相关文章
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 刷题64. Minimum Path Sum
一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...
- 【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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分
A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- ASP.NET Web API与Rest web api(一)
本文档内容大部分来源于:http://www.cnblogs.com/madyina/p/3381256.html HTTP is not just for serving up web pages. ...
- hdu1078 记忆化搜索(DP+DFS)
题意:一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子上的数必须比上一个走的格子的数大,问最大的路径和. 我一开始的思路是,或许是普通的最大路径和,只 ...
- MAC 如何修改PATH
http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac
- 解决Ubuntu下vbox的(rc=-1908)
在Ubuntu下用虚拟机VBOX的时候总是遇到 Kernel driver not installed (rc=-1908) The VirtualBox Linux kernel driver (v ...
- caffe: fuck compile error again : error: a value of type "const float *" cannot be used to initialize an entity of type "float *"
wangxiao@wangxiao-GTX980:~/Downloads/caffe-master$ make -j8find: `wangxiao/bvlc_alexnet/spl': No suc ...
- java linux book
calvin1978.blogcn.com/articles/javabookshelf.html
- JS兼容IE浏览器的方法
背景 系统需要兼容蛋疼的IE6... 解决方案 *{ 兼容IE6-8 }* <!--[if lt IE 9]> <script src="@{'/public/mng/ja ...
- 木匠ing[索引]
古人有云,一个不会写代码的木匠不会是个好厨子. 为了响应这个号召,开始我的木工之路. 首先介绍一个网站,www.zuojiaju.com 木工爱好者 ,里面有大量的关于木匠的帖子,感谢一下. 以前只是 ...
- BNUOJ 1006 Primary Arithmetic
Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你 ...