码迷,mamicode.com
首页 > 其他好文 > 详细

Given a matrix, get the number of path from the top left cell to the top right cell.

时间:2018-10-04 08:46:01      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:||   hat   this   get   cti   when   ase   matrix   []   

 

 

Given a matrix, get the number of path from the top left cell to the top right cell.

Note that: you can only go right, up right, down right.

(0,0)

(0,1)

(0,2)

(0,3)

(0,4)

(1,0)

(1,1)

(1,2)

(1,3)

(1,4)

(2,0)

(2,1)

(2,2)

(2,3)

(2,4)

(3,0)

(3,1)

(3,2)

(3,3)

(3,4)


sol1: DFS

(0,0)

9

/ur |r \dr

(-1,1) (0,1) (1,1)

 0                     4 5

   /ur |r \dr /ur |r \dr

(-1,2)   (0,2) (1,2) (0,2) (1,2) (2,2)

 0                   2   2 2                        2 1

   / |     \     / |      \         /      | \          / | \           / |  \

(-1,3)(0,3)(1,3)   (0,3)(1,3)(2,3)   (-1,3)(0,3)(1,3)   (0,3)(1,3)(2,3)    (1,3)(2,3)(3,3)

  0    1 1         1 1 0     0 1 1 1       1 0 1 0 0

         / | \ / | \    / | \   / | \ / | \              / | \ / | \ / | \ / | \  / | \ / | \ / | \ / | \

                  010 100    010 100000           010 100    010   100 000  100 000 00 0

Each node is a sub problem: the number of path from the cell (x,y) to (0,4)

branching factor: 3

base case: (x,y) out of bound, / y = col - 1

Time: O(3^n * 1)

Space: O(n)  n = col


sol2: DFS + memorization

We can notice that, some sub problem, ie. the node with the same cell pair, is calculated multiple times.

So when we calculate it the first time, we memorize the result of this sub problem.

memo[i][j]  the number of path from (i,j) to (0,4)

(0,0) (3,4)  4*5

Time: O(col* row * 1)

Space:O(col)  + O(col*row)



  public int DFSwithMemo(int x, int y, int row, int col, int[][] memo) {
       if (x < 0 || x > row - 1 || y < 0 || y > col - 1) {
           return 0;
       }
       if (x == 0 && y == col - 1) {
           return 1;
       }
       if (y == col - 1) {
           return 0;
       }
       if (memo[x][y] >= 0) {
           return memo[x][y];
       }
       int upRight = DFSwithMemo(x - 1, y + 1, row, col, memo);
       int right = DFSwithMemo(x, y + 1, row, col, memo);
       int downRight = DFSwithMemo(x + 1, y + 1, row, col, memo);
       memo[x][y] = upRight + right + downRight;
       return upRight + right + downRight;
   }



sol3: DP

dp定义: dp[i][j] the number of path from (i,j) to (0,4)

induction rule: dp[i][j] = dp[i-1][j+1] + dp[i][j+1]+dp[i+1][j+1]

base case: dp[0][4] = 1, dp[i][4] = 0 i != 0

i < 0 || i > row - 1 || y < 0 || y> col - 1 dp[i][j] =  0

result: dp[0][0]

Space:O(row*col)

从右向左

2   4

2   4

1  3

0  1

O(row * 2)

 









 4*5     0

x  x 2  1 1

x x  x 1 0

x x  x 0 0

x x  x 0 0


2     0

2

1

0

x  x 2  1 1

x x  x 1 0

x x  x 0 0

x x  x 0 0



(0,0)

(0,1)

(0,2)

(0,3)

(0,4)

(1,0)

(1,1)

(1,2)

(1,3)

(1,4)

(2,0)

(2,1)

(2,2)

(2,3)

(2,4)

(3,0)

(3,1)

(3,2)

(3,3)

(3,4)


dp定义: dp[i][j] the number of path from (0,0) to (i,j)

induction rule: dp[i][j] = dp[i-1][j-1] + dp[i][j-1]+dp[i+1][j-1]

base case: dp[0][0] = 1, dp[i][0] = 0 i != 0

i < 0 || i > row - 1 || y < 0 || y> col - 1 dp[i][j] =  0

result: dp[0][4]

Time: O(col * row * 1)

Space:O(col * row)




""

 

 

 

 

 

Follow up 1: What if you are required to go below the line







(0,0)

(0,1)

(0,2)

(0,3)

(0,4)

(1,0)

(1,1)

(1,2)

(1,3)

(1,4)

(2,0)

(2,1)

(2,2)

(2,3)

(2,4)

(3,0)

(3,1)

(3,2)

(3,3)

(3,4)

 

Follow up 2: What if you are required to use the cell (1,1)

Follow up 3: What if there are some blocked cell

Given a matrix, get the number of path from the top left cell to the top right cell.

标签:||   hat   this   get   cti   when   ase   matrix   []   

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9741349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!