Answered step by step
Verified Expert Solution
Question
1 Approved Answer
JAVA modify the following program as described: For each generated walk, print the array as a table having a border made by #. Print space
JAVA
modify the following program as described:
- For each generated walk, print the array as a table having a border made by #.
- Print space if a cell was not visited and * for a cell that was visited.
- Print if the walk was a dead end or an escape
(for each walk, stimulate the walk with # that are the borders of each walk and inside it print * if the intersection was visited and print a space if it wasn't visited. for each walk print if it is a dead end or not).
public class SelfAvoidingWalk { public static void main(String[] args) { int n = Integer.parseInt(args[0]); // lattice size int trials = Integer.parseInt(args[1]); // number of trials int deadEnds = 0; // trials resulting in a dead end // simulate trials self-avoiding walks for (int t = 0; t < trials; t++) { boolean[][] a = new boolean[n][n]; // intersections visited int x = n/2, y = n/2; // current position // repeatedly take a random step, unless you've already escaped while (x > 0 && x < n-1 && y > 0 && y < n-1) { // dead-end, so break out of loop if (a[x-1][y] && a[x+1][y] && a[x][y-1] && a[x][y+1]) { deadEnds++; break; } // mark (x, y) as visited a[x][y] = true; // take a random step to unvisited neighbor double r = Math.random(); if (r < 0.25) { if (!a[x+1][y]) x++; } else if (r < 0.50) { if (!a[x-1][y]) x--; } else if (r < 0.75) { if (!a[x][y+1]) y++; } else if (r < 1.00) { if (!a[x][y-1]) y--; } } } System.out.println(100*deadEnds/trials + "% dead ends"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started