Question: imulate how many steps its take a random walker starting at the center of an 10x10 grid to visit every cell of the grid. If

imulate how many steps its take a random walker starting at the center of an 10x10 grid to visit every cell of the grid. If the walker tries to go outside of the grid then it doesn't move in that step.

Write a java program which simulates th steps of the random walker, and keeps hold about the grid with 2D boolean array and write out the steps when the random walker have walked all the cells.

(I have asked this question two times now here: And I always get answer where someone post the program DemoWalker. That program doesn't work and is wrong answer. Can you please at least not post that program to my question! Would be lovely!). Ty in advance!

-----------------------------------------------------------------------------------------------

I have managed to come up with this code:

-----------------------------------------------------------------------------------------------

public class SlembiEind2 {

public static void main(String[] args) { int n = Integer.parseInt(args[0]); int[] x = new int[n]; int[] y = new int[n]; int steps = 0; int ctv = n*n; boolean[][] fylki = new boolean[n][n]; StdDraw.setXscale(0,n-1); StdDraw.setYscale(0,n-1); StdDraw.clear(StdDraw.GRAY); StdDraw.enableDoubleBuffering(); for(int i = 0; i < n; i++) { x[i] = n/2; y[i] = n/2; StdDraw.filledSquare(x[i], y[i], 0.45); } fylki[n/2][n/2] = true; ctv--; while (ctv > 0) {

double r = Math.random(); for(int i = 0; i < n; i++) { StdDraw.setPenColor(StdDraw.BLUE); StdDraw.filledSquare(x[i], y[i], 0.45); StdDraw.show(); StdDraw.pause(100); if(r < 0.25){ x[i]--; } else if(r < 0.50){ x[i]++; } else if(r < 0.75){ y[i]--; } else if(r < 1.00){ y[i]++; } if (x[i] < n && y[i] < n && x[i] >= 0 && y[i] >= 0 && !fylki[x[i]][y[i]]) { ctv--; fylki[x[i]][y[i]] = true; } steps++; StdDraw.setPenColor(StdDraw.WHITE); System.out.println(ctv + " " + steps); } } StdOut.println("Total steps = " + steps); } }

-------------------------------------------------------------------------------------

But I have two problems.

The walker seems to walk outside of the array/grid.

And it keeps counting the steps when it walks outside of the grid.

Anyway you can help me fix the code so it fix these problems? The code should not count if the walke trys to go out of the grid!

-----------------------------------------------------------------------------

ctv is the cells left to visit before the walker fill the grid!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!