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?

class Solution {
public:
    int ans;
    void dfs(int i, int j, int step,int m, int n){
        if (i == m || j == n){
            return;
        }
        if (step == m + n -1){
            ans++;
            return;
        }
        dfs(i+1,j,step + 1,m,n);
        dfs(i,j+1,step + 1,m,n);
        return;
    }
    int uniquePaths(int m, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        ans = 0;
        dfs(0,0,1,m,n);
        return ans;
    }
};

 

posted @ 2013-07-12 07:24  一只会思考的猪  阅读(121)  评论(0编辑  收藏  举报