Sei sulla pagina 1di 3

ECE 242 Project 1

Prof. Tilman Wolf and Prof. Lixin Gao Due: Thursday 9/22/11 at 11:30 p.m.
September 15, 1851 You are on your way to take part in the California Gold Rush. After spending weeks crossing the Plains, your biggest obstacle still lies ahead of you. You must cross the Sierra Nevada with its steep grades and dangerous passes before the rst snow blocks your way (and ends your journey in a shallow grave on the side of a mountain). You have a rough map of the area and your goal is to nd the fastest path. Going uphill and moving at high elevations takes you more time than going downhill and staying at low elevations. As you study your map, you feel this hard-to-describe envy of your descendants who, a 160 years later, will be able to use a stack-based Java implementation to solve this problem...

Introduction
In this project assignment, you will implement a program that can nd the best route between two points on a 2-dimensional map. The program will use a simple search method based on a stack data structure and the problem will be constrained so that it can be solved in a reasonable amount of time and with the techniques we currently have available. Note that real routing algorithms work dierently as you will learn later in the semester.

Overview
The project will use a map that is stored in an array to represent the terrain of a particular area. The terrain consists of water and land with dierent levels of elevation. The map also contains one start point and one end point. The goal of your project is to determine a path from start to end such that it requires least amount of time to traverse. Moving between neighboring elds on the map incurs some cost (e.g., time) depending on the elevation of the terrain (and you cannot move through water). One special constraint for our problem is that the starting point of the search is always to the right and up from the end point. (That is, on a map, where up is north, you always start in the northeast and go to the southwest.) In this project, a valid path can only takes steps toward the west or toward the south (i.e., you cannot go north or east in any step and thus you cannot oscillate between locations or create loops). An example of a map is shown below. The character values indicate elevation (with 0 representing water); S and E mark the starting point and end point, respectively. 001111S 0012223 0123323 0112212 1123222 E112211 In your project you will write a path nding algorithm that uses a stack data structure to record the path that has been taken so far and its cost. In each step, the algorithm decides which way to go next: west or south. The stack records this decision and the updated cost of the path and then proceeds from the next eld in the map. Whenever the end point is reached (or no valid moves are possible), the algorithm tracks back to the most recent decision and tries the other direction (if it has not been tried before) and continues 1

again until the end. Using this approach, eventually all valid paths will be explored. Your program should print a map of the terrain and report how many paths were explored, and which path is the least cost path and what its total cost is. (If there are multiple equal-cost solutions, you may report any one of them.) An example output for the map above is shown below. Note that when printing the map, dierent symbols were used to represent dierent types of terrain (e.g., for water). You may choose any combination of symbols that you nd visually appealing. The path is shown with . overlayed on the map. Map: ~~++++S ~~+OOO@ ~+O@@O@ ~++OO+O ++O@OOO E++OO++ PathSearch has completed, 366 paths have been explored, cost of best path is 14 Solution: ~~....S ~~.OOO@ ~..@@O@ ~.+OO+O ..O@OOO E++OO++

Details
Your program should work with any valid map. A valid map le contains character values of 0 (indicating water) or 1, 2, or 3 (indicating land with dierent heights). A valid map also contains exactly one starting point (marked by E) and one end point (marked by S). The end point is always south and west of the starting point. The elevation of the starting point and the end point is 1. Note that start and end points do not have to be in the corner of the map. Your program should use the le name of the map from the command line. To pass command line options to your program in Eclipse, go to Run->Run Congurations..., expand Java Application on the left to see the class that contains the main() function. Click on that class and select the Arguments tab on the right. Then, you can enter your command line arguments into the Programs arguments eld. You should implement a PathStack class that implements a stack where you can store path information (i.e., the elds that you have visited in your current path plus any other information you may need). The minimum set of functions for the PathStack class are push(), pop(), and isEmpty() (using appropriate arguments and return values). A path can only continue south or west from any given point. It is not possible to go south and west in a single step. A path cannot traverse any water (i.e., elevation value of 0). The cost of travel between neighboring elds depends on their elevation. Assuming hf rom represents the height of the eld from where the step is taken and hto represents the height of the eld to where the step is taken, then the cost for this step is c = max(hto , hf rom )+2max(hto hf rom, 0)+min(hto hf rom , 0). If there is no valid path, then your program should report so.

How to Start?
We are providing you with a Terrain class that implements a method to read the le that contains terrain information. The class stores the terrain information in a 2-dimensional array of characters. It also stores

the size of the terrain in each dimension. For Eclipse to nd the terrain le, it needs to be included in your project at the same level as the src folder (i.e., not inside the src folder, but outside of it). As a rst step, you may want to create a new class (e.g., PathFinder) that contains a main() function and that instantiates a new terrain object for a given le. Use the command line argument to obtain the le name that way you can run the program easily for dierent maps. As a second step, extend the Terrain class with a method for printing the terrain. This will allow you to get familiar with the structure of data within that class. Then, create the stack and think about what you want to store in the stack. Once you have a stack, you can start traversing the map. Make sure that your traversal algorithm explores all valid moves. At that point, you are close to nding a path and then the best path. For initial experiments, use a small map (and feel free to create your own maps to test certain scenarios). The search in the largest map (crossingTheSierras.txt) explores well over 100 million paths and has a running time in the order of a minute.

Grading
You should submit your complete code in a .zip le on SPARK. You will receive 75 points for your code running correctly and 25 points for the thoroughness and usefulness of the comments included in your code. The breakdown of points is: Program uses command line parameter to get map le name: 5 Program prints map correctly: 5 Implementation of the PathStack class: 15 Program nds and prints a valid path between start and end (using PathStack): 30 Printed path is best path: 10 Program reports correct number of paths explored: 5 Program reports if no path exists: 5 Comments describing your design decisions and how the code operates: 25 (Please see the Project page on the course website for further information on comments.) Note that all submissions must adhere to the course policies posted on the course web site.

Potrebbero piacerti anche