Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8633477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:29:53+00:00 2026-06-12T09:29:53+00:00

I have a n*n matrix, where each element represents an integer. Starting in [0,0]

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:29:53+00:00Added an answer on June 12, 2026 at 9:29 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In R I have the following matrix (each row represents a bootstrap 95% confidence
I have a matrix A with integer elements from 0 to N-1. What I
I have a list where each element is a 5*5 matrix. Eg [[1]] V1
I have a matrix in R. Each entry i , j is a score
I have a SurfaceView where I'm drawing a matrix (game board) and each cell
I am trying to plot a matrix where each element is in one out
Possible Duplicate: How do I iterate through each element in an n-dimensional matrix in
I have a vector A [9 3 6 9 3 6] each 2 elements
I have 100 models (matrices), where each matrix's size is 4X3. Each of the
I have a matrix in SAS IML. For each pair of rows (say vectors

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.