Collecting Gems

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?

0 1 2 3 4

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.

8 solutions

Arjen Vreugdenhil
Aug 20, 2017

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 x guards, then he will be left with y = gems guards = ( 7 x ) x = 7 2 x y = \text{gems} - \text{guards} = (7-x) - x = 7 - 2x gems when he reaches the market. To maximize y y we must minimize x x .

The minimum number of guards on a path is x = 2 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 y = 7 - 2\cdot 2 = \boxed{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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
final int WIDTH = 5, HEIGHT = 5;
int map[][] =
    {   { 0, 1, -1, 1, -1 },
        { -1, -1, 1, -1, -1 }, 
        { -1, 1, 1, -1, 1}, 
        { 1, -1, -1, 1, -1 },
        { 1, -1, 1, 1, 0 }
    };    
int score[][] = new int[WIDTH][HEIGHT];

score[0][0] = 0;
for (int s = 1; s < WIDTH+HEIGHT-1; s++)
    for (int x = 0; x <= s; x++) {
        int y = s - x; if (x < WIDTH && y < HEIGHT) {
            int sc1 = (x > 0) ? score[x-1][y] : -1;
            int sc2 = (y > 0) ? score[x][y-1] : -1;

            int sc = (sc1 < 0 || sc2 > sc1) ? sc2 : sc1;

            score[x][y] = (sc < 0) ? -1 : sc + map[x][y];
        }
    }

System.out.println(score[WIDTH-1][HEIGHT-1]);

Each position of the map is assigned a value map[x][y] representing the gain or loss for passing that point: 1 -1 for a guard, + 1 +1 for a gem.

We keep track of the number of gems Joe is carrying in an array score[x][y] , starting with score[0][0] = 0 at the starting point. Now we move upward in diagonals, i.e. x + y = s x + y = s for s = 1 , 2 , s = 1, 2, \dots . Thus we first consider ( 0 , 1 ) (0,1) and ( 1 , 0 ) (1,0) ; then ( 0 , 2 ) , ( 1 , 1 ) , ( 2 , 0 ) (0,2), (1,1), (2,0) ; and so on, until we read the destination score[WIDTH][HEIGHT] . This will end up containing the answer to the problem.

For each point on the grid ( x , y ) (x,y) , we look at its neighbors to the left and down: ( x 1 , y ) (x-1,y) and ( x , y 1 ) (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 in score[x][y] .

The output of this program is 3 , in agreement with the analysis above.

It did not say that Jim was allowed to pick up gems. But I can see that every one has assumed that

Peyman Zehtab-Fard - 3 years, 9 months ago

Log in to reply

I he doesn't, he can't pay the guards, and can't go anywhere!

Arjen Vreugdenhil - 3 years, 9 months ago

Log in to reply

Which is exactly what i thought was the answer. 0. He can not gain any gems.

Peyman Zehtab-Fard - 3 years, 9 months ago

R R U U L L U U R R R R results in Jim acquiring 5 gems on his way to the market.

Trevor Trouchon - 3 years, 9 months ago

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...

Arjen Vreugdenhil - 3 years, 9 months ago

You can only move UP or RIGHT, tou should read it better next time.. i also didnt see that on first time

Pedro Alexandre Arantes - 3 years, 9 months ago

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?

Julian Tegg - 3 years, 9 months ago

Log in to reply

Can't go left...

Arjen Vreugdenhil - 3 years, 9 months ago

Log in to reply

@Julian Tegg Yes, the rules clearly state that you cannot move left or downwards.

William Huang - 3 years, 9 months ago

I got 5 too

Torsten Diesel - 3 years, 9 months ago

Log in to reply

Don't forget to pay the guards one gem each on your way through the red boxes, either.

Arjen Vreugdenhil - 3 years, 9 months ago

But how do you know this is the best?

Agnishom Chattopadhyay - 3 years, 9 months ago

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 4 .

More likely, it's trial-and-error. Let's see if there is the elegant method to work this problem out. :)

Michael Huang - 3 years, 9 months ago

Log in to reply

Hint: dynamic programming

Agnishom Chattopadhyay - 3 years, 9 months ago

Log in to reply

Yup, I thought that one, but I find Arjen's answer to be great! :)

Michael Huang - 3 years, 9 months ago

My answer now has both: my original pen-and-paper analysis, and Java-code using dynamic programming. :)

Arjen Vreugdenhil - 3 years, 9 months ago

Log in to reply

I like the algebraic one though. I love both anyway! :)

Michael Huang - 3 years, 9 months ago

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.

József Inczefi - 3 years, 9 months ago

We can also get 5 gems by another method which, though does not provide the shortest path, gives most gems

Jivesh Kesar - 3 years, 9 months ago

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.

Mark D'Souza - 3 years, 9 months ago
Richard Desper
Aug 22, 2017

Index the 25 25 squares as ( i , j ) (i,j) with ( 1 , 1 ) (1,1) in the lower left and ( 5 , 5 ) (5,5) in the upper right. The only legal moves are ( i , j ) ( i , j + 1 ) (i,j) \rightarrow (i,j+1) and ( i , j ) ( i + 1 , j ) (i,j) \uparrow (i+1,j) .
For n = 2 n = 2 to 10 10 let the set S n = { ( i , j ) i + j = n } S_n = \{(i,j) | i+j = n\} . It's clear every path goes from S 2 S_2 to S 3 S_3 to ... to S 10 S_{10} . None of the intermediate seven sets can be avoided. Now note that both S 4 S_4 and S 7 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 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 ) . (1,1) \rightarrow (1,2) \rightarrow (1,3) \uparrow (2,3) \uparrow (3,3) \uparrow (4,3) \uparrow (5,3) \rightarrow (5,4) \rightarrow (5,5).

Nice approach looking at the diagonals!

Arjen Vreugdenhil - 3 years, 9 months ago

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.

Jonathan Fitch - 3 years, 9 months ago

Log in to reply

No, Joe can't. He is not allowed to go left.

Arjen Vreugdenhil - 3 years, 9 months ago

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
#include <bits/stdc++.h>

#define X 5
#define Y 5

using namespace std;

typedef pair<int,int> ii;
typedef vector<ii> vii;

int dx[] = {1,0};
int dy[] = {0,1};

vii listvertex;

int main(){

int mapweight[5][5] = {{0,1,-1,1,-1},
                       {-1,-1,1,-1,-1}, 
                       {-1,1,1,-1,1}, 
                       {1,-1,-1,1,-1},
                       {1,-1,1,1,0}};

int value[X][Y];

for(int x = 0; x < X; x++)
for(int y = 0; y < Y; y++)
{
value[x][y] = 0;
listvertex.push_back(make_pair(x,y));
}

for(auto u = listvertex.begin(); u < listvertex.end(); u++)
for(int i = 0; i < 2; i++){

ii w_static = *u;
ii w = w_static;

if( (w.first + dx[i] < X) && (w.second + dy[i] < Y) &&
    (w.first + dx[i] >= 0) && (w.second + dy[i] >= 0) ){

w.first += dx[i]; w.second += dy[i];

if(value[w_static.first][w_static.second] + mapweight[w.first][w.second] >= 0){
value[w.first][w.second] = max(value[w.first][w.second],
               value[w_static.first][w_static.second] + mapweight[w.first][w.second]);
}
}

}

cout<<"Maximum Gems: "<<value[X-1][Y-1]<<endl;

}

Surya Subbarao
Aug 24, 2017

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.

Maryam M
Aug 27, 2017

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?

Christopher Boo - 3 years, 9 months ago

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.

Zeno van Ditzhuijzen - 3 years, 8 months ago
Annie Li
Aug 22, 2017

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.

Gordon Chan - 3 years, 9 months ago

Actually, please ignore my solution. I made a big mistake in it. I forgot the the guard charges a gem for every move. Sorry! 😐

Annie Li - 3 years, 9 months ago

Log in to reply

If you do not like your solution, you can always delete it.

William Huang - 3 years, 9 months ago

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.

Terry Flannery - 3 years, 9 months ago

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)

Brad Tandy - 3 years, 9 months ago

Some of the posters have forgotten that you can only move right or up.

Carole Henry - 3 years, 9 months ago

@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! :)

William Huang - 3 years, 9 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...