Fortune left France title 7 basis class8- two-dimensional array and a minimum path

1. Title: minimum path and the two-dimensional array

Give you a two-dimensional array, each two-dimensional array of numbers are positive, the requirements from the upper left
corner went to the bottom right corner of each step can only be to the right or down. Digital tired along the way through to
add up. And a minimum return path.
Here Insert Picture Description

2. Violence recursion:

(1) Analysis

Can be solved with the idea because recursive
① looking termination conditions
if the lower right corner position has been reached: the path is the value and the lowest angular position
if (can only go right) in the last line: The next position (right position) to the most and the lower right corner of the shortest path plus the value of the current position
value of the next position (lower position) to the lower right corner and a shortest path plus the current position: If (only go down) in the last one

② the cost of each step
if right away: the price is right side position to the lower right corner of the shortest path and
, if go down: the cost is lower position the shortest path to the lower right corner and
above both the minimum value, plus the current value of the position

(2) the core code

int walk (const vector<vector<int> >& v,int i,int j)//从i、j出发 
{
	if(i == v.size() - 1 && j == v[0].size() - 1) //已经到达最右下角位置
		return v[i][j];
	if(i == v.size() - 1) 						  //在最后一行 
		return v[i][j] + walk(v,i,j + 1);		  
	if(j == v[0].size() - 1) 					  //在最后一列 
		return v[i][j] + walk(v,i + 1,j);
	int right =  walk(v,i,j + 1);//右边位置到右下角位置的最小路径 
	int down =  walk(v,i + 1,j); //下边位置到右下角位置的最小路径 
	return v[i][j] + min(right,down);
}

3. Dynamic Programming:

There are a lot of repeat violence recursive calculation, consider the shortest path to the Cache section, when needed directly

(1) Analysis

This question duplicate state, and each step has nothing to do with the shortest path problem no after-effect, you can change the dynamic programming

Here Insert Picture Description
Establish i, j two-dimensional array dp
① identify needs to point ※ dp [0] [0],
② identify reference points, the last position is dp [row] [col] = v [row] [col] ; in addition, the last row and the last column may be calculated shortest path below
Here Insert Picture Description
③ analysis each step: the current position of the minimum is the minimum path right side position and the lower position of the minimum path
Here Insert Picture Description

(2) the core code

int walk2 (const vector<vector<int> >& v)
{
	int row = v.size() - 1;
	int col = v[0].size() - 1;
	vector<vector<int> > dp(v.size(),vector<int>(v[0].size(),0));	//初始化dp数组 
	dp[row][col] = v[row][col];		//基础点 
	//最后一行 
	for(int j = col - 1; j >= 0; --j)
	{
		dp[row][j] = dp[row][j + 1] +  v[row][j];
	}  
	//最后一列 
	for(int i = row - 1; i >= 0; --i)
	{
		dp[i][col] = dp[i + 1][col] + v[i][col];
	} 
	//每一步 
	for(int i = row - 1; i >= 0; --i)
		for (int j = col - 1; j >= 0; --j)
		{
			dp[i][j] = min(dp[i][j + 1],dp[i + 1][j])+ v[i][j];
		}
	return dp[0][0];		//需求位置
}

4. The complete code

#include<iostream>
#include<vector>
//#include<math.h>
using namespace std;

 //递归 
int walk (const vector<vector<int> >& v,int i,int j)//从i、j出发 
{
	if(i == v.size() - 1 && j == v[0].size() - 1) //已经到达最右下角位置
		return v[i][j];
	if(i == v.size() - 1) 						  //在最后一行 
		return v[i][j] + walk(v,i,j + 1);		  
	if(j == v[0].size() - 1) 					  //在最后一列 
		return v[i][j] + walk(v,i + 1,j);
	int right =  walk(v,i,j + 1);//右边位置到右下角位置的最小路径 
	int down =  walk(v,i + 1,j); //下边位置到右下角位置的最小路径 
	return v[i][j] + min(right,down);
}

//无后效性问题,可以改为动态规划
int walk2 (const vector<vector<int> >& v)
{
	int row = v.size() - 1;
	int col = v[0].size() - 1;
	vector<vector<int> > dp(v.size(),vector<int>(v[0].size(),0));		//初始化dp数组 
	dp[row][col] = v[row][col];		//基础点 
	//最后一行 
	for(int j = col - 1; j >= 0; --j)
	{
		dp[row][j] = dp[row][j + 1] +  v[row][j];
	}  
	//最后一列 
	for(int i = row - 1; i >= 0; --i)
	{
		dp[i][col] = dp[i + 1][col] + v[i][col];
	} 
	//每一步 
	for(int i = row - 1; i >= 0; --i)
		for (int j = col - 1; j >= 0; --j)
		{
			dp[i][j] = min(dp[i][j + 1],dp[i + 1][j])+ v[i][j];
		}
	return dp[0][0];			//需求位置
}

int main()
{

	//二维初始化vector<vector<int> > v(4,vector<int>(4,0));
	//cout<<v.size()<<" " <<v[0].size();
	vector<vector<int> > v = {{ 1, 3, 5, 9 }, 
							  { 8, 1, 3, 5 }, 
							  { 5, 0, 6, 1 }, 
							  { 8, 8, 4, 0 } };
	
//	cout<<walk(v,0,0);
	cout<<walk2(v);
	return 0;
}
Published 51 original articles · won praise 1 · views 1360

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/104829757