I try to solve BITMAP using C#, but i'm always get NZEC. This my code:
using System;
class Bitmap
{
private static int[,] matrix;
static void SetAround(int i, int j, int n, int m)
{
if (i + 1 < n)
if (matrix[i + 1, j] == -1)
matrix[i + 1, j] = matrix[i, j] + 1;
if (i - 1 >= 0)
if (matrix[i - 1, j] == -1)
matrix[i - 1, j] = matrix[i, j] + 1;
if (j + 1 < m)
if (matrix[i, j + 1] == -1)
matrix[i, j + 1] = matrix[i, j] + 1;
if (j - 1 >= 0)
if (matrix[i, j - 1] == -1)
matrix[i, j - 1] = matrix[i, j] + 1;
}
static void GetMin(int n, int m)
{
bool isNull = true;
while (isNull)
{
isNull = false;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (matrix[i, j] == 0 | matrix[i, j] != -1)
SetAround(i, j, n, m);
else
isNull = true;
}
}
}
}
public static void Main()
{
int k;
int n;
int m;
string temp;
k = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < k; i++)
{
temp = Console.ReadLine();
n = Convert.ToInt32(temp.Substring(0, temp.IndexOf(" ")));
m = Convert.ToInt32(temp.Substring(temp.IndexOf(" "), temp.Length - temp.IndexOf(" ")));
matrix = new int[n, m];
// Read matrix and if 1 than put 0, and put -1 if 0;
for (int j = 0; j < n; j++)
{
temp = Console.ReadLine();
for (int l = 0; l < m; l++)
if (Convert.ToInt32(temp[l]-'0') == 1)
matrix[j, l] = 0;
else
matrix[j, l] = -1;
}
// Calculate min way to white for all matrix
GetMin(n, m);
// Print results
for (int j = 0; j < n; j++)
{
for (int l = 0; l < m; l++)
Console.Write(matrix[j, l] + " ");
Console.WriteLine();
}
}
}
}
Thank you.