简体   繁体   中英

Java - find consecutive elements in 2D array

I'm having trouble with an algorithm, suppose the following: A cinema has n rows, each row consists of m seats (n and m do not exceed 20). A two-dimensional matrix stores the information on the sold tickets, number 1 means that the ticket for this place is already sold, the number 0 means that the place is available. You want to buy k tickets to the neighboring seats in the same row. Find whether it can be done.

Input data

On the input, the program gets the number of n rows and m seats. Then, there are n lines, each containing m numbers (0 or 1) separated by spaces. The last line contains a number k.

Output data

The program should output the number of the row with k consecutive available seats. If there are several rows with k available seats, output the first row with these seats. If there is no such a row, output the number 0.

Code

import java.util.Scanner;

class Main {
    static int findRowWithAvailableSeat(int[][] matrix, int tickets) {
        final int rows = matrix.length;
        final int columns = matrix[0].length;
        int seatCounter = 0;

        for (int r = 1; r <= rows; r++) {
            for (int c = 1; c <= columns; c++) {
                if (matrix[r][c] == 1) {
                    continue;
                } if (matrix[r][c] == matrix[r][c + 1]) {
                    seatCounter++;
                    if (seatCounter == tickets) {
                        return r;
                    }
                }
            }
        }
        return 0;
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int rows = scanner.nextInt();
        int columns = scanner.nextInt();

        int[][] matrix = new int[rows][columns];
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                matrix[r][c] = scanner.nextInt();
            }
        }
        int kTickets = scanner.nextInt();
        int rowWithAvailableSeats = findRowWithAvailableSeat(matrix, kTickets);
        System.out.println(rowWithAvailableSeats);
    }
}

I know the problem is somewhere in findRowWithAvailableSeat method. I would like a hint on how to solve the problem, not the actual solution. Thank you very much.

EDIT

I could finally solve it (or at least it works as intended, I'm not sure if it's the best implementation). Thanks you all for your tips.

   static int findRowWithAvailableSeat(int[][] matrix, int tickets) {
        final int rows = matrix.length;
        final int columns = matrix[0].length;
        int seatCounter;

        for (int r = 0; r < rows; r++) {
            seatCounter = 0;
            for (int c = 0; c < columns; c++) {
                if (matrix[r][c] == 1) {
                    seatCounter = 0;
                    continue;
                }
                if (matrix[r][c] == 0) {
                    seatCounter++;
                    if (seatCounter == tickets) {
                        return r + 1;
                    }
                }
            }
        }
        return 0;
    }

Hope this helps

  1. on main() method, you set matrix from index 0 to rows-1, and from index 0 to columns-1. But on findRowWithAvailableSeat(), you start from index 1. So index 0 will never be accessed.
  2. } if (matrix[r][c] == matrix[r][c + 1]) { this line will try to access index c+1, will result index out of bound exception. either you need to check whether or not c+1 is less than column size or compare matrix[r][c] with 0 (matrix[r][c] == 0).
  3. if you looking for neighboring seats, then you need to reset the seatCounter when you visit the sold seat (matrix[r][c] == 1)
  4. you also need to reset seatCounter for each row.

Your for loops start at 1 instead of 0

for (int r = 1; r <= rows; r++) {
    for (int c = 1; c <= columns; c++) {
        if (matrix[r][c] == 1) {

The first index of the matrix array is being skipped. So you're not checking the first row and the first seat of each row.

Also, seatCounter is not being reset for each row. It currently retains any seats counted from previous rows.

I'm also not sure what this is for

if (matrix[r][c] == matrix[r][c + 1])

I would put each row in a string and all rows in a string array a[m], then do a[i].contains(k) on each row: "1001101110100010110010".contains("000")

Here are the 4 tips you need:

  1. Don't compare matrix[r][c] == matrix[r][c + 1] . Plainly compare to 0: matrix[r][c] == 0 . To avoid ArrayIndexOutOfBoundsException

  2. As DarkSigma already pointed, start your loops iterator from 0, not 1: for (int r = 0; r < rows...

  3. Re-init your seatCounter to 0 for each row.

  4. And! When you print out System.out.println(rowWithAvailableSeats) , remember that row counting starts from 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM