Write Java program that implements the A* algorithm to find a path from any two
ID: 3845916 • Letter: W
Question
Write Java program that implements the A* algorithm to find a path from any two given nodes. Problem Overview & Algorithm Description The agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan method for calculating the heuristic. Your environment should be a 15x15 tile-based world that randomly generates nodes that are unpathable (blocks) in This should be done each time the program compiles ensuring that there are different environment makeups each run. 10% of the nodes The program should display the generated environment when the program runs The Program should allow the user to select a starting node and goal node Once the start and goal nodes have been defined, the program should run the A* algorithm to find a path In a fully-observable environment where there are both pathable and blocked nodes, The path should be displayed (series of [x,y] nodes, highlighting nodes, or actually moving the agent) if one exists, or a message indicating that a path could not be found. The user should be able to continue specifying starting and goal nodes after paths have been found.
Explanation / Answer
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/** Class KnuthMorrisPratt **/
public class KnuthMorrisPratt
{
/** Failure array **/
private int[] failure;
/** Constructor **/
public KnuthMorrisPratt(String text, String pat)
{
/** pre construct failure array for a pattern **/
failure = new int[pat.length()];
fail(pat);
/** find match **/
int pos = posMatch(text, pat);
if (pos == -1)
System.out.println(" No match found");
else
System.out.println(" Match found at index "+ pos);
}
/** Failure function for a pattern **/
private void fail(String pat)
{
int n = pat.length();
failure[0] = -1;
for (int j = 1; j < n; j++)
{
int i = failure[j - 1];
while ((pat.charAt(j) != pat.charAt(i + 1)) && i >= 0)
i = failure[i];
if (pat.charAt(j) == pat.charAt(i + 1))
failure[j] = i + 1;
else
failure[j] = -1;
}
}
/** Function to find match for a pattern **/
private int posMatch(String text, String pat)
{
int i = 0, j = 0;
int lens = text.length();
int lenp = pat.length();
while (i < lens && j < lenp)
{
if (text.charAt(i) == pat.charAt(j))
{
i++;
j++;
}
else if (j == 0)
i++;
else
j = failure[j - 1] + 1;
}
return ((j == lenp) ? (i - lenp) : -1);
}
/** Main Function **/
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Knuth Morris Pratt Test ");
System.out.println(" Enter Text ");
String text = br.readLine();
System.out.println(" Enter Pattern ");
String pattern = br.readLine();
KnuthMorrisPratt kmp = new KnuthMorrisPratt(text, pattern);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.