PRINT SUB MATRIX WITH EQUAL NUMBER OF 0s AND 1s
namespace SubMatrixWithEqual0sAnd1s
{
class Program
{
public static bool FindSubMatrix(int[,] Mat, int r1, int c1, int r2, int c2)
{
if (r1 > r2 || c1 > c2) return false;
if ((r2 * c2) % 2 == 0)
{
int ones = 0;
int Zeros = 0;
for (int i = r1; i < r2; i++)
{
for (int j = c1; j < c2; j++)
{
if (Mat[i, j] == 1) ones++;
else Zeros++;
}
}
if (ones == Zeros)
{
Console.WriteLine(r1 + " " + c1 + " " + r2 + " " + c2);
Console.WriteLine();
for (int i = r1; i < r2; i++)
{
for (int j = c1; j < c2; j++)
{
Console.Write(Mat[i, j] + " ");
}
Console.WriteLine();
}
return true;
}
}
if (FindSubMatrix(Mat, r1, c1, r2 - 1, c2) == true) return true;
if (FindSubMatrix(Mat, r1, c1, r2, c2 - 1) == true) return true;
if (FindSubMatrix(Mat, r1 + 1, c1, r2, c2) == true) return true;
if (FindSubMatrix(Mat, r1, c1 + 1, r2, c2) == true) return true;
return false;
}
static void Main(string[] args)
{
int[,] Mat = {
{1,0,0,1},
{0,1,1,1},
{1,0,0,0},
{1,0,0,0}
};
FindSubMatrix(Mat, 0, 0, Mat.GetLength(0), Mat.GetLength(1));
}
}
}
No comments:
Post a Comment