Você está na página 1de 4

Data Structures Assignment 2

(Backtracking using stack)


Due: Sep 2, 2010

This assignment is designed to get you comfortable with linked lists and
backtracking.

The plan is to start from your room and navigate a labyrinth made of rooms and
doors between them. The goal is to find friends in this labyrinth: you need to
find one buddy for each course you are taking. For each course, you want to
find a friend who lives near your room. Some rooms also have enemies -- you
never want to cross these rooms.

Your input is a set of rooms and a set of doors. There are also corridors, which
are like any other room, except no one lives in a corridor. This means you
should NEVER store any resident info for a corridor. Each line is in one of the
following four formats:

ROOM
FRIEND name:String numcourses:int course1:String course2:String .. cours
eN:String
ROOM
FOE name:String numcourses:int course1:String course2:String .. courseN:
String
CORRIDOR
DOOR room1:int room2:int

The words in red (and capital) appear verbatim. The other fields (shown in
bold) have a descriptive name. Their type (in italics) is listed here for clarity.
The input file will not have the :type part. The names are firs names (one word)
only. See the example at the end. While reading, if the word at the beginning of
a line is not one of the specified keywords, you should output:

BAD FORMAT ON LINE lineNumber:int

immediately after reading the line and ignore that line. If a door is specified
between room i and j, and if no such rooms exist even after reading the entire
input file, output:
BAD ROOM ON LINE lineNumber:int

and ignore the contents of that line. Note that all BAD FORMAT messages
come before BAD ROOM messages. Each category of error message is listed
in the order of line number of the error. A room's number is the number of
ROOM lines before it in the input file. As you read, for each room (including
corridors), build the list of neighboring rooms. Create an object for each room
you encounter. When a DOOR line is found, both rooms must add each other's
reference at the end of their respective neighbors lists. (Note that the room may
not have been read in yet.)

Following classes (and methods) are required.


class SinglyLinkedList<T> {
private T element;
private SinglyLinkedList<T> nextInList; // No accessor to set it
directly

public SinglyLinkedList()
{
element = null;
nextInList = null;
}
public void addAfter(T element) {
...
}

public void deleteAfter(T element) {


...
}
...
}

class SinglyLinkedListIterator<T> implements Iterator<T> {

private SinglyLinkedList<T> curr;

public SinglyLinkedListIterator(SinglyLinkedList<T> list) {


...
}
...
}

class Room {
private boolean isFriend;
private String resident;
private int number;
private SinglyLinkedList<Room> neighbors;
private SinglyLinkedList<String> courses;
...
}
Fill in the dots.
The input is in a file specified at the command line and the output will be to
Standard.out. The main class should be called FindBuddies and should be run
so:
java FindBuddies inputfilename
If the inputfile is not specified, output

NO INPUT FILE

and terminate the program.

If there is an exception reading the file, output

ERROR READING FILE filename:String

and terminate the program.

After the input file is read and the (Room) objects created, the search for
friends starts in the last ROOM of the file ("your room"). This means the last
line may not be a CORRIDOR and may not be a FOE. If it is, you should
report FATAL ERROR and quit.

You explore neighbors in the order of the neighbor-list. As you traverse


friendly rooms, you put the traversed room on a stack. If the occupant is the
only one found so far sharing some course with you, store that information as
well as the size of the stack: this is the "distance" to that friend's room. Do not
go into a room that has a FOE (i.e., do not put on the stack and do not traverse)
and do not go into a room that has already been explored. If you hav nowhere
further to go, backtrack: pop the stack, go back to the previous room and then
explore the next unexplored door. Once you have explored a path out of any
room, you never explore it again. In fact, once you pop a room, you never
explore it again.

While traversing, if a new friend is found for a course for which another friend
was found before, and the distance to the new room is less, this new friend is
stored for that course.

Do not use java's Stack. You should implement a linked list based generic stack
class called ListStack<T>, which should use SinglyLinkedList<T>.

Your output shows every new room your visited. This is when you access the
room from a neighbors list, verify it is a friend and traverse its course list. A
room is again visited when returning after visiting a neighbor (after
backtracking). Each visit is output on a line as one of the following:

DISCOVERED roomnumber:int distance:int


RETURNED TO roomnumber:int

Again :type part should not appear. After the traversal, the results are printed:
the name of the course-buddy, the room number and its distance:

FOUND
BUDDY course:String buddyName:String roomNumber:int distance:int

These lines are in the same order in which your courses were given (at the last
line of the input file). It is possible that the same friend is a buddy for multiple
courses. It is also possible to find no buddies for some course.

Example

INPUT FILE CONTAINS:


DOOR 1 3
DOOR 6 5
ROOM FRIEND Asimov 4 CS100 CS101 CE150 HU300
ROOM FRIEND Bart 3 CS100 CS101 CE150 HU300
ROOM FRIEND Prince 5 EE100 EE101 EE150 EE300
DOOR 0 5
SOMEJUNK
DOOR 4 1
ROOM FOE 12346 1 TX213
CORRIDOR
ROOM FRIEND Oster 2 HU300 CS200
DOOR 0 3
DOOR 2 1
OUTPUT IS:
BAD FORMAT ON LINE 7
BAD ROOM ON LINE 2
DISCOVERED 5 0
DISCOVERED 0 1
RETURNED 5
FOUND BUDDY HU300 Asimov 0 1
FOUND BUDDY CS100 Asimov 0 1

Você também pode gostar