Joe (bottom left) is on his way to the market (top right), and he can only move up or right in each step.
In every cell he visits, there is either a gem or a guard. Each guard charges 1 gem to let him move on.
What is the maximum (net) number of gems Joe can gain along the way to the market?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
It did not say that Jim was allowed to pick up gems. But I can see that every one has assumed that
Log in to reply
I he doesn't, he can't pay the guards, and can't go anywhere!
Log in to reply
Which is exactly what i thought was the answer. 0. He can not gain any gems.
R R U U L L U U R R R R results in Jim acquiring 5 gems on his way to the market.
Log in to reply
Jim maybe able to do this, but the Joe of the problem is not allowed to take steps to the left...
You can only move UP or RIGHT, tou should read it better next time.. i also didnt see that on first time
We went firstUp up right up up right right then we reach market this is the easiest way.
I got 5. RRUULLUUR to the end. Or am I missing something?
Log in to reply
Can't go left...
Log in to reply
@Julian Tegg Yes, the rules clearly state that you cannot move left or downwards.
I got 5 too
Log in to reply
Don't forget to pay the guards one gem each on your way through the red boxes, either.
But how do you know this is the best?
Actually, there is more than a path to obtain the most gems. However, to prove that amount is the maximum, you need to show that there is no path that gives at least 4 .
More likely, it's trial-and-error. Let's see if there is the elegant method to work this problem out. :)
Log in to reply
Hint: dynamic programming
Log in to reply
Yup, I thought that one, but I find Arjen's answer to be great! :)
My answer now has both: my original pen-and-paper analysis, and Java-code using dynamic programming. :)
Log in to reply
I like the algebraic one though. I love both anyway! :)
How do you move "firstUp"? I mean that's a guard tile. Because the tiles with the gems are... the tiles with gems in them?! So you can't pay the guard on the first move.
We can also get 5 gems by another method which, though does not provide the shortest path, gives most gems
Log in to reply
it is 8 steps only, no more or less. the rules only allow you to move up or right. also, you need to give away a gem on every red square or the game is over. the first move is R then you lose the gem on the next move, so you pick the path with 2 gems landing in the middle, lose 1, gain 2 before you finish. there is only one successful path that yields 1 gem net, the rest yield 3 and no more and all require your first move to the right and land in the middle on move 4.
Index the
2
5
squares as
(
i
,
j
)
with
(
1
,
1
)
in the lower left and
(
5
,
5
)
in the upper right. The only legal moves are
(
i
,
j
)
→
(
i
,
j
+
1
)
and
(
i
,
j
)
↑
(
i
+
1
,
j
)
.
For
n
=
2
to
1
0
let the set
S
n
=
{
(
i
,
j
)
∣
i
+
j
=
n
}
. It's clear every path goes from
S
2
to
S
3
to ... to
S
1
0
.
None of the intermediate seven sets can be avoided.
Now note that both
S
4
and
S
7
contain only guards and no diamonds. Thus any path must hit at least two guards and at most five diamonds. Thus the maximum possible return is
3
diamonds.
This can be achieved by taking the path
(
1
,
1
)
→
(
1
,
2
)
→
(
1
,
3
)
↑
(
2
,
3
)
↑
(
3
,
3
)
↑
(
4
,
3
)
↑
(
5
,
3
)
→
(
5
,
4
)
→
(
5
,
5
)
.
Nice approach looking at the diagonals!
Actually, he can net 6. Take this path (r=right, l=left, u=up, d=down). R u r u l l u u r r r d u r. Gains 9 loses 3.
Log in to reply
No, Joe can't. He is not allowed to go left.
This problem can be solved using Bellman-Ford Algorithm which algorithm search the minimum in this case maximum value of a vertex if there are negative weights, here the implementation in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
The total number of steps for any given path is 8 steps. If we minimize the amount of red squares we step on, we will maximize the amount of gems he has.
We can see that finding a one red square solution is impossible after trying things. Next, we try a two step solution, and we find one. (It's right-right-up-up-up-up-right-right)
Counting the number of gems we get in this solution, we find that we have 3 gems left.
Assuming all the red blocks are gaurding and all the blue blocks are gems the shortest route would be go to the middle of the bottom row, the way up the middle column and finally all the way right with 3 gems.
I noticed that Joe will visit 7 places on his way to the market, because of the limitation on his movement (up/right). Each place he visits lies on a diagonal of potential visits per step (see screenshot below). Step 1's diagonal allows two different places to be visited, step 2 has 3 different places, etc.
Notice that step 2 & 5 have no diamonds, hence in theory the maximum diamonds Joe will be able to get is 1-1+1+1-1+1+1 = 3
When checking for a possible path, I found: Right, Up, Right, Up, Right, Up, Up, Right.
There are many variations to this path. Key diamonds to get are the first diamond after stepping right, the diamond in the middle (as you can never reach the one in the top-left afterwards), and the one directly left to the market.
What do you mean by "diagonal of potential visits per step"? Why are we focusing on the diagonals?
Log in to reply
With diagonal I mean: http://i65.tinypic.com/dzv8f6.jpg (also added the picture to my original post, sorry that it wasn't clear initially). Every step brings you to a square in the next diagonal. And so you can never visit two squares that lie on the same diagonal. Hence, because step 2 & 5 you can't possibly get a diamond, the max you can get is 3.
that is what I did. Also, what sort of jails have GEMS 💎 in them? Jail is a place for criminals and one of the most common crimes is stealing so I am not surprised 😳 on why the criminals haven't stolen them yet? Plus, if the jail guard takes so many gems just because someone needs to visit the market or toilet, they must be really, REALLY rich by now.
This statement is not unclear. "In every cell he visits, there is either a gem or a guard. Each guard charges 1 gem to let him move on". The way I read this is: When Joe passes a cell with a GEM, he gains one GEM because there is no GUARD there, when passes a cell with a GUARD, he gives away one GEM and he must has a GEM to be collected in order to pass the GUARD cell. If I take the path of: (2,1)(3,1)(3,2)(3,3)(2,3)(1,3)(1,4)(1,5)(2,5)(3,5)(4,5), I ended up with +1-1+1+1+1-1+1+1-1+1+1=5.
Actually, please ignore my solution. I made a big mistake in it. I forgot the the guard charges a gem for every move. Sorry! 😐
Log in to reply
If you do not like your solution, you can always delete it.
I get 5. Going 2 to the right leaves Joe with zero gems. If he then goes 2 up and 2 left, he will be left with 2 gems. Going 2 up from there will bring him to 4 gems. Now going straight over to the market will gain him 2 but cost him 1, leaving him with 5.
Reading the statement "In every cell he visits, there is either a gem or a guard. Each guard charges 1 gem to let him move on" as meaning "every cell either PAYS or COSTS one gem", not both. Therefore the solution is 5 net gems. R (+1), R (-1=0), U (+1=1), U (+1=2), L (+1=3), L (-1=2), U (+1=3), U (+1=4), R (-1=3), R (+1=4), D (-1=3), R (+1=4), U (+1=5), R (Market with 5 gems in hand)
Some of the posters have forgotten that you can only move right or up.
@Gordon Chan , @Terry Flannery , @Brad Tandy - I think you have forgotten that you are only allowed to move up and right. You are not allowed to move left as the question clearly states so. The question reads...
and he can only move up or right in each step.
Just for your information, not trying to offend or anything! :)
Problem Loading...
Note Loading...
Set Loading...
No matter which of the 70 possible paths Joe chooses, he must pass through 7 boxes with a gem or a guard. If there are x guards, then he will be left with y = gems − guards = ( 7 − x ) − x = 7 − 2 x gems when he reaches the market. To maximize y we must minimize x .
The minimum number of guards on a path is x = 2 : go two steps to the right, then all the way to the top, then all the way to the right. This leaves y = 7 − 2 ⋅ 2 = 3 gems to the market.
Quick check: nowhere along this path does Joe run out of gems...
And here we let the computer sort it out:
Each position of the map is assigned a value
map[x][y]
representing the gain or loss for passing that point: − 1 for a guard, + 1 for a gem.We keep track of the number of gems Joe is carrying in an array
score[x][y]
, starting withscore[0][0] = 0
at the starting point. Now we move upward in diagonals, i.e. x + y = s for s = 1 , 2 , … . Thus we first consider ( 0 , 1 ) and ( 1 , 0 ) ; then ( 0 , 2 ) , ( 1 , 1 ) , ( 2 , 0 ) ; and so on, until we read the destinationscore[WIDTH][HEIGHT]
. This will end up containing the answer to the problem.For each point on the grid ( x , y ) , we look at its neighbors to the left and down: ( x − 1 , y ) and ( x , y − 1 ) . We choose the highest of both (to optimize the number of gems). A negative value indicates that the neighbor lies of the grid, or that Joe has insufficient gems to make it through; in other words,
score[x][y] < 0
means "inaccessible". Finally, we add to the highest score of accessible neighbors the gain or loss of the new box; this is the highest number of gems with which the box can be reached, and this we store inscore[x][y]
.The output of this program is
3
, in agreement with the analysis above.