Question

In: Computer Science

Need to program a maze traversal program using dynamic array and stacks. The problem is faced...

Need to program a maze traversal program using dynamic array and stacks. The problem is faced in setting up the data structure and due the problem in setting the data structure other functions like moving in all direction, marking the current cell that is visited are showing the error.

The work so far is below:

const int R = 5; //Number of rows
const int C = 5; //Number of columns

class coordinate
{
public:
int v;
int h;
};

class stack
{
public:
coordinate x;
coordinate y;
};

The maze looks like:

# # S _ #

_ _ _ # #

_ # # _ #

_ _ # # #

_ _ _ _ G

where S = starting point, G = end point, _ = space for moving around and # = wall

Solutions

Expert Solution

// Java program to solve maze traversal program
// problem with dynamic array and stacks
import java.util.Stack;
  
class Node
{
private int x, y;
private int dir;
  
public Node(int i, int j)
{
this.x = i;
this.y = j;
  
// default value for direction set to 0 (Up)
this.dir = 0;
}
  
public int getX()
{
return x;
}
  
public void setX(int x)
{
this.x = x;
}
  
public int getY()
{
return y;
}
  
public void setY(int y)
{
this.y = y;
}
  
public int getDir()
{
return dir;
}
  
public void setDir(int dir)
{
this.dir = dir;
}
}
  
public class MazeTravel
{
private static final int R = 5;
private static final int C = 5;
public static String out="";
  
// maze of n*m matrix
int n = R, m = C;
  
private static boolean[][] visited = new boolean[R][C];
  
// Driver code
public static void main(String[] args)
{
// Initially setting the visited
// array to true (unvisited)
setVisited(true);
  
// Maze matrix
char maze[][] = {{ '#', '#', 'S', '_', '#' },
{ '_', '_', '_', '#', '#' },
{ '_', '#', '#', '_', '#' },
{ '_', '_', '#', '#', '#' },
                       { '_', '_', '_', '_', 'G' }};
                      
       for (int k = 0; k < maze.length   ; k++)
{
for (int l = 0; l < maze[k].length; l++)
{
System.out.print(maze[k][l]+"\t");
}
System.out.println();
}
  
if (isReachable(maze))
{
System.out.println("Path Found!\n");
System.out.println(out.substring(0,out.length()-4)+"Goal");
  
}
else
System.out.println("No Path Found!\n");
}
  
private static void setVisited(boolean b)
{
for (int i = 0; i < visited.length; i++)
{
for (int j = 0; j < visited[i].length; j++)
{
visited[i][j] = b;
}
}
  
}
  
private static boolean isReachable(char maze[][])
{
// Initially starting at S.
  
int i=0, j=0;
       for (int k = 0; k < maze.length; k++)
{
for (int l = 0; l < maze[k].length; l++)
{
if(maze[k][l] == 'S'){
                   i=k;
                   j=l;
               }
}
}
System.out.println("Start coordinates: " + i + ", " + j);
// Goal coordinates
// Coordinates of Goal
int fx=R, fy=C;
       for (int k = 0; k < maze.length   ; k++)
{
for (int l = 0; l < maze[k].length; l++)
{
if(maze[k][l] == 'G'){
                   fx=k;
                   fy=l;
               }
}
}
System.out.println("Goal coordinates: " + fx + ", " + fy);
Stack<Node> s = new Stack<Node>();
  
Node temp = new Node(i, j);
  
out = out + "Start("+(i)+" ,"+(j)+") -> ";
  
s.push(temp);
  
while (!s.empty())
{
  
// Pop the top node and move to the
// left, right, top, down or retract
// back according the value of node's
// dir variable.
temp = s.peek();
int d = temp.getDir();
i = temp.getX();
j = temp.getY();
  
// Increment the direction and
// push the node in the stack again.
temp.setDir(temp.getDir() + 1);
s.pop();
s.push(temp);
  
// If we reach the Food coordinates
// return true
if (i == fx && j == fy)
{
return true;
}
  
if (d == 0)
{
// Checking the Up direction.
if (i - 1 >= 0 && (maze[i - 1][j] == '_' || maze[i - 1][j] == 'G') &&
visited[i - 1][j])
{
Node temp1 = new Node(i - 1, j);
visited[i - 1][j] = false;
out = out + "("+(i - 1)+" ,"+(j)+") -> ";
s.push(temp1);
}
}
else if (d == 1)
{
// Checking the left direction
if (j - 1 >= 0 && (maze[i][j-1] == '_' || maze[i][j-1] == 'G') &&
visited[i][j - 1])
{
Node temp1 = new Node(i, j - 1);
visited[i][j - 1] = false;
out = out + "("+(i)+" ,"+(j-1)+") -> ";
s.push(temp1);
}
}
else if (d == 2)
{
// Checking the down direction
if (i + 1 < R && (maze[i + 1][j] == '_' || maze[i + 1][j] == 'G') &&
visited[i + 1][j])
{
Node temp1 = new Node(i + 1, j);
visited[i + 1][j] = false;
out = out + "("+(i + 1)+" ,"+(j)+") -> ";
s.push(temp1);
}
}
else if (d == 3)
{
// Checking the right direction
if (j + 1 < C && (maze[i][j+1] == '_' || maze[i][j+1] == 'G') &&
visited[i][j + 1])
{
Node temp1 = new Node(i, j + 1);
visited[i][j + 1] = false;
out = out + "("+(i)+" ,"+(j+1)+") -> ";
s.push(temp1);
}
}
  
// If none of the direction can take
// the rat to the Food, retract back
// to the path where the rat came from.
else
{
visited[temp.getX()][temp.getY()] = true;
s.pop();
}
}
  
// If the stack is empty and
// no path is found return false.
return false;
}
}


Related Solutions

Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Description: One problem with dynamic arrays is that once the array is created using the new...
Description: One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This assignment asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have the following: • A private member variable called...
Why do we need a dynamic stack and How to implement a dynamic array stack? (...
Why do we need a dynamic stack and How to implement a dynamic array stack? ( Please answer in Java)
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to be implemented by you. Write a menu driven program that implements the following Binary Search Tree Operations FIND (item) INSERT (item) DELETE (item) DELETE_TREE (delete all nodes - be careful with the traversal!)
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
// JavaLanguage . You need to write a program that asks the user for an array...
// JavaLanguage . You need to write a program that asks the user for an array size, and then asks the user to enter that many integers. Your program will then print out the sum , average, largest and smallest of the values in the array.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT