MacTech Network:   MacTech Forums  |  MacForge.net  |  Computer Memory  |  Register Domains  |  Cables  |  iPod Deals  | Mac Deals  | Mac Book Shelf


  MacTech Magazine

The journal of Macintosh technology

 
 

Magazine In Print
  About MacTech  
  Home Page  
  Subscribe  
  Archives DVD  
  Submit News  
  MacTech Forums  
  Get a copy of MacTech RISK FREE  
MacTech Only Search:
Community Search:
MacTech Central
  by Category  
  by Company  
  by Product  
MacTech News
  MacTech News  
  Previous News  
  MacTech RSS  
Article Archives
  Show Indices  
  by Volume  
  by Author  
  Source Code FTP  
Inside MacTech
  Writer's Kit  
  Editorial Staff  
  Editorial Calendar  
  Back Issues  
  Advertising  
Contact Us
  Customer Service  
  MacTech Store  
  Legal/Disclaimers  
  Webmaster Feedback  
 Get Netflix

 January 2000 Programmer's Challenge

Peg Triangles

Mail solutions to: progchallenge@mactech.com
Due Date: 11:59pm ET, Saturday, 1 January 2000

Welcome to the New Year! If you’re not one of those people who quibble about whether the new century starts in 2000 or 2001, welcome also to a new century and a new millennium! As I write this, I can’t be certain whether the doomsday predictions will turn out to have had any validity, but I’m betting that as you read this, the lights will be on, the telephones will work, the ATMs might be occasionally short of cash, the grocery stores will be replenishing their canned goods, and the pundits will be questioning what the fuss was all about. (At least this is my prediction for the so-called First World — I’m not close enough to the situation in the rest of the world to be as confident.) Of course, as someone whose Real Job included making certain that this transition to January was as boring as any other, I know that there was a lot of hard work invested in ensuring that life went on in the IT world.

But I’m betting that most of you are ready and able to take on this month’s Programmer’s Challenge. Especially since most of you are presumably Mac users, who didn’t (or don’t) have to go through the complicated process of remediating their Monopoly-OS machine so that it would continue to function. We’re going to make the Challenge a little simpler this month, in hopes of encouraging some new participants to take a shot at making the Top Five.

A while back my daughter presented me with a puzzle she had made in wood-shop. (Yes, my town has progressed to the point where the girls take wood-shop and metallurgy classes and the boys take cooking and sewing.) The puzzle is a triangular block of wood with holes drilled into a triangular pattern: 1 hole in the first row, 2 holes in the second, etc. A peg occupies each of the holes, except one. The puzzle is solved by a sequence of jump moves, where a peg jumps over an adjacent peg into an open hole in the same direction, after which the jumped peg is removed from the board. The object of the game is to remove all of the pegs from the board, except one.

Our Challenge will be a slight generalization of this puzzle. The initial board position will always contain at least one vacant hole, but perhaps more than one. The object will be to minimize the number of pegs remaining on the board at the end of the game, but because of the initial configuration it might not be possible to reduce the number of pegs remaining to one.

The prototype for the code you should write is:

#if defined(__cplusplus)
extern "C" {
#endif

typedef struct TrianglePegPosition {
  short row;  /* 0..numberOfRows-1 */
  short col;  /* -row, -row+2, ..., +row */
} TrianglePegPosition;

typedef struct PegJump {
  TrianglePegPosition from;
  TrianglePegPosition to;
      // must satisfy
      //    (((to.row == from.row) && (to.col == from.col +- 4) && 
      //      ((from.row, from.col +-2) occupied))  ||
      //     ((to.row == from.row +- 2) && (to.col == from.col +- 2) &&      //      ((from.row +- 1, from.col +-1) occupied)) )
} PegJump;

short /* number of moves */ SolvePegTriangle (
  short triangleSize,    /* number of rows in triangle to solve */
  short numInitialPegs,  /* number of pegs in starting puzzle position */
  TrianglePegPosition initialPegPositions[],
        /* peg locations in starting puzzle position */
  PegJump pegJumps[]
        /* return peg moves that solve the puzzle here, in sequence */
);

#if defined(__cplusplus)
}
#endif

Your SolvePegTriangle routine is provided with the initial puzzle conditions and must store the moves required to solve the puzzle in pegJumps, returning from SolvePegTriangle the number of moves in pegJumps. As initial conditions, SolvePegTriangle is given the dimension of the puzzle (triangleSize), the number of pegs in the initial puzzle state (numInitialPegs), and the position of those initial pegs.

The winner will be the solution that solves a sequence of puzzles at the lowest cost. Each millisecond of execution time used to solve the puzzles will incur a cost of 1 point. Each peg left on the board beyond the first will incur a cost of 1000 points (whether or not it is possible to remove all but one peg from the board).

This will be a native PowerPC Challenge, using the CodeWarrior Pro 5 environment. Solutions may be coded in C, C++, or Pascal. Solutions in Java will also be accepted this month. Java entries must be accompanied by a test driver that uses the interface provided in the problem statement.


Test code for this Challenge is available.


You can get a head start on the Challenge by reading the Programmer's Challenge mailing list. It will be posted to the list on or before the 12th of the preceding month. To join, send an email to listserv@listmail.xplain.com with the subject "subscribe challenge-A". You can also join the discussion list by sending a message with the subject "subscribe challenge-D".





Generate a short URL for this page:


Click on the cover to
see this month's issue!

TRIAL SUBSCRIPTION
Get a RISK-FREE subscription to the only technical Mac magazine!

Today's Deal


Apple Special

Order
Snow Leopard,
Mac Box Set, Family Pack,
and Snow Leopard Server
at a discount.
 


MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797

Register Low Cost (ok dirt cheap!) Domain Names in the MacTech Domain Store. As low as $1.99!
Save on long distance * Upgrade your Computer. See local info about Westlake Village
appleexpo.com | bathjot.com | bathroomjot.com | bettersupplies.com | comclothing.com | computerlunatics.com | dotcomclothing.com | explainit.com | exposinternational.com | homeismycastle.com | hoodcards.com | intlexpo.com | keyinfocard.com | kosheru.com | learnmorsels.com | localdealcards.com | lvschools.com | macjobsearch.com | mactechjobs.com | mactechmonitor.com | mactechsupplies.com | macwishbook.com | movie-depot.com | netprofessional.com | nibblelearning.com | notesintheshower.com | officejot.com | onlinebigbox.com | palmosdepot.com | peopleslineitemveto.com | showerjot.com | snapestore.com | snapishop.com | snapistore.com | snaptradingpost.com | stimulusmap.com | stimulusroadmap.com | triunfoguides.com | video-depot.com
Staff Site Links



All contents are Copyright 1984-2008 by Xplain Corporation. All rights reserved.

MacTech is a registered trademark of Xplain Corporation. Xplain, Video Depot, Movie Depot, Palm OS Depot, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, NetProLive, JavaTech, WebTech, BeTech, LinuxTech, Apple Expo, MacTech Central and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.