Question

Java maze Create a grid of #’s and dots(.) using a two dimensional array representation of...

Java maze

Create a grid of #’s and dots(.) using a two dimensional array representation of a maze that looks like this:

############

#...#......#

..#.#.####.#

###.#....#.#

#....###.#..

####.#.#.#.#

#..#.#.#.#.#

##.#.#.#.#.#

#........#.#

######.###.#

#......#...#

#’s represent the walls of the maze, and the dots represent locations in the possible paths through the maze. A move can be made only to a location in the array that contains a dot.

You can use any maze formation you wish, but there must be a starting point and exit, with a path to each with some dead paths along the way.

Write a program that contains a recursive method (mazeTraversal) to walk through the maze. The method should receive as arguments a 12 by 12 character array representing the maze and the current location in the maze. (The first time this method is called, it should receive the entry point in the maze. In this example, it would be 2,0). As mazeTraversal attempts to locate the exit, it should place the character x in each square in the path, which is used as a breadcrumb to indicate where it has been.

The algorithm: From the current location in the maze, try to move one space in any of the possible directions(down, right, up or left). If it’s possible to move in at least one direction, call mazeTraversal recursively, passing the new spot on the maze as the current spot. If it’s not possible to go in any direction, “back up” to a previous location in the maze and try a new direction for that location (this is an example of recursive backtracking). Program the method to display the maze after each move so the user can watch as the maze is solved. The final output of the maze should display only the path needed to solve the maze---If going in a particular direction results in a dead end, the x’s going in that direction should not be displayed. [Hint: To display only the final path, it may be helpful to mark off spots that result in a dead end with another character.

Homework Answers

Answer #1

# Maze size 
N=12

# A utility function to print solution matrix sol 
def printSolution( sol ): 
        
        for i in sol: 
                for j in i: 
                        print(str(j) + " ", end ="") 
                print("") 

# A utility function to check if x, y is valid 
# index for N * N Maze 
def isSafe( maze, x, y ): 
        
        if x >= 0 and x < N and y >= 0 and y < N and maze[x][y] == 1: 
                return True
        
        return False

""" This function solves the Maze problem using Backtracking. 
        It mainly uses solveMazeUtil() to solve the problem. It 
        returns false if no path is possible, otherwise return 
        true and prints the path in the form of 1s. Please note 
        that there may be more than one solutions, this function 
        prints one of the feasable solutions. """
def solveMaze( maze ): 
        
        
        sol = [ [ 0 for j in range(12) ] for i in range(12) ] 
        
        if solveMazeUtil(maze, 0, 0, sol) == False: 
                print("Solution doesn't exist"); 
                return False
        
        printSolution(sol) 
        return True
        
# A recursive utility function to solve Maze problem 
def solveMazeUtil(maze, x, y, sol): 
        
        # if (x, y is goal) return True 
        if x == N - 1 and y == N - 1 and maze[x][y]== 1: 
                sol[x][y] = '.'
                return True
                
        # Check if maze[x][y] is valid 
        if isSafe(maze, x, y) == True: 
                # mark x, y as part of solution path 
                sol[x][y] = '.'
                
                # Move forward in x direction 
                if solveMazeUtil(maze, x + 1, y, sol) == True: 
                        return True
                        
                # If moving in x direction doesn't give solution 
                # then Move down in y direction 
                if solveMazeUtil(maze, x, y + 1, sol) == True: 
                        return True
                
                # If none of the above movements work then 
                # BACKTRACK: unmark x, y as part of solution path 
                sol[x][y] = '!'
                return False


# Initialising the maze 
m=[['#','#','#','#','#','#','#','#','#','#','#','#']

['#','.','.','.','#','.','.','.','.','.','.','#'],

['.','.','#','.','#','.','#','#','#','#','.','#'],

['#','#','#','.','#','.','.','.','.','#','.','#'],

['#','.','.','.','.','#','#','#','.','#','.','.'],

['#','#','#','#','.','#','.','#','.','#','.','#'],

['#','.','.','#','.','#','.','#','.','#','.','#'],

['#','#','.','#','.','#','.','#','.','#','.','#'],

['#','.','.','.','.','.','.','.','.','#','.','#'],

['#','#','#','#','#','#','.','#','#','#','.','#'],

['#','.','.','.','.','.','.','#','.','.','.','#']]
rows=len(m)
cols=len(m[0])
maze=[][]
for i in range(rows):
  for j in range(cols):
    if m[i][j]=='#':
      maze[i][j]=0
    else:
      maze[i][j]=1
    
                        
solveMaze(maze) 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Question 5 0/1 point (graded) The following is the full path to a homework assignment file...
Question 5 0/1 point (graded) The following is the full path to a homework assignment file called "assignment.txt": /Users/student/Documents/projects/homeworks/assignment.txt. Which line of code will allow you to move the assignment.txt file from the “homeworks” directory into the parent directory “projects”? mv assignment.txt mv assignment.txt . mv assignment.txt .. mv assignment.txt /projects incorrect Answer Incorrect: Try again. This code does not provide enough information about where to move the file. You need to specify a relative or full path to the...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
Laboratory 4: Black Jack! Java API ArrayList Lab 04 Documentation Included matrix package "external" documentation Included...
Laboratory 4: Black Jack! Java API ArrayList Lab 04 Documentation Included matrix package "external" documentation Included Download Lab04.zip for all of the additional supporting files that you will need to compile and run. Extract the files into your working directory You can find the rules of blackjack all over the Internet. To get an idea of what you are trying to accomplish in this lab, I’ll demonstrate the final solution. The dealer stands on all 17s. Doubling after splitting is...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
1-How important do you believe it is to be task-oriented and relationship-orientated? How would you describe...
1-How important do you believe it is to be task-oriented and relationship-orientated? How would you describe Musk? 2-Why do you think Musk is successful in overcoming obstacles and handling conflict? How do you overcome obstacles in influencing individuals in your life? 3-Besides the personality traits given in chapter 2, what other traits attribute to a great leader? Recognizing Your Traits The definition of intelligence is the ability to acquire and apply knowledge and skills. Elon Musk knew little about rockets...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect on what happens to the families when there is a suicide in the family, based on the Seasons of Love chapter...how should people be told? What details are best left unshared? below is the story These theories may have a certain face-validity, but they often neglect environmental or contextual factors that are innate to answering the question of “why” a person might engage in...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT