![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Banned
![]() ![]() |
SMAC #4 [09-08] - Digging For Gold (Junior)
Attached to this post is the test data for Digging For Gold.
Digging For Gold (Junior) This is a problem in Graph Theory very similar to Trapped (Junior) from SMAC #2. It requires that you implement a standard depth-first search. Since the cube is limited to 10 x 10 x 10, efficiency is of low priority for this question. I will proceed with the most efficient solution. But even exponential solutions should still get full points unless over-done. Suppose you have some function dig(i,j,k) which digs to square [i,j,k] (where i=level, j=x, and k=y), from some adjacent square (we really don't care what square it came from). The function will return how much gold it can retrieve from here on, in as many digs as needed (since we always have the option to bring the rig back up to the top and get to this same square again). If [i,j,k] is off the cube, then we can find no gold from here. So return 0. If [i,j,k] is a rock, then we shouldn't be here. So return 0. If [i,j,k] is a piece of gold, then add 1 to a local counter and remove the gold (alternatively, turn it into a rock-- see below end note). Since the dig(i,j,k) function will return the amount of gold in as many digs as needed from square [i,j,k], it's pointless to call dig(i,j,k) more than once for the same [i,j,k] triple. Therefore, remember that you've visited [i,j,k]. Alternatively, turn [i,j,k] into a rock. To see how much gold we can get from this point (in as many digs as necessary), by definition of the dig function, it is: (All the gold we can retrieve by repeating this on one square down) + (All the gold we can retrieve by repeating this on each of the 4 side squares) dig(i+1,j,k) + dig(i,j-1,k) + dig(i,j,k-1) + dig(i,j+1,k) + dig(i,j,k+1) So the total answer for dig(i,j,k) is the above plus 1 or 0 depending on the local counter (whether or not [i,j,k] is gold). Now that we have a dig function, the problem is pretty much complete. You are allowed to dig an infinite number of times from any of the ground-level squares. Therefore, call dig(0,j,k) for all j and for all k : {0 <= j,k < n} and sum the total.This will work as long as we remember to remove all the gold we find during each dig. And this will work in polynomial time (O(N^3) to be precise) if we never process the same dig(i,j,k) request more than once. Arla's perfect in Java. Congrats! ![]() Java Syntax (Toggle Plain Text)
Here is a courtesy submission from The Dark. It scores 100, but is not included in the results. It is here for sake of comparison and to learn from. Thanks! C++ Syntax (Toggle Plain Text)
And the official solution in C: C Syntax (Toggle Plain Text)
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges! Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download. Last edited by Sane; Oct 8th, 2008 at 11:11 AM. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Mar 2005
Posts: 346
Rep Power: 4
![]() |
Re: SMAC #4 [09-08] - Digging For Gold (Junior)
Have to admit I made use of Trapped's official solution to figure my own solution for this one, it was an interesting challenge, I think the biggest thing that I needed to get "over" was that you can go in as many times as you like, which is where I ended up just going with just working out which squares you can get to (in the problem you always said which squares you went to in what order, where really what square you went to in what order was utterly irrelevant, it's whether I can get to a square or not).
Now, a harder problem would definitely be the same thing but having to figure both how many pieces you can get, and the shortest path to get them (in this modern gas-efficient age where we want to get the most gold for the least cost!). |
|
|
|
![]() |
| Bookmarks |
| Tags |
| smac junior solutions |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| SMAC #3 [07-08] - Stock Market (Junior) | Sane | Software Design and Algorithms | 5 | Aug 8th, 2008 11:50 AM |
| SMAC #3 [07-08] - Results | Sane | Software Design and Algorithms | 4 | Aug 6th, 2008 8:23 PM |
| SMAC #2 [06-08] - Trapped (Junior) | Sane | Software Design and Algorithms | 0 | Jul 1st, 2008 12:19 PM |
| SMAC #1 [05-08] - Mobile (Beginner) and "n Reach k" (Junior) | Sane | Software Design and Algorithms | 2 | Jun 3rd, 2008 9:10 AM |
| SMAC #1 [05-08] - Results | Sane | Software Design and Algorithms | 6 | Jun 2nd, 2008 5:56 PM |