8

I was trying to solve one Interview problem that is:

Given a matrix of n*n. Each cell contain 0, 1, -1. 0 denotes there is no diamond but there is a path. 1 denotes there is diamond at that location with a path -1 denotes that the path is blocked. Now you have start from 0,0 and reach to last cell & then return back to 0,0 collecting maximum no of diamonds. While going to last cell you can move only right and down. While returning back you can move only left and up.

I have solved the problem but I am not sure that is the optimal solution.What I am doing is

  • That instead of going back from last cell to first cell I am allowing 2 iteration from initial cell to last cell.
  • When I do first iteration I will try to obtain maximum number of diamonds using dynamic programming and after that I will remove those diamonds that are collected in first iteration from the matrix, ie: set matrix value 0 from 1.
  • In second iteration I will call the same method as of first iteration but with modified matrix.
  • And return the sum of both two calls.

Any suggestions about correctness? I have written the code, If that is needed I will share.

Khatri
  • 616
  • 6
  • 19
  • Just as @Petar Ivanov say, greedy may not work, I think this is a [Bitonic tour](https://en.wikipedia.org/wiki/Bitonic_tour) problem, you can solve it with dynamic programming – throwit Oct 21 '15 at 09:21
  • @F.Ju Here is my code: https://ideone.com/zbTtMp Can you provide me one such example for which it will fail.Note that I am always given priority to row over column whenever possible.For 2 examples Petar provided my algorithm is working well. – Khatri Oct 21 '15 at 09:22
  • possibly related: http://stackoverflow.com/questions/32630503/find-path-cross-matrix-with-max-sum-forward-then-backward – גלעד ברקן Oct 21 '15 at 15:32
  • @F.Ju any counter example? – Khatri Oct 21 '15 at 16:21
  • Please provide a field sizes an required output path or only count of collected diamonds. – Толя Oct 27 '15 at 09:52

3 Answers3

4

Your algorithm is not correct. Here is a counter example:

<table border="1">
  <tr>
    <td>1</td>
    <td>1</td>
    <td>1</td>
  </tr>
  <tr>
    <td>1</td>
    <td>1</td>
    <td>1</td>
  </tr>
   <tr>
    <td>0</td>
    <td>1</td>
    <td>1</td>
  </tr>
</table>

A maximum path from top to bottom will collect 5 diamonds and could be this:

<table border="1">
  <tr>
    <td>*</td>
    <td>*</td>
    <td>_</td>
  </tr>
  <tr>
    <td>_</td>
    <td>*</td>
    <td>*</td>
  </tr>
   <tr>
    <td>_</td>
    <td>_</td>
    <td>*</td>
  </tr>
</table>

But then your second iteration can only collect 2 more.

So your algorithm will return a max value of 7.

But there is a solution, with which you can collect 8.

E.g. if you path down looks like this:

<table border="1">
  <tr>
    <td>*</td>
    <td>_</td>
    <td>_</td>
  </tr>
  <tr>
    <td>*</td>
    <td>*</td>
    <td>_</td>
  </tr>
   <tr>
    <td>_</td>
    <td>*</td>
    <td>*</td>
  </tr>
</table>
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • you are right.For above given input my algorithm might not work.Any suggestions to modify the algorithm? – Khatri Oct 21 '15 at 08:47
  • What if I always prefer row over column if possible, I think then it should work,or I am missing something again? – Khatri Oct 21 '15 at 08:49
  • 1
    If you prefer row over column, I will just traspose my example and it would fail again. There is nothing that distinguishes rows from columns in a matrix... – Petar Ivanov Oct 21 '15 at 08:56
  • No it will not fail if you transpose the matrix.Say input matrix(Transpose of your input) is 1 1 0 1 1 1 1 1 1 Then my lookup matrix will be: 1 2 2 2 3 3 4 5 5 so after first iteration modified matrix will look like: 0 0 0 1 0 0 1 1 0.Sorry I don't know how to write matrix in well format here. – Khatri Oct 21 '15 at 09:01
  • I thin Khatri is right: transpose or not, at least this example would be solved by an algorithm that always prefers to go right, or equally by an algorithm that always prefers to go down. – Janne Karila Oct 21 '15 at 18:56
3

You can use dynamic programming. You need fill matrix with sizes[2*n-1,n,n] Where DP[A,B,C] equals to best result for diagonal(or total way length) A and first way into position B and second in position C.

You starts from DP[0,0,0] and ends into DP[2*n-1,0,0] Answer will be DP[2*n-1,0,0]

DP[l,i,j] = MAX(DP[l-1,i,j], DP[l-1,i-1,j], DP[l-1,i,j-1], DP[l-1,i-1,j-1]) + Diamond[i, l-i] + Diamond[j, l-j] and decrease by Diamond[j, l-j] if i equals j.

And skip all places where way is not possible.

Total complexity will be O(N^3)

Толя
  • 2,839
  • 15
  • 23
0

"you have start from 0,0 and reach to last cell & then return back to 0,0"

== "you have start from 0,0 reach to last cell twice"

and

d: total dist

x1: x position for first

x2: x position for second

y1: d - x1 // can be ommited

y2: d - x2 // can be ommited

dp[d][x1][x2]: Maximum obtained diamond from cell (0, 0) recach to cell (x1, y1) and from cell (0, 0) reach to cell (x2, y2)

Matthias
  • 4,481
  • 12
  • 45
  • 84