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

  • Home
  • SEARCH
  • 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 3627080
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:50:34+00:00 2026-05-18T23:50:34+00:00

Here is my first question about maximum L sum and here is different and

  • 0

Here is my first question about maximum L sum and here is different and hard version of it.

Problem : Given a mxn positive integer matrix find the minimum L sum from 1th row to the m’th row . L(4 item) likes chess horse move

Example : M = 3×3

0 1 2

1 3 2

4 2 1

Possible L moves are : (0 1 2 2), (0 1 3 2) (0 1 4 2)
We should go from 1th row to the 3th row with minimum sum

I solved this with dynamic-programming and here is my algorithm :

1. Take a mxn another Minimum L Moves Sum array and copy the first row of main matrix. I call it (MLMS)
2. start from first cell and look the up L moves and calculate it
3. insert it in MLMS if it is less than exists value
4. Do step 2. until m’th row
5. Choose the minimum sum in the m’th row

Let me explain on my example step by step:

  1. M[ 0 ][ 0 ] sum(L1 = (0, 1, 2, 2)) = 5 ; sum(L2 = (0,1,3,2)) = 6; so MLMS[ 0 ][ 1 ] = 6
    sum(L3 = (0, 1, 3, 2)) = 6 ; sum(L4 = (0,1,4,2)) = 7; so MLMS[ 2 ][ 1 ] = 6

  2. M[ 0 ][ 1 ] sum(L5 = (1, 0, 1, 4)) = 6; sum(L6 = (1,3,2,4)) = 10; so MLMS[ 2 ][ 2 ] = 6

    …
    the last MSLS is :
    0 1 2
    4 3 6
    6 6 6
    Which means 6 is the minimum L sum that can be reach from 0 to the m.

I think it is O(8*(m-1)n) = O(mn). Is there any optimal solution or dynamic-programming algorithms fit this problem?

Thanks, sorry for long question

  • 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-05-18T23:50:35+00:00Added an answer on May 18, 2026 at 11:50 pm

    How there can be both 0-th and m-th rows in a matrix with total m rows?

    Anyway, a simple Dijkstra will give you an optimal path in O(n*m) (assuming you have a good data structure to find minimum in the list of not yet reached points).

    Also, if your knight can move only down the board (it can be useful to move up sometimes to decrease total path weight), you can write simple DP. Just start from the bottom of the board and for each position calculate how much it’ll take to reach bottom. Since you can do up to 4 moves from each position, it’s a simple check for minimum. Something like this

    for (int row = m - 1; row >= 0; --row) {
        for (int col = 0; col < m; ++col) {
            int path1 = a[row][col + 1] + a[row][col + 2] + a[row + 1][col + 2] + best[row + 1][col + 2];
            int path2 = a[row][col + 1] + a[row + 1][col + 1] + a[row + 2][col + 1] + best[row + 2][col + 1];
            int path3 = ...
            int path4 = ...
            best[row][col] = min(path1, path2, path3, path4);
        }
    }
    

    edit
    Why do you need to go up? Imagine matrix like this (where * means some ridiculously big number, like 1000000000). Obviously, you’ll have to go from (0, 0) to the right part of the board before you go down.

    111111111111
    111111111111
    *********111
    *********111
    

    So, path will look like this

    ...1...1...1
    11...1...1..
    *********11.
    *********11.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First question on here so please be nice :) I know very little about
i've asked the first question about selects here , i thought, that when i
this is my first question here. I've made a small quiz project about the
Here's my first question at SO. I have a internal application for my company
first question here. I'm developing a program in C# (.NET 3.5) that displays files
this is my first question here so I hope I can articulate it well
Greetings to all! This is my first question here on stackoverflow. I have a
this is my first question to stackoverflow so here it goes... I use cruise
Here is a problem I've struggled with ever since I first started learning object-oriented
I had another question about this issue, but I didn't ask properly, so here

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.