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

Source: Internet
Author: User

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

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

(0,0)

(0,1)

(0,2)

(0,3)

(0,4)

(1,0)

(a)

(from)

(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)( a)

0 4 5

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

( -1,2) (0,2) (0,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 was 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 if we calculate it the first time, we memorize the result of this sub problem.

MEMO[I][J] The number of the 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 definition: 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)

From right to left

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)

(a)

(from)

(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 definition: 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 is required to go below the line







(0,0)

(0,1)

(0,2)

(0,3)

(0,4)

(1,0)

(a)

(from)

(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 is required to use the cell

Follow up 3:what If there is some blocked cell

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.