Homework 1

Implementing Search to solve a "Challenge"


Due: Friday, February 13th, 1:00 PM


Introduction

[Note: This problem was originally created by Dr. Shaw as a Math contest activity.]

Suppose that you have a 3x3 pendent of jewels.  Each of the 9 jewels is either a diamond, a ruby, or an emerald.  These jewels can be changed by casting a spell at a particular jewel.  This spell will change a diamond into a ruby, a ruby into an emerald, or an emerald into a diamond. Unfortunately, the spell also modifies the jewels touching the particular jewels both horizontally and vertically! 

Thus, if you started with the following pendant:

DRE
RED
EDR

and cast a spell at the center emerald, you would end up with the following pendant

DEE
EDR
ERR

The "goal" of this puzzle is that given a starting pendent, find the shortest sequence of spells that will turn the pendent into one containing 9 of the same jewel. 

BEAUTIFUL!  A straightforward search problem where at each node in the search you have 9 possible next moves (9 possible spell locations).  The challenge is picking which of the 5 strategies we might use.  We will talk about this in class, but I think there are two which make the most sense. 

Requirements

For this homework you should write a program that fits the following requirements:

public class JewelGame {

public static void main(String[] args) {

String start = "DREREDEDR";

....

}

}

 

Hints from past experience with this assignment...

Remember what happens when you "copy" a referential data type in Java... you only get a copy of the reference, not a copy of the object.  This means that if you have a node that you want to copy several times and modify to create the children nodes, you need to be careful what you do to copy the node.  Students have a habit of saying something like:

    //Assume that Node parent was created elsewhere
    Node child1 = parent;
    Node child2 = parent;
    Node child3 = parent;
    child1.modifyOneWay();
    child2.modifyAnotherWay();
    child3.modifyYETAnotherWay();

This just gives you FOUR variables all of which point to the same single instant.  Students end up forgetting this and then being surprised when child1, child2, and child3 are identical and TOTALLY screwed up (in essence, they just cast 3 different spells on the same single object.

You will need to write code which produces a true, second copy of whatever data structure you use in your tree.

 

2)  When it gets to be time to test your code, start with simple problems.  For example,

DED
EEE
DED

can EASILY be turned into a pendent of all diamonds by casting the spell on the center emerald.  Shortest solution is ONE move.  If your code can't solve this, than you have a significant problem.  Similarly, your code should detect that

EEE
EEE
EEE

is already a solution and requires no moves at all.

Similar "copy by value" vs. "copy by reference" problems exist in other language.  Make sure you pay attention to this issue.

Submitting Your Work

Prior to the deadline, use the homework submission system (http://math-cs.cns.uni.edu/~schafer/submit/which_course.cgi) to upload any files you have produced. 

You should bundle all of your files (including your readme.txt) into a zip file.

In addition to online copies, please submit hard copies of this lab at the start of class on the day this lab is due.