Question

In: Mechanical Engineering

********JAVA************ This program deals with graphs and path-finding within them. write a program which allows the...

********JAVA************

This program deals with graphs and path-finding within them.
write a program which allows the user to enter multiple levels of a text, consisting of a
'start', an 'exit', 'walls', and 'floors'. The letter S represents the start. E is the exit. O (the letter 'oh') represents a
'floor' (meaning a path you can follow). X is a wall (meaning a block you cannot pass).
The program will accept Standard Input (System.in), It is up to you whether you wish to use an adjacency matrix or adjacency lists (linked or array-based).
Ideally, the program will allow the user to find the 'fastest' path through the maze. In this context, 'fastest'
means the fewest number of steps possible to get from the start to the exit, without crossing a wall. Note: You
may only move North, South, East, West, Up, or Down (diagonal travel is not allowed).
If no valid path exists, the program should say so. You may assume 'sane' input, and that a start and exit are
present (though there might not be a path connecting them). The maps do not 'wrap' (i.e. you can't exit the left
edge to appear on the right edge), and you may not assume that a floor will have a boundary of walls.
Specifically, the input will be as follows:
An integer for the width of the map (W)
An integer for the height (length) of the map (H)
An integer for the depth (number of layers) of the map (D)
D×H lines of W characters (from {X,O,S,E}), terminated with newlines
For D layers, first the (H) lines of the top layer will be entered, then the lines of the second-highest, etc.
In place of any line of W characters, it is legal to include an entirely blank line (as such, any line will have W
characters followed by a \n, or consist solely of a \n. the code must be able to ignore such blank lines, but
may never rely on their presence; an input might not have any blank lines at all).

output should look like this:

Enter width: 8
Enter height: 5
Enter depth: 1
Enter maze below. Only rows of width 8 will be accepted.
XXXOOOOX
OXXXOXOE
OXXOOXXO
OSXOXXXO
XOOOXOOO
1. Solve suboptimally
2. Estimate optimal solution cost
3. Solve optimally
4. Enter new puzzle
5. Quit
:> 1
Not implemented. Use optimal instead.
1. Solve suboptimally
2. Estimate optimal solution cost
3. Solve optimally
4. Enter new puzzle
5. Quit
:> 3
Finding Solution...
Optimal Path Cost: 12
Optimal Path:
S E E N N E N N E E S E

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;

void addEdge(vector<int> adj[], int u, int v)
{
   adj[u].push_back(v);
   adj[v].push_back(u);
}

void printGraph(vector<int> adj[], int V)
{
   for (int v = 0; v < V; ++v)
   {
       cout << "\n Adjacency list of vertex "
           << v << "\n head ";
       for (auto x : adj[v])
       cout << "-> " << x;
       printf("\n");
   }
}


int main()
{
   int V = 5;
   vector<int> adj[V];
   addEdge(adj, 0, 1);
   addEdge(adj, 0, 4);
   addEdge(adj, 1, 2);
   addEdge(adj, 1, 3);
   addEdge(adj, 1, 4);
   addEdge(adj, 2, 3);
   addEdge(adj, 3, 4);
   printGraph(adj, V);
   return 0;
}


Related Solutions

Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
in Java, write a program that takes an input of 4 numbers and splits them into...
in Java, write a program that takes an input of 4 numbers and splits them into 4 separate lines. EXAMPLE: so if input is 1994 the output should be 1 9 9 4
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit to degrees Celsius. The program should prompt for a temperature in Fahrenheit and output a temperature in Celsius. All calculations should be done in in ints, so be careful of truncation.
In Java Write a program that allows Professor Poindexter to get class averages including the average...
In Java Write a program that allows Professor Poindexter to get class averages including the average for allher/his classes. Professor Poindexter has M classes. M will be input from the terminal. For each class he/she will input each student’s grade for that class until 0 is input. Once the goof prof inputs 0, The class average for that class is output in the form:   Average for Class X 1 is XXX.XX Finally, once all the M class data is input,...
Write a short, java program that interacts with the user asking them to buy an xbox...
Write a short, java program that interacts with the user asking them to buy an xbox series x or PS5 and does the following: create at least 2 double variables create at least 1 constant get at least 1 string input from the user get at least 1 int/double input from the user include both, incorporating the input you got from the user: 1 multiway if-else with at least 3 "else if" 1 nested if Your program should combine all...
JAVA Exercise BasketBall Bar Chart Write a program that allows you to create a bar chart...
JAVA Exercise BasketBall Bar Chart Write a program that allows you to create a bar chart for the active members of a basketball team during a game. (Recall: there are only 5 active players on the court per team in a standard basketball game.) Your program should do the following tasks: • Prompt the user to store the first name of the five players • Prompt the user to enter the points scored by each player a. Use a while...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT