Perthupuethto
Perthupuethto

Reputation: 1

How can I save all the coordinates on which a person has moved to a two-dimensional array?

Heading

There is a man which is trying to move inside a square board. The board is a grid of size where the point is in its upper-left corner and the point is in its bottom-right corner. The man will move only by following some rules:

It will always move according to the current direction of the wind (possible directions are North, South, West, East, North-East, North-West, South-East, South-West). It can visit a specific cell of the board only once. It can not go out of the borders of the board. If it reaches at a point where it can not move according to the rules, it will stay at the current position. It can move only to the neighbouring cells (8 possible moves according to the above directions). It will always start its journey from the point (the upper-left corner of the board). It can only make one movement per second. In order to make things more difficult for him, the wind will change its direction multiple times during the man's journey. You will always be given the direction of the wind before the man starts to move (0-th second) and then you will be given the specific time in seconds when the wind is going to change its direction (in a strictly increasing sequence).

After following all the journey of the man, you will have to specify how many positions inside the board were not visited by the man.

Input Format

In the first line you will be given two integers 'n' and 'k' where 'n' represents the dimension of the board. Then in the following 'k' lines you will be given an integer 'ti' and a string 'w'. The 'ti' represents the time in seconds in which on the board a wind of direction 'w' will be applied.The 'w' might take a value of N, S, W, E, NE, NW, SE, or SW (representing North, South, West, East, North-East, North-West, South-East or South-West, respectively).

Constraints

3 <= n <= 100000

2 <= k <= 100000

0 <= ti <=100000

t0 = 0 < t1 <...< tk-1

Output Format

Output the number of positions inside the board that were not visited by the man.

Sample Input 0

5 6

0 SE

1 NE

2 E

6 SW

15 N

20 W

Sample Output 0

13

public class Solution {

    public static void main (String args []) {

        //Table
        int [][] board = new int [7][7]; 

        // Seconds available for moving
        int seconds = 43; 
        // Initial Man Coordinate
        int Man = board [0][0]; 

        // Two dimensional array to save the coordinates visited by the man
        int [][] isVisited = new int [7][7]; 

        // Formula to calculate the cells that are not visited by the man ( i know is wrong )
        int notVisited = isVisited.isEmpty(true) - isVisited.isEmpty(false);  

        for (int i=0;i<=board.length;i++) {
            for(int j=0;j<=board.length;j--) {

                while (seconds < 4 ) {
                    i++;
                    j++;
                    Man = board [i][j];

                    //Here should be the code to save the coordinates visited by the man -->

Upvotes: 0

Views: 581

Answers (1)

MWB
MWB

Reputation: 1879

I would first create a new call called Board:

public class Board {
    private boolean[][] visited;
    private int boardSize;
    private int numVisited;

    public Board(int boardSize) {
        this.boardSize = boardSize;
        visited = new boolean[boardSize][boardSize];
    }

    public void setVisited(int x, int y) {
        if (!visited[x][y]) {
            visited[x][y] = true;
            numVisited++;
        }
    }

    public int getBoardSize() {
        return boardSize;
    }

    public int getNumVisited() {
        return numVisited;
    }

    public int getNumNotVisited() {
        return boardSize * boardSize - numVisited;
    }
}

Afterwards you can create an instance of Board

Board myBoard = new Board(7);

And then in your logic, you can set a cell to visited by calling setVisited:

myBoard.setVisited(3, 3);

And you can count the number of visited cells by calling countVisited

int numberOfVisitedCells = myBoard.getNumVisited();

Or, if you want the number of cells that have not been visited:

int numberofCellsNotVisited = myBoard.getNumNotVisited();

EDIT: Thanks @Matthew for the improvement!

Upvotes: 1

Related Questions