In either case, remember to delete each array that you allocate.

Similar documents
CS103L PA4. March 25, 2018

EE 352 Lab 3 The Search Is On

CS 103 The Social Network

CS 103 Six Degrees of Kevin Bacon

CS201 - Assignment 7 Due: Wednesday April 16, at the beginning of class

CS 103 Unit 11. Linked Lists. Mark Redekopp

(Refer Slide Time: 02.06)

EE 355 PA4 A* Is Born

Lab 9: Pointers and arrays

Lab 12: Lijnenspel revisited

CMPSCI 250: Introduction to Computation. Lecture #24: General Search, DFS, and BFS David Mix Barrington 24 March 2014

CS 2704 Project 3 Spring 2000

CS 103 Lab The Files are *In* the Computer

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Recitation 2/18/2012

CS 103 Unit 11. Linked Lists. Mark Redekopp

EE 355 OCR PA You Better Recognize

Carnegie Mellon. Cache Lab. Recitation 7: Oct 11 th, 2016

CS 103 Chroma Key. 1 Introduction. 2 What you will learn. 3 Background Information and Notes

Tutorial 1 C Tutorial: Pointers, Strings, Exec

EE355 Lab 5 - The Files Are *In* the Computer

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

CS201 Latest Solved MCQs

Java Review. Fundamentals of Computer Science

Data Types primitive, arrays, objects Java overview Primitive data types in Java

a f b e c d Figure 1 Figure 2 Figure 3

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Lecture 10 Notes Linked Lists

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

Search in discrete and continuous spaces

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

Programming Assignment Multi-Threading and Debugging 2

Lecture 10 Notes Linked Lists

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Autumn 2012 October 29 th, 2012 CS106X Midterm Examination Solution

Homework 7: Sudoku. Preliminaries. Overview

Blocky: A Game of Falling Blocks

Starting to Program in C++ (Basics & I/O)

HOT-Compilation: Garbage Collection

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Warm-up sheet: Programming in C

CS143 Handout 05 Summer 2011 June 22, 2011 Programming Project 1: Lexical Analysis

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Operating Systems and Networks Assignment 2

CS2255 HOMEWORK #1 Fall 2012

Lab 12 Lijnenspel revisited

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Project #1 Exceptions and Simple System Calls

The Maze Runner. Alexander Kirillov

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

CS106X Handout 17 Winter 2015 January 28 th, 2015 CS106X Practice Exam

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CS355 Hw 4. Interface. Due by the end of day Tuesday, March 20.

COP 3014: Spring 2019 Homework 6

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Programming assignment A

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Pointers and References

15-122: Principles of Imperative Computation, Fall 2015

Lecture Notes on Queues

hw6, BFS, debugging CSE 331 Section 5 10/25/12 Slides by Kellen Donohue

CS102: Variables and Expressions

COP Programming Assignment #7

ENGR 40M Project 3c: Switch debouncing

APCS-AB: Java. Recursion in Java December 12, week14 1

Creating a Shell or Command Interperter Program CSCI411 Lab

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Programming Assignment HW4: CPU Scheduling v03/17/19 6 PM Deadline March 28th, 2019, 8 PM. Late deadline with penalty March 29th, 2019, 8 PM

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

Grad AI Fall, 2007 Homework 1

Programming Assignment HW5: CPU Scheduling draft v04/02/18 4 PM Deadline April 7th, 2018, 5 PM. Late deadline with penalty April 9th, 2018, 5 PM

CS4023 Week06 Lab Exercise

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Mehran Sahami Handout #7 CS 106A September 24, 2014

Absolute C++ Walter Savitch

CSCI 204 Introduction to Computer Science II

Chapter 2. Procedural Programming

Pebbles Kernel Specification September 26, 2004

University of Colorado at Colorado Springs CS4500/ Fall 2018 Operating Systems Project 1 - System Calls and Processes

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CS 241 Data Organization using C Project 3: Tron Spring 2017

ITI Introduction to Computing II

C++ for Java Programmers

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Program Organization and Comments

CMPUT 396 Sliding Tile Puzzle

Lecture Transcript While and Do While Statements in C++

Eat (we provide) link. Eater. Goal: Eater(Self) == Self()

CSE 100: GRAPH ALGORITHMS

Programming Assignment 1 (PA1) - Display Bowtie

[2:3] Linked Lists, Stacks, Queues

Some major graph problems

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

Transcription:

CS 103 Path-so-logical 1 Introduction In this programming assignment you will write a program to read a given maze (provided as an ASCII text file) and find the shortest path from start to finish. 2 Techniques Used 1. Using file streams to perform text file I/O 2. Performing dynamic memory allocation of arrays and understanding 2D arrays. 3. Applying the backtracking Breadth-First-Search algorithm. 4. Implement and utilize a simple queue. 3 Background Information and Notes The input maze file format is as follows. The first line of the file contains two integer numbers indicating the row and column size of the maze. The number of rows indicated will determine how many lines of text follow (1 row per line). On each line will be one character for each of the indicated number of columns followed by a newline character. The characters can be a period (.) indicating a space in the maze, a # sign indicating a wall in the maze, an S indicating the start location for your search, or an F for the desired finish location. You can t go outside the grid. (I.e., you may think that walls surround the maze perimeter.) Sample Input File 4 4..#...#S F.#.... General File Format rows cols <col characters>\n... <col characters>\n Character Meaning. (period) Free space in the maze # Wall in the maze S Start location in the maze F Finish location in the maze Your search algorithm will find a shortest path from the start cell to the finish. Indicate this path by filling in the character locations on the path with asterisks (*); then, write the resulting character maze to an output text file. E.g., Output File 4 4..#...#S F*#*.*** Sometimes, no path exists. In this case your program will just output No path could be found! to the screen instead. Last Revised: 10/19/2014 1

Breadth-First Search (BFS): Breadth First Search is a general technique with many uses including flood filling, shortest paths, and meet-in-the-middle search. The idea is to explore every possible valid location, beginning at the start location, using an ordering so that we always explore ALL locations at a shorter distance from the start/source before exploring any location at a longer distance from the start/source (i.e. all locations at a distance of i are explored before any location at a distance of i+1 from the source). This property ensures that when we do find the finish cell, we ve arrived there via a shortest-length path. As we search we mark cells that we've explored so that we don't explore them again and so the search doesn t run forever. How do we ensure the BFS property (underlined above)? We keep a list of locations in the maze. Initially, we only list the start location. In each iteration, we remove the list s first location from the list, and we add all of its new neighbors to the end of the list. It is not too hard to prove 1 that this simple algorithm successfully implements the BFS. We keep going until either we hit the finish cell (found the shortest path), or until the list becomes empty (no path exists). This behavior, of adding items only to the end of the list, while deleting items only from the start of the list, means that we are implementing a data structure known as a queue. File I/O: You will use an ifstream object to read the maze data in from the given file and an ofstream object to write your results to an output file. Remember you can use the ifstream object like you do cin to read data into variables; and like cin, the ifstream object skips whitespace (including newline characters). By reading the number of rows and columns into two integers initially, you can determine how much input needs to be read. 2-D Array Allocation: You will not know the size of the maze until runtime, when you read the maze data. Thus we will need to dynamically allocate an array to hold the maze data. Remember that new by default can only allocate a 1D array. You will need to allocate some 2D arrays in this assignment. This can be done in two ways. Using new[] once to allocate a 1D array of pointers, then using a loop containing new[] to allocate many 1D arrays. See http://cs103.usc.edu/websheets/#nxmboard http://cs103.usc.edu/websheets/#deepnames for examples of that approach. (Click on View Reference Solution.) If the second array dimension is a constant that is known at compile time (like 2), you can use one line: int (*queue)[2] = new int[max_queue_size][2]; In either case, remember to delete each array that you allocate. 1 Each neighbor of a location at distance i from the start must be at distance i-1, i, or i+1 from the start. However, any new neighbor must be at distance i+1, since those at i-1 or i were already listed. And every location at distance i+1 has some neighbor at distance i. This is enough to finish the proof. 2 Last Revised: 10/19/2014

4 Requirements Your program shall meet the following requirements for features and approach: 1. Break your program into three files with the given structure: File Description / Function Definitions maze.cpp main(int argc, char *argv[]) Accepts the input filename and output filename as command line arguments Calls other functions int maze_search(char **maze, int rows, int cols) Performs the BFS search for a valid path, filling in the path with * characters if found Returns 1 if a valid path was found, 0 if no path was found, and -1 if an error occurred during the search. Possible errors include inability to find the start ( S ) cell or finish ( F ) cell. maze_io.h maze_io.cpp Prototypes for functions in maze_io.cpp Functions: char ** read_maze(char *filename, int *rows, int *cols ); Reads the maze from the given filename, allocates an array for the maze and returns it. The rows and cols arguments should point to variables that can be filled in with the dimensions of the maze read in from the file. void print_maze(char **maze, int rows, int cols); Prints the maze dimensions and maze contents to the screen in a two dimensional format void write_maze(char *filename, char **maze, int rows, int cols); Like print_maze, but writes to the given filename. 2. A valid path consists of steps north, south, east, and west but no diagonals. Thus we only need to explore neighbors of a cell in those 4 directions, not along diagonals. 3. You must dynamically allocate arrays to hold the maze data, the BFS array/queue, and any other needed arrays. 4. A queue is a data structure with the FIFO property (First-In, First-Out): items go in the back (or tail) but are removed from the front (head). It acts like a list where we write items at the bottom and cross them off as we do them from the top. It serves to help us remember what we have to do (until it is time to actually process it) and in what order the work arrived. To implement the queue s behavior let us allocate a large array (NROWS * NCOLS which is large enough to hold all items if we needed). We will also maintain two integer indices: head (the index of the front item) and tail (the index of the back item). Both should start at 0. When an item is added, it should be placed at the Last Revised: 10/19/2014 3

index specified by tail (i.e. 0 for the first item added) and then the tail index should be incremented (i.e. to 1). Another addition to the queue would cause the same behavior (placed at location specified by the tail index and then the tail index incremented). When an item is to be removed and processed, we should always take it from the front (head) index and then increment the head. Equivalently, you can think of tail as counting how many items were added so far, and head as counting how many items were removed. Note that when you delete from the queue, you do NOT move all the other items. You simply move the head counter forwards (leaving the old stuff sitting in its original location). Later in the course you ll learn how to optimize this. For example, here is a queue where, so far, two items were inserted and no items were removed: Head Tail 0 1 2 3 4 5 6 Item Item - - - - - Now, we must be careful not to try to remove an item if the list is empty. Notice that head and tail will be the same if everything that has been added has also been removed, and tail > head if it is nonempty. 5. Remember the earlier description of how to implement breadth-first search with a queue: BFS Algorithm add start location to BFSQ while BFSQ is not empty do front <- remove earliest item from BFSQ for each neighbor of front if neighbor is open square and unfound set predecessor of neighbor = front add neighbor to BFSQ Note that each item in the queue is a pair of numbers {row, col}. 6. You need to avoid adding any location to the queue more than once. Otherwise, your search can cycle infinitely or exceed the maximum queue size. Here is a bad way to solve this problem: before inserting a location into the queue, search the whole queue to see if it s already there. This is correct but has slow performance, O(NROWS 2 *NCOLS 2 ) worst-case time. Don t do this! Instead, maintain a data structure that remembers, for each grid cell, if it s already been added to the queue or not. Therefore, this data structure should let you look up, for a given pair of indices {row, col}, whether that cell has already 4 Last Revised: 10/19/2014

been visited, or not yet visited. What type of structure could you use for this? In your code, allocate and initialize this structure before you start the BFS algorithm. At what step of the BFS algorithm do you have to mark a cell as visited? 7. The final step is actually locating the optimal path and marking it with * characters. This requires a little more bookkeeping, like a trail of breadcrumbs that you can follow from the finish back to the start. In the BFS search, you should utilize a predecessor array (which is of the same size as the queue, and therefore dynamically allocated) to track, for each enqueued location L, the queue location (i.e. array ndex) of L s neighbor that caused L to be explored (the predecessor of L). The predecessor is utilized to find the actual path when your algorithm is complete: we trace it from the finish back to the start. I.e., the predecessor of the finish cell will tell us how to go back one step, then that cell s predecessor will tell us how to go back another step, etc: previous_cell = predecessor[current_cell] At what step of the algorithm will you fill a new entry in the predecessor array? 5 Prelab 1. BEFORE beginning, consider the sample maze shown below and put yourself in place of the BFS. Our examples will explore each cell s neighbors in the order {North, West, South, East}. Show the coordinates of each cell placed in the BFSQ in the appropriate order from the Start node and stopping as soon as the Finish node is entered into the queue. Then for each cell placed in the BFS queue, keep track of how the predecessor array would be updated. Show the predecessor array s FINAL value at the end of the BFS execution. We have started the example for you. Complete it on a separate page. Maze 0123 <- Col. Index Locations Row index -> 0..#. 0,0 0,1 0,2 0,3 1..#S 1,0 1,1 1,2 1,3 2 F.#. 2,0 2,1 2,2 2,3 3... 3,0 3,1 3,2 3,3 BFSQ: (index) 0 1 2 3 value {1, 3}, {0, 3}, {2, 3}, {3, 3} PRED: -1 0 0 2 2. Next, do the backtracking on this example. Which BFSQ index did the F cell (finish) correspond to? If you backtrack from there, what indices do you pass through? What path through the maze does this correspond to? 3. Fill in the readme.txt file prelab, which asks you about your design of the structure that you use to remember which cells have been visited or not. 6 Procedure Perform the following. Last Revised: 10/19/2014 5

1. Create a maze directory in your VM (or wherever you prefer) $ mkdir ~/maze $ cd ~/maze 2. Download the sample mazes and skeleton code to your local directory: $ wget http://ee.usc.edu/~redekopp/cs103/maze.tar This will download the maze_io header file, Makefile, & 3 sample mazes: maze_io.h Prototypes for the maze file/display I/O maze_io.cpp Code for the I/O routines maze.cpp - Main program and maze_search() algorithm Makefile - Run make to compile your program maze1.in Sample 5 x 5 maze maze2.in Sample 10 x 12 maze maze3.in - Sample maze with no solution path maze4.in Sample maze with an error (no S cell) Checkpoint 1: Input/output: 3. Complete the maze_io.cpp code so that you can read/print/write mazes to and from files. In main() in maze.cpp, write a program that simply reads in the file specified on the command line, prints it to the screen, and writes it out to the specified filename (i.e. the output file should thus be a copy of the input file). 4. Compile and run your program: $ make $./maze maze1.in maze1.copy 5. Check that the printed output and output file look correct. It is okay if your program prints Path successfully found! to the screen. Did you remember to deallocate the dynamic memory? Fill in the ADD CODE HERE part. Checkpoint 2: Core BFS algorithm: 6. Now write the search code in maze_search() a. Find the Start and Finish cells b. Setup and initialize your BFSQ, Predecessor and any other arrays/data structures necessary. c. Perform the Breadth First Search until you find the finish cell or the BFSQ is empty. 7. Does your program successfully distinguish mazes where a path exists, from ones where a path does not? Try it on this input (maze3.in): 2 3 F#..#S If need be, print out the location of each item you add or remove to the queue for debugging purposes, and print out the r,c index of the Finish cell when found. Final Product: 1. Now add the code to walk the predecessor array backwards to the start location filling in the cells with * 2. Return the status code: 1=success, 0=no shortest path exists, -1=error 3. Make sure your code meets all the requirements given earlier in this handout. 4. Make sure all dynamically allocated memory is deallocated. 6 Last Revised: 10/19/2014

5. Comment your code as you write your program documenting what abstract operation a certain for, while, or if statement is performing as well as other decisions you make along the way that feel particular to your approach. 6. Compile your program: $ make 7. Run your program: $./maze maze1.in maze1.out 8. Verify your program s outputs and run the program again on the other sample cases. 9. Create your own sample input mymaze.txt that tests if the algorithm is actually returning a shortest path? So your test case should have multiple paths from start to finish, of different lengths. Once you ve verified that your program behaves as expected, upload it along with your submission. 7 Rubric 3 points: Readme completed; get it from http://cs103.usc.edu/files/pa3/readme.txt 5 points: Follows required API and division of functionality into files, has correct return values 5 points: Opens files, reads input and prints output correctly 7 points: Allocates and deallocates memory correctly 10 points: BFS functionality: Finds start, uses queue insert-and-delete algorithm, checks all 4 neighbors, stays in bounds, updates predecessor array, correctness 4 points: Backtracks, draws asterisks through maze along shortest path 4 points: Style and documentation 2 points: Submitted mymaze.txt Last Revised: 10/19/2014 7