Unique Paths

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?

 

解答:动态规划

  1. int uniquePaths(int m, int n) {
  2. vector<int> res(n,0);
  3. for(int i=0;i<n;i++) res[i]=1;
  4. for(int i=1;i<m;i++) {
  5. for(int j=0;j<n;j++) {
  6. if(j==0) res[j]=1;
  7. else {
  8. res[j] = res[j] + res[j-1];
  9. }
  10. }
  11. }
  12. return res[n-1];
  13. }

 

posted @ 2014-08-31 23:25  purejade  阅读(70)  评论(0)    收藏  举报