沒有花哨的東西:
int width = binaryzacja.GetLength(0);
int height = binaryzacja.GetLength(1);
int newWidth = width;
int newHeight = height;
int x, y, x2, y2;
for (y = 0; y < height; y++)
{
if (IsRowEmpty(binaryzacja, y)) newHeight--;
}
for (x = 0; x < width; x++)
{
if (IsColumnEmpty(binaryzacja, x)) newWidth--;
}
int[,] binaryzacja2 = new int[newWidth, newHeight];
// copy to new array
for (y2 = y = 0; y < height; y++)
{
if (!IsRowEmpty(binaryzacja, y))
{
for(x = x2 = 0; x < width; x++)
{
if (!IsColumnEmpty(binaryzacja, x))
{
binaryzacja2[x2, y2] = binaryzacja[x, y];
x2++;
}
}
y2++;
}
}
bool IsRowEmpty(int[,] array, int y)
{
for (int x = 0; x < array.GetLength(0); x++)
{
if (array[x, y] != 0) return false;
}
return true;
}
bool IsColumnEmpty(int[,] array, int x)
{
for (int y = 0; y < array.GetLength(1); y++)
{
if (array[x, y] != 0) return false;
}
return true;
}
-----新版本:
int width = binaryzacja.GetLength(0);
int height = binaryzacja.GetLength(1);
int newWidth = width;
int newHeight = height;
int[] keepRows = new int[height];
int[] keepColumns = new int[width];
int x, y, x2, y2;
int i;
for (i = y = 0; y < height; y++)
{
if (IsRowEmpty(binaryzacja, y)) newHeight--;
else
{
keepRows[i] = y;
i++;
}
}
for (i = x = 0; x < width; x++)
{
if (IsColumnEmpty(binaryzacja, x)) newWidth--;
else
{
keepColumns[i] = x;
i++;
}
}
int[,] binaryzacja2 = new int[newWidth, newHeight];
// copy to new array
for (y2 = y = 0; y < height; y++)
{
if(y == keepRows[y2])
{
for (x2 = x = 0; x < width; x++)
{
if(x == keepColumns[x2])
{
binaryzacja2[x2, y2] = binaryzacja[x, y];
x2++;
}
}
y2++;
}
}
你給你的代碼的粗線條聽起來像它會工作。爲什麼不向我們展示代碼,以便我們可以看到可能出現的問題? – user12864 2014-11-01 17:58:56
如何填充陣列?爲什麼不在插入時記錄每行和每列。爲每個列布爾,只要你做一個插入(不是0)將其設置爲true。相同的行... – Pleun 2014-11-01 18:53:36
我只是建議你添加每行或列的元素,如果0跳過它。這是一個算法問題。 – 2014-11-01 21:00:50