Unique Paths
Total Accepted: 45580 Total Submissions: 138958My Submissions
Question Solution
A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Hide Tags
这道题比较简单,就是采用动态规划的做法,需要维护一个路径个数的数组,从靠近终点的点开始往前算起就好了
#include<iostream> #include <vector> using namespace std; #define MAXSIZE 110 int uniquePaths(int m, int n) { int ary1[MAXSIZE][MAXSIZE]; ary1[m][n]=1; for(int i=m;i>=1;i--) for(int j=n;j>=1;j--) { if(i==m||j==n) ary1[i][j]=1; else ary1[i][j]=ary1[i+1][j]+ary1[i][j+1]; } return ary1[1][1]; } int main() { }
时间: 2024-11-10 06:02:07