Você está na página 1de 6

Walking Over a Matrix

The problem
Show and discuss a code to walk on an NxN matrix. Each cell in matrix will initially have a true or a false. True indicates the cell is blocked or already visited.

The problem requires an NxN matrix, filled randomly with true and false values. TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE

Now, we choose a random starting position and start moving randomly, by one block at a time. We can to visit only those blocks that have a false value. The move can be in any of the possible eight directions.

The travelling process will end when all the surrounding blocks of the current block have been visited or have the value true.

The Solution
Generating the Matrix.
We are using an NxN String matrix in this solution, therefore the cells can have any value, but we will confine ourselves to true, false, and visited only. To generate random Boolean values, we would require the nextBoolean() method from the Java.util.Random class. The function fill() fills the NxN matrix Arr with random Boolean values. The Code:

1 2 3 4 5 6 7 8

public void fill() { Random randomGenerator = new Random(); for (int i = 0; i < Arr.length; i++) { for (int j = 0; j < Arr[i].length; j++) { Arr[i][j] = randomGenerator.nextBoolean() + ""; } } }

Note that, in line 5 of this code, we are implicitly converting Boolean values to String values. This code will generate the following matrix. TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE

Generating Random Starting Point


We need a starting point to travel in the matrix. Now, we would be using the nextInt() method from Java.util.Random class, to generate a pair of integers to indicate row and column. We will then store them as an integer array of length 2. A[0] A[1] The Code: 1 public static int[] getPos(int n) { 2 int[] i = new int[2]; 3 i[0] = randomGenerator.nextInt(n - 1); 4 i[1] = randomGenerator.nextInt(n - 1); 5 return i; 6 } We also need to check the validity of the position, i.e. if it lies within the matrix, and there is a false at the location. To do this, we can use a function check, as defined below. The code: 1 public boolean check(int[] x) throws ArrayIndexOutOfBoundsException { 2 if (x[0] < Arr.length && x[0] >= 0 && x[1] < Arr[0].length && x[1] >= 0) { 3 if (Arr[x[0]][x[1]].equalsIgnoreCase("false")) 4 return true; 5 else 6 return false; 7 } 8 else 9 return false; 10 } After running this piece of code, we will have a valid starting point. Let it be (4, 4). TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE row column

Travelling
So far, our matrix has been populated, and we have a valid starting point. Now we need to move in any of the eight directions. To be able to do so, we can generate a pair of integers having random values in the interval [-1, 1]. This will allow us to make a move to a neighboring cell.
(-1, 0) (-1, -1) (-1, 1)

(0, -1)

Current Position

(0, 1)

(1, -1) (1, 0)

(1, 1)

This way, we can generate a move to a neighboring cell. The Code: 1 public static int[] getMove() { 2 int[] i = new int[2]; 3 i[0] = randomGenerator.nextInt(3) -1; 4 i[1] = randomGenerator.nextInt(3) -1; 5 return i; 6 } Assuming our current position to be stored in A, the new position N will be given by, Int[] i = getMove(); N[0] = A[0] + i[0]; N[1] = A[1] + i[1]; This can be looped till we gat a valid N. Validity of the cell can be tested using the function we previously defined, check(). When we get a valid cell, we change the value of the previous cell to visited, and move to the new cell. Assuming i to be (1, 0), the new position is (5, 4). TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE VISITED FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE

The above process can be looped to get valid neighbouring cells untill we reach a dead end. Let the next move be(1, 1), so the next cell would be (6,5). TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE VISITED VISITED TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE

Next move: (1, 1) Next Cell: (7, 6) TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE VISITED VISITED RUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE VISITED FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE

Next move: (0, -1) Next Cell: (7, 5) TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE VISITED VISITED TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE VISITED FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE VISITED FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE

Terminating Condition
We need to check the terminating condition to know when we cannot go any further. We can do this by checking all the surrounding cells. If all of them has a value true or visited, we abort. The code: 1 public boolean checkAround(int[] n) { 2 for (int i = -1; i < 2; i++) { 3 for (int j = -1; j < 2; j++) { 4 int x[] = new int[2]; 5 x[0] = n[0] + i; 6 x[1] = n[1] + j; 7 if (check(x)) { 8 return true; 9 } 10 } 11 } 12 return false; 13 } TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE VISITED VISITED TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE VISITED VISITED FALSE TRUE TRUE TRUE FALSE TRUE TRUE VISITED FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE

The present cell has no more connected cells with a false value, and now the program will terminate.

Você também pode gostar