Prev: P4 Next: P6
Back to week 6 page: Link


# P5 Programming Problem Instruction

📗 Enter your ID (the wisc email ID without @wisc.edu) here: and click (or hit the "Enter" key)
📗 You can also load from your saved file
and click .
📗 If the questions are not generated correctly, try refresh the page using the button at the top left corner.
📗 The official deadline is July 31, late submissions within two weeks will be accepted without penalty, but please submit a regrade request form: Link.
📗 The same ID should generate the same set of questions. Your answers are not saved when you close the browser. You could either copy and paste or load your program outputs into the text boxes for individual questions or print all your outputs to a single text file and load it using the button at the bottom of the page.
📗 Please do not refresh the page: your answers will not be saved.
📗 You should implement the algorithms using the mathematical formulas from the slides. You can use packages and libraries to preprocess and read the data and format the outputs. It is not recommended that you use machine learning packages or libraries, but you will not lose points for doing so.
📗 Please report any bugs on Piazza: Link

# Warning: please enter your ID before you start!



📗 (Introduction) In this programming homework, you create a random maze and compare multiple uninformed and informed search algorithms.

📗 (Part 1) Create a \(h\) = ? by \(w\) = ? (height by width) rectangular orthogonal maze by parsing an image from the the Maze Generator: Link. You can also generate a random maze yourself using Recursive Backtracker or one of the Minimum Spanning Tree algorithms (Kruskal or Prim): Wikipedia or Link. Make sure you enter from the center square of the top row and exit from the center square of the bottom row.
An example:
You are allowed to use this example for your Question 1, it is generated using Recursive Backtracker. The advantage of creating your own maze is that you can get your successor matrix and the solution at the same time you create the maze.

📗 (Part 1) Plot the maze using | for vertical walls and -- for horizontal walls and + for intersections. Use @ to highlight the solution (not #). Below is an 15 by 15 example of such plot and the solution.
Example:
Solution:

📗 (Part 1) Use BFS and DFS to get the solution of the maze. Keep track of the squares you searched (expanded, or checked goal on).

📗 (Part 2) Use A* with the heuristic of Manhattan distance to the goal and Euclidean distance to the goal. Keep track of the squares you searched (expanded, or checked goal on).

# Question 1 (Part 1)

📗 [2 points] Enter the plot of the maze (either copy from the example or use your own).




# Question 2 (Part 1)

📗 [5 points] Enter the successor matrix of the maze (\(h\) lines, each line containing \(w\) strings, comma separated, each string represents the list of possible successors, a subset of the characters "U" "D" "L" "R").
Hint 📗 For each square \(\left(i, j\right)\) centered at \(\left(2 i + 1, 3 j + 1\right)\) and \(\left(2 i + 1, 3 j + 2\right)\) (i is the line index, j is the character index in the line), start with "": if \(\left(2 i, 3 j + 1\right)\) is " ", add "U", if \(\left(2 i + 2, 3 j + 1\right)\) is " ", add "D", if \(\left(2 i + 1, 3 j\right)\) is " ", add "L"; if \(\left(2 i + 1, 3 j + 3\right)\) is " ", add "R".
📗 You should use the successor matrix to solve the maze instead of the plot.




# Question 3 (Part 1)

📗 [5 points] Enter the action sequence (one line, a sequence of characters "U" "D" "L" "R", no comma in between them). 
Hint 📗 Do not start or finish outside the maze: start inside the center square of the top row.




# Question 4 (Part 1)

📗 [5 points] Enter the plot of the maze with the solution (use the same format as the example).




# Question 5 (Part 1)

📗 [5 points] Enter the list of squares searched by BFS (\(h\) lines, each line containing \(w\) integers either 0 or 1, 1 means searched, comma separated).
Hint 📗 Use a Queue of pairs of numbers as the Queue. You can use an array or list too, but you have to implement the "enQueue" and "deQueue" operations first.
📗 Start by putting the entry square (center square of the top row) in the Queue.
📗 DeQueue \(\left(i, j\right)\) and find the string of the \(\left(i, j\right)\) element of the successor matrix: if it contains "U", enQueue \(\left(i - 1, j\right)\), if it contains "D", enQueue \(\left(i + 1, j\right)\), if it contains "L", enQueue \(\left(i, j - 1\right)\); if it contains "R", enQueue \(\left(i, j + 1\right)\).
📗 Repeat until the exit square (center square of the bottom row) is deQueued.






# Question 6 (Part 1)

📗 [5 points] Enter the list of squares searched by DFS (\(h\) lines, each line containing \(w\) integers either 0 or 1, 1 means searched, comma separated). Expand the successors in the order "U", "D", "L", "R" in case auto-grader does not accept other variations.
Hint 📗 Use a Stack of pairs of numbers as the Stack. You can use an array or list too, but you have to implement the "Pop" and "Push" operations first.
📗 Start by putting the entry square (center square of the top row) in the Stack.
📗 Pop \(\left(i, j\right)\) and find the string of the \(\left(i, j\right)\) element of the successor matrix: if it contains "U", push \(\left(i - 1, j\right)\), if it contains "D", push \(\left(i + 1, j\right)\), if it contains "L", push \(\left(i, j - 1\right)\); if it contains "R", push \(\left(i, j + 1\right)\).
📗 Repeat until the exit square (center square of the bottom row) is popped.






# Question 7 (Part 2)

📗 [5 points] Enter the Manhattan distances to the goal for each square in the maze (\(h\) lines, each line containing \(w\) integers, comma separated).
Hint 📗 The Manhattan distance between \(\left(i, j\right)\) and \(\left(i', j'\right)\) is \(\left| i - i' \right| + \left| j - j' \right|\).




# Question 8 (Part 2)

📗 [5 points] Enter the list of squares searched by A* with Manhattan distance to the goal as the heuristic (\(h\) lines, each line containing \(w\) integers either 0 or 1, 1 means searched, comma separated).
Hint 📗 Use a PriorityQueue of pairs of numbers as the Queue. You can use an array or list too, but you have to implement the "enQueue" and "deQueue" operations first. Since the mazes are relatively small, you do not have to implement efficient PriorityQueue algorithms.
📗 Start by putting the entry square (center square of the top row) with priority = cost + heuristic, where the cost is \(g = 0\), and the heuristic is \(h = H\left(i, j\right)\) (\(H\) is the matrix from the previous question) in the Queue.
📗 DeQueue \(\left(i, j\right)\) with cost \(c\) and find the string of the \(\left(i, j\right)\) element of the successor matrix: if it contains "U", enQueue \(\left(i - 1, j\right)\) with and , if it contains "D", enQueue \(\left(i + 1, j\right)\) with and , if it contains "L", enQueue \(\left(i, j - 1\right)\) with and ; if it contains "R", enQueue \(\left(i, j + 1\right)\) with and .
📗 Repeat until the exit square (center square of the bottom row) is deQueued.






# Question 9 (Part 2)

📗 [5 points] Enter the list of squares searched by A* with Euclidean distance to the goal as the heuristic (\(h\) lines, each line containing \(w\) integers either 0 or 1, 1 means searched, comma separated).
Hint 📗 Replace the Manhattan distance by Euclidean distance \(\sqrt{\left(i - i'\right)^{2} + \left(j - j'\right)^{2}}\).






# Question 10

📗 [1 points] Please confirm that you are going to submit the code on Canvas under Assignment P5, and make sure you give attribution for all blocks of code you did not write yourself (see bottom of the page for details and examples).
I will submit the code on Canvas.

# Question 11

📗 [1 points] Please enter any comments and suggestions including possible mistakes and bugs with the questions and the auto-grading, and materials relevant to solving the question that you think are not covered well during the lectures. If you have no comments, please enter "None": do not leave it blank.
📗 Answer: .

# Grade


 ***** ***** ***** ***** ***** 

 ***** ***** ***** ***** *****

# Submission


📗 Please do not modify the content in the above text field: use the "Grade" button to update.
📗 Warning: grading may take around 10 to 20 seconds. Please be patient and do not click "Grade" multiple times.


📗 You could submit multiple times (but please do not submit too often): only the latest submission will be counted. 
📗 Please also save the text in the above text box to a file using the button or copy and paste it into a file yourself . You can also include the resulting file with your code on Canvas Assignment P5.
📗 You could load your answers from the text (or txt file) in the text box below using the button . The first two lines should be "##p: 5" and "##id: your id", and the format of the remaining lines should be "##1: your answer to question 1" newline "##2: your answer to question 2", etc. Please make sure that your answers are loaded correctly before submitting them.



📗 Saving and loading may take around 10 to 20 seconds. Please be patient and do not click "Load" multiple times.

# Solutions

📗 The sample solution in Java and Python will be posted around the deadline. You are allowed to copy and use parts of the solution with attribution. You are allowed to use code from other people (with their permission) and from the Internet, but you must and give attribution at the beginning of the your code. You are allowed to use large language models such as GPT4 to write parts of the code for you, but you have to include the prompts you used in the code submission. For example, you can put the following comments at the beginning of your code:
% Code attribution: (TA's name)'s P5 example solution.
% Code attribution: (student name)'s P5 solution.
% Code attribution: (student name)'s answer on Piazza: (link to Piazza post).
% Code attribution: (person or account name)'s answer on Stack Overflow: (link to page).
% Code attribution: (large language model name e.g. GPT4): (include the prompts you used).
📗 Solution:
📗 Java: Link
📗 Python: Link
📗 Sample solution from last year: 2022 P5. The homework is slightly different, please use with caution.
📗 You can get help on understanding the algorithm from any of the office hours; to get help with debugging, please go to the TA's office hours. For times and locations see the Home page. You are encouraged to work with other students, but if you use their code, you must give attribution at the beginning of your code.





Last Updated: April 29, 2024 at 1:11 AM