Find coordinate of a cell in an array of characters

123 Views Asked by At

How would I find a coordinate of a cell in a 2d char table. For example if my table would display something like this:

  1. .....@@@@.....
  2. .........@@@.....
  3. .....@@@@.....

I want to find the second '@' in second row, as this would produce a square of 3x3. The squares Im trying to find are made of odd numbers 3x3, 5x5... And if There are two squares of same dimensions next to each other they need to be separated.

I am storing all the values in a table c[row][column] and thought of using mod 2=1 to find the odd number, but Im not sure how I would find the coordinate and make sure it doesn't repeat if there are two squares next to each other.

So far I've got:

for (int r = 0; r < row; r++) {
    for (int col = 0; col < column; col++) {
        if (c[r][col] != '.') {
            if (c[r][col] != '.' && c[r + 1][col] != '.' && c[r + 2][col] != '.') {
                if (c[r][col + 1] != '.' && c[r + 1][col + 1] != '.' && c[r + 2][col + 1] != '.') {
                    if (c[r][col + 2] != '.' && c[r + 1][col + 2] != '.' && c[r + 2][col + 2] != '.') {
                        System.out.println(r + " " + col);
                    }
                }
            }
        }
    }
}
1

There are 1 best solutions below

0
On

Well you could look through the array first:

  for (int i=0; i< row.size; i++){
      for (int j=0; j<column.size; j++){
             if (c[i][j] != '.'){
                 System.out.print (i + " " + j);
             }
        }
    }

Not sure about how to find the middle cell so it makes a square with the odd number of cells, sorry :(