Home > database >  Row matrix and move in row matrix
Row matrix and move in row matrix

Time:06-14

I want to write a method that will move the zero in all 4 directions And update the map by moving the zero in the console, how do I do this?

    public static void main(String args[]) {
        String[][] map = {
                {".",".","."},
                {".","0","."},
                {".",".","."}
        };
        for(int i=0;i<map.length;i  ){
            for(int j=0;j<map[i].length;j  ){
                System.out.print(map[i][j] " ");
            }
            System.out.println();
        }

    }

there are no restrictions or anything, I just want to figure out how to move the zero, by any means.

CodePudding user response:

The code you've written will just attempt the printing of your matrix. If you want to move the "0" element around your matrix, you first need to define an enum to establish the direction and then a method move() that given a direction d and a matrix m will move the "0" element accordingly.

The element movement is just a rotation that increments or decrements the respective index by taking care of the edge cases (the row and column's bounds).

public class Main {

    public enum Direction {
        UP, DOWN, LEFT, RIGHT
    }

    public static void move(Direction move, String[][] matrix) {
        int row = -1, col = -1, newRow, newCol;
        String temp;

        //Finding the position of the "0" element
        for (int i = 0; i < matrix.length; i  ) {
            for (int j = 0; j < matrix[i].length; j  ) {
                if (matrix[i][j].equals("0")) {
                    row = i;
                    col = j;
                    break;
                }
            }
            if (row != -1) break;
        }

        //If not 0 element is found then the method is terminated
        if (row < 0 || col < 0) {
            return;
        }

        switch (move) {
            case UP:
                newRow = row - 1 < 0 ? matrix.length - 1 : row - 1;
                temp = matrix[newRow][col];
                matrix[newRow][col] = matrix[row][col];
                matrix[row][col] = temp;
                break;
            case DOWN:
                newRow = (row   1) % matrix.length;
                temp = matrix[newRow][col];
                matrix[newRow][col] = matrix[row][col];
                matrix[row][col] = temp;
                break;
            case LEFT:
                newCol = col - 1 < 0 ? matrix[row].length - 1 : col - 1;
                temp = matrix[row][newCol];
                matrix[row][newCol] = matrix[row][col];
                matrix[row][col] = temp;
                break;
            case RIGHT:
                newCol = (col   1) % matrix[row].length;
                temp = matrix[row][newCol];
                matrix[row][newCol] = matrix[row][col];
                matrix[row][col] = temp;
                break;
        }
    }

Here is a link testing a possible implementation

https://www.jdoodle.com/iembed/v0/s63

  • Related