unique path2

 1 class Solution {
 2 public:
 3     int helper(vector<vector<int> > &G, vector<vector<int> > &S,int r, int c )
 4     {
 5         if( r == G.size()-1 && c == G[0].size()-1 && G[r][c]!=1 )  return 1;
 6         if( r < 0 || r >= G.size() || c <0 || c >= G[0].size() || G[r][c] == 1)
 7             return 0;
 8         if( S[r][c] != -1 )  return S[r][c];
 9         
10         S[r][c] = helper( G, S, r+1, c ) + helper( G, S, r, c+1 );
11         
12         return S[r][c];
13            
14     }
15     int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
16         // Start typing your C/C++ solution below
17         // DO NOT write int main() function
18         if( obstacleGrid.empty() || obstacleGrid[0].empty() )
19             return 0;
20         vector<vector<int> > v( obstacleGrid.size(), vector<int>(obstacleGrid[0].size(),-1));
21         return helper(obstacleGrid, v, 0,0 );
22         
23     }
24 };

 

posted on 2013-09-03 20:54  jumping_grass  阅读(185)  评论(0)    收藏  举报

导航