In an M * N square, how many ways are there to go from the lower left corner to the upper right corner

There is a square matrix with m rows and n columns on the table. Each square is represented by coordinates. The row coordinates increase from bottom to top, and the column coordinates increase from left to right. The coordinates of the lower left corner are (1, 1), the coordinates of the square in the upper right corner are (m,n).

1. Problem Analysis

There is a square matrix with m rows and n columns on the table. Each square is represented by coordinates. The row coordinates increase from bottom to top, and the column coordinates increase from left to right. The coordinates of the lower left corner are (1, 1), the coordinates of the upper right corner are (m,n) and (0<m+n<=20).

Xiao Ming is a naughty child. One day he caught an ant and accidentally injured the ant's right foot, so the ant could only move up or to the right. Xiao Ming puts the ant in the square in the lower left corner, and the ant moves from the square in the lower left corner to the square in the upper right corner, moving one square at a time. Ants always move in the grid matrix, please calculate the number of different moving routes.

1.1 Objects of processing (data)

The first line has two numbers m rows and n columns.

1.2 Implemented functions

Output the number of different travel routes.

1.3 How to display the processed results

An integer indicating the number of path types.

1.4 Please use the sample in the title to give the solution process of the sample in detail.

【Input example】 7 8

【Example of output】 1716

【analyze】

Method 1. Mathematical Analysis

It is not difficult to find that the answer can be calculated by permutation and combination (the specific proof will be given later),

The result is C (7+8-2, 7-1) or C (13, 6), just program to calculate the number of combinations. The final answer was 1716.

Method 2, dynamic programming method

It is not difficult to find that m[1][1]=1, and the first row is also 1. The second row and the second column start to change, m[2][2]=m[1][2]+m[2][1]=2, then follow m[i][j]=m[i -1][j]+m[i][j-1] and so on. The final answer was 1716.

2. Algorithm design and complexity analysis

2.1 Mathematical methods

2.1.1 Algorithm idea

After careful observation, it is not difficult to find that this question is essentially a relatively simple model in mathematics. We found that no matter how you walk, the total number of steps is limited, and the number of steps to the right and the number of steps up are both constant. That is, the upward must be m-1, and the right must be n-1.

So the essence of what I mentioned becomes to select m-1 times from m+n-2 times to make them upward.

The answer is C(m+n-2, m-1) . Just count the number of combinations .

2.1.2 Key functions

There are two key functions, one is to calculate the number of combinations, and to calculate the number of combinations needs to calculate three factorials. Therefore, programming is required to realize the process of calculating factorial and the process of calculating the number of combinations through factorial.

In the following code, the long long int jiecheng(int n) function shows the method of calculating factorial, and long long int combination(int n,int m) shows the method of calculating the number of combinations.

2.1.3 Code implementation

#include<bits/stdc++.h>
 
using namespace std;  
 
long long int jiecheng(int n)
 
{ 
    long long int temp=1;
 
    for (int i=1;i<=n;i++) 
    { 
        temp*=i; 
    } 
    return temp; 
}

long long int combination(int n,int m) 
{ 
    return (jiecheng(n)/(jiecheng(m)*jiecheng(n-m))); 
}
 
int main() 
{
     int n,m; 
    cin>>n>>m; 
    cout<<combination(n+m-2,m-1); 
    return 0; 
}

 2.1.4 Complexity analysis

Time complexity: Mainly use a layer of for loop to calculate factorial, the complexity is O(n).

Space complexity: almost negligible.

2.2 Dynamic programming method

2.2.1 Algorithm idea

In fact, this question can also be regarded as a simple dynamic programming. For the nodes that are not on the boundary (that is, the first row or the first column) in the graph, their values ​​obviously have m[i][j]=m[i-1][j]+m[i][j -1], that is, the method to reach this node is equal to the method to reach a node before this node, plus the method to reach this node.

In this way, the size of the problem can be reduced. The way to get to the origin will eventually be compressed to the origin, and obviously 1 from the origin to the origin.

2.2.2 Key functions

Construct the state transition equation,

m[i][j]=1; (i is 1 or j is 1)

m[i][j]=m[i-1][j]+m[i][j-1];(i,j不为1)

A double loop is constructed for the dynamic programming process.

2.2.3 Code implementation

#include<bits/stdc++.h>
 
using namespace std;
 
int main() 
{ 
    int n,m; 
    cin>>n>>m; 
    long long int map[m+1][n+1];
 
    for (int i=1;i<=m;i++)
    {
        for (int j=1;j<=n;j++) 
        { 
            if (i==1 || j==1)
            {
                map[i][j]=1;
            }  
            else 
            {
                map[i][j]=map[i-1][j]+map[i][j-1];
            } 
        }
    }
    cout<<map[m][n]; 
    return 0; 
}

2.2.4 Complexity Analysis

Time complexity: dynamic programming, double loop, O( n^{2}).

Space complexity: an array of n*m is O( n^{2}).

Guess you like

Origin blog.csdn.net/weixin_43705303/article/details/130011952