Question: Modify that code so that it prints multiple paths, one for each exit. You will have to modify the maze so that it has more

Modify that code so that it prints multiple paths, one for each exit. You will have to modify the maze so that it has more than one exit. Make sure not to print solutions within the 'solve' method, but rather return the list of solutions.

Hint: When an exit is found, don't return "true". Instead, add the current path to a list of paths. Pass in a new list which will contain the paths that were found. It will be empty at first. Since each path is a list of integers, the path list will be a list of integer lists. Don't use any global (i.e., static) variables.

Provide Screenshot of the code working.

line 91 and line 95 are giving me a error

line 91 "The operator - is undefined for the argument type(s) String, char"

line 95 ans cannot be resolved

import static java.lang.System.out; import java.util.ArrayList; import java.util.List;

class GraphNode {

int id; List children = new ArrayList<>();

boolean isExit = false;

public GraphNode(int id) { this.id = id;

}

@Override public String toString() {

return ((Integer) id).toString();

}

}

class ch15 {

public static void main(String[] args) {

List nodes = new ArrayList<>();

for (int i = 0; i < 20; i++) nodes.add(new GraphNode(i + 1));

nodes.get(15).isExit = true;

connect(nodes, 1, 2); connect(nodes, 2, 3); connect(nodes, 3, 4); connect(nodes, 4, 5); connect(nodes, 6, 7); connect(nodes, 7, 8); connect(nodes, 8, 9); connect(nodes, 9, 10); connect(nodes, 10, 11); connect(nodes, 11, 12); connect(nodes, 12, 13); connect(nodes, 13, 14); connect(nodes, 14, 15); connect(nodes, 15, 16); connect(nodes, 16, 17); connect(nodes, 17, 18); connect(nodes, 18, 19); connect(nodes, 19, 20);

List visited = new ArrayList<>();

List path = new ArrayList<>(); ArrayList> allpath = new ArrayList<>();

boolean result = solve(nodes.get(2), visited, path, allpath, "");

if (result) {

out.println("Exit has been found");

for (GraphNode pathNode : path)

out.print(pathNode.id + " ");

} else

out.print("Exit has not been found");

}

private static boolean solve(GraphNode node, List visited, List path, ArrayList> allpath, String asf) {

out.print(node.id + " ");

visited.add(node); asf = asf + node.id + " "; path.add(node);

if (node.isExit) { String[] arr = asf.split("s+"); ArrayList tempans = new ArrayList<>(); for (int i = 0; i < arr.length; i++) { int a = (int) (arr[i] - '0'); tempans.add(a); }

ans.add(tempans);

return true;

}

for (GraphNode child : node.children)

if (!visited.contains(child))

if (solve(child, visited, path, allpath, asf)) return true;

path.remove(path.size() - 1); return false;

}

private static void connect(List nodes, int nodeId1, int nodeId2) {

nodes.get(nodeId1 - 1).children.add(nodes.get(nodeId2 - 1)); nodes.get(nodeId2 - 1).children.add(nodes.get(nodeId1 - 1));

} }

Step by Step Solution

3.51 Rating (154 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

java import static javalangSystemout import javautilArrayList import javautilList class GraphNode int id List children new ArrayList boolean isExit false public GraphNodeint id thisid id Override publ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!