private SquareOfCells GenerateSquares(int[,] cells, int size) { SquareOfCells bestSquare = null; for (var x = 1; x + size - 1 <= 300; x++) { for (var y = 1; y + size - 1 <= 300; y++) { var totalLevel = 0; for (var xCoord = x; xCoord < x + size; xCoord++) { for (var yCoord = y; yCoord < y + size; yCoord++) { totalLevel += cells[xCoord - 1, yCoord - 1]; } } if (bestSquare == null || bestSquare.Level < totalLevel) { bestSquare = new SquareOfCells(totalLevel, size, x, y); } } } return(bestSquare); }
public string Calc2() { var cells = GenerateCells(); SquareOfCells bestSquare = null; var bag = new ConcurrentBag <SquareOfCells>(); Parallel.For(1, 300, size => bag.Add(GenerateSquares(cells, size))); var allSquares = bag.ToList(); bestSquare = allSquares.OrderByDescending(s => s.Level).FirstOrDefault(); //for (var size = 1; size <= 300; size++) //{ // var newBest = GenerateSquares(cells, size); // if (bestSquare == null || bestSquare.Level < newBest.Level) // bestSquare = newBest; //} return($"{bestSquare.X},{bestSquare.Y},{bestSquare.Size}"); }