Hey, somebody finally fixed this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C9Test
{
class Program
{
static void Main(string[] args)
{
int width = 6, length = 5;
int[][] arr = new int[length][];
arr[0] = new int[] { 1, 1, 1, 0, 1, 1 };
arr[1] = new int[] { 1, 1, 1, 1, 1, 1 };
arr[2] = new int[] { 1, 1, 1, 1, 1, 1 };
arr[3] = new int[] { 0, 0, 1, 1, 1, 1 };
arr[4] = new int[] { 0, 0, 1, 1, 1, 1 };
/*
//After run 1
{ 1, 1, 0, 0, 1, 0 }
{ 1, 1, 1, 1, 1, 0 }
{ 0, 0, 1, 1, 1, 0 }
{ 0, 0, 1, 1, 1, 0 }
{ 0, 0, 0, 0, 0, 0 }
//After run 2
{ 1, 0, 0, 0, 0, 0 }
{ 0, 0, 1, 1, 0, 0 }
{ 0, 0, 1, 1, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
//After run 3
{ 0, 0, 0, 0, 0, 0 }
{ 0, 0, 1, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
//After run 4
{ 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
*/
int size = 1;
int maxlencheck = length;
int maxcolcheck = width;
do
{
var newarr = SetMask(arr, width, size);
var hasone = newarr.Count(y => y.Any(x => x == 1));
if (hasone == 0)
{
break;
}
else
{
size++;
arr = newarr;
maxlencheck = length - size;
maxcolcheck = width - size;
}
}
while (maxlencheck > 0 && maxcolcheck > 0);
if (size > 1)
{
var rowhasones = arr.Select((r, i) => new { Index = i, RowData = r })
.Where((r, i) => r.RowData.Any(x => x == 1))
.Select(r => r.Index);
foreach (var rowindex in rowhasones)
{
for (int i = 0; i <= maxcolcheck; i++)
{
if (arr[rowindex][i] == 1)
{
Console.WriteLine("{0:d}x{0:d} square @ ({1:d},{2:d})", size, rowindex, i);
}
}
}
}
}
static int[][] SetMask(int[][] arrSrc, int width, int offset)
{
var length = arrSrc.Length;
var maxlencheck = length - 1;
var maxcolcheck = width - offset;
var arrRet = new int[maxlencheck][];
for (int i = 0; i < maxlencheck; i++)
{
arrRet[i] = new int[maxcolcheck]; //default to zeroes
}
//Project to anonymous type to get array index
var rowhasones = arrSrc.Select((r, i) => new { Index = i, RowData = r })
.Where((r) => r.Index < maxlencheck && r.RowData.Any(x => x == 1))
.Select(r => r.Index)
.OrderBy(i => i);
foreach (var rowindex in rowhasones)
{
var currow = arrSrc[rowindex];
var nextrow = arrSrc[rowindex + 1];
for (int i = 0; i < maxcolcheck; i++)
{
if (currow[i] == 1 && currow[i + 1] == 1 && nextrow[i] == 1 && nextrow[i + 1] == 1)
{
arrRet[rowindex][i] = 1;
}
}
}
return arrRet;
}
}
}
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.