Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
867 views
in Technique[技术] by (71.8m points)

performance - Algorithm to find max cost path of length N in matrix, from [0,0] to last row

I have a n*n matrix, where each element represents an integer. Starting in [0,0] I have to find the path of exactly m elements down to the last row, returning the maximum cost. The path can end in any column on the last row and n ≤ m ≤ n^2

I thought of finding all paths of length m from [0,0] to [n-1, 0], [n-1, 1] ... [n-1, n-1]. But it does not feel optimal...

Which algorithm would be the most efficient way of doing this? BFS or DFS?

EDIT

Possible directions are down/right/left, but only visit each element at most once.

EDIT 2

So for example, if this matrix is given (n=4):

[   1   4   1  20 ]
[   5   0   2   8 ]
[   6   8   3   8 ]
[   3   2   9   5 ]

And m=7, the path could be

[   →   →   →   ↓ ]
[   5   0   2   ↓ ]
[   6   8   3   ↓ ]
[   3   2   9   x ] = Path cost = 47

or

[   ↓   4   1  20 ]
[   ↓   0   2   8 ]
[   →   →   ↓   8 ]
[   3   2   →   x ] = Path cost = 32 

or if m = n^2

[   →   →   →   ↓ ]
[   ↓   ←   ←   ← ]
[   →   →   →   ↓ ]
[   x   ←   ←   ← ]

EDIT 3 / SOLUTION

Thanks to Wanderley Guimar?es,
http://ideone.com/0iLS2

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can solve this problem with dynamic programming. Let value(i, j) the value from position (i, j) of your matrix (i-th line, j-th column).

if i <  0 then f(i, j) = 0
if i == 0 then f(i, j) = value(i, j)
if i >  0 then f(i, j) = max(f(i-1, j), f(i, j-1)) + value(i, j)

That recurrence assume that you use a position from your matrix when you step down. So, you answer is max(f(m, 0), f(m-1, 1), f(m - 2, 2), ..., f(1, m)).

For example:

Give the follow matrix for n = 4:

1 1 1 1
1 2 1 1
2 1 1 1
1 2 1 1

If m = 2 then you cant go after second line. And you answer is f(2, 2) = 4.

If m = 4 then you cant go after third line. And you answer is f(3, 2) = 5.

(Im learning english so If you didnt understand something let me know and I will try to improve my explanation).

Edit :: allow down/left/right moves

You can implement follow recurrence:

if i == n, j == n, p == m, j == 0 then f(i, j, p, d) = 0
if d == DOWN  then f(i, j, p, d) = max(f(i+1, j, p+1, DOWN), f(i, j-1, p+1, RIGHT),   f(i, j+1, p+1,LEFT)) + value(i, j)
if d == LEFT  then f(i, j, p, d) = max(f(i+1, j, p+1, DOWN), f(i, j+1, p+1, LEFT )) + value(i, j)
if d == RIGHT then f(i, j, p, d) = max(f(i+1, j, p+1, DOWN), f(i, j-1, p+1, RIGHT)) + value(i, j)

This solution is O(n^4). Im trying to improve it.

You can test it at http://ideone.com/tbH1j


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...