By the way, there should be 4 types of mask, not 3 types. My bad.
No no... The way you go through the array is ...
for (int row=0; row<matrix.length; row++) {
for (int col=0; col<matrix.length; col++) {
// for all masks, check the first and last position whether they are in range
// if they are ALL in range, then you can further check if the value
// inside are consecutive
}
}
/*
for example...
+ - + - + - + - + - + - + - + - +
| 0 | 1 | 0 | 2 | 3 | 3 | 2 | 0 |
+ - + - + - + - + - + - + - + - +
| 1 | 1 | 3 | 3 | 3 | 3 | 0 | 1 |
+ - + - + - + - + - + - + - + - +
| 0 | 1 | 0 | 2 | 3 | 1 | 2 | 1 |
+ - + - + - + - + - + - + - + - +
| 3 | 1 | 0 | 2 | 3 | 3 | 2 | 3 |
+ - + - + - + - + - + - + - + - +
| 0 | 3 | 0 | 2 | 3 | 3 | 2 | 3 |
+ - + - + - + - + - + - + - + - +
| 0 | 2 | 3 | 2 | 0 | 3 | 3 | 2 |
+ - + - + - + - + - + - + - + - +
| 2 | 1 | 0 | 2 | 3 | 3 | 3 | 1 |
+ - + - + - + - + - + - + - + - +
| 0 | 1 | 1 | 2 | 3 | 1 | 2 | 2 |
+ - + - + - + - + - + - + - + - +
For mask#1, you start at 0,0 (row, column) and keep going through column until
the end because it is vertical check. You need to keep checking if the end
position is a valid location. If it is not, you skip this check.
for mask#2, while you are going through the same loop, keep going through column
until the mask becomes invalid at position 0,7 because it is horizontal check.
Then you skip this check until the next row.
for mask#3, still inside the same loop, you would start with 0,0 and end with 2,2
(startRow+2, startCol+2). Remember to keep checking whether the end position is
valid each time a loop is going through.
for mask#4, still inside the same loop, you would not start it until the start
location is at least 2 (at 2,2) and the end will be 0,0 (startRow-2, startCol+2).
Iteration at row=0, col=0
mask#1 - start 0,0 and end 2,0 => value [0, 1, 0] => false
mask#2 - start 0,0 and end 0,2 => value [0, 1, 0] => false
mask#3 - start 0,0 and end 2,2 => value [0, 1, 0] => false
mask#4 - start 0,0 and end 2,-2 => skip
Iteration at row=0, col=1
mask#1 - start 0,1 and end 2,1 => value [0, 3, 0] => true, count it!
mask#2 - start 0,1 and end 0,3 => value [0, 2, 3] => false
mask#3 - start 0,1 and end 2,3 => value [0, 3, 3] => false
mask#4 - start 0,1 and end 2,-1 => skip
Iteration at row=0, col=2
mask#1 - start 0,2 and end 2,2 => value [0, 1, 0] => false
mask#2 - start 0,2 and end 0,4 => value [0, 1, 0] => false
mask#3 - start 0,2 and end 2,4 => value [0, 1, 0] => false
mask#4 - start 0,2 and end 2,0 => value [0, 1, 0] => false
... and go on until the next row, repeat the same check ...
*/