Почему?
Мой вывод.
count = 2. Должно быть 2
count = 4. Должно быть 4
package com.javarush.task.task20.task2026;
/*
Алгоритмы-прямоугольники
*/
public class Solution {
public static void main(String[] args) {
byte[][] a1 = new byte[][]{
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 1}
};
byte[][] a2 = new byte[][]{
{1, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 0, 0, 1}
};
int count1 = getRectangleCount(a1);
System.out.println("count = " + count1 + ". Должно быть 2");
int count2 = getRectangleCount(a2);
System.out.println("count = " + count2 + ". Должно быть 4");
}
public static int getRectangleCount(byte[][] a) {
int count = 0, tmp = -1, jt = a[0].length;
boolean fund = false;
while (tmp != count) {
tmp = count;
round:
for (int i = 0; i < a[0].length; i++) {
for (int j = 0; j < a[0].length; j++) {
if (a[i][j] == 1 && fund == false) {
fund = true;
count++;
jt = j;
a[i][j] = 0;
} else if (a[i][j] == 1 && fund == true) {
a[i][j] = 0;
} else if (a[i][j] == 0 && fund == true) {
if ((i + 1) < a[0].length) {
if (a[i + 1][jt] == 1) {
break ;
} else {
fund = false;
break round;
}
} else {
fund = false;
break round;
}
}
}
}
}
return count;
}
}