Untitled
unknown
csharp
3 months ago
2.3 kB
8
Indexable
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace NumberMatchSolver;
public static class FastMatchFinder
{
private static readonly List<(int startRow, int startCol, int endRow, int endCol, int number)> ConnectedPairs
= new(100);
public static List<(int startRow, int startCol, int endRow, int endCol, int number)> Solve(int[][] grid)
{
if (grid == null || grid.Length == 0)
return new List<(int startRow, int startCol, int endRow, int endCol, int number)>();
ConnectedPairs.Clear();
int totalRows = grid.Length;
int totalCols = grid[0].Length;
for (int row = 0; row < totalRows; row++)
{
for (int col = 0; col < totalCols; col++)
{
int currentNumber = grid[row][col];
if (currentNumber == 0)
continue;
int[] rowDirections = { 0, 1, 1, -1 };
int[] colDirections = { 1, 0, 1, 1 };
for (int directionIndex = 0; directionIndex < 4; directionIndex++)
{
int rowStep = rowDirections[directionIndex];
int colStep = colDirections[directionIndex];
int nextRow = row + rowStep;
int nextCol = col + colStep;
while (nextRow >= 0 && nextRow < totalRows && nextCol >= 0 && nextCol < totalCols)
{
int nextNumber = grid[nextRow][nextCol];
if (nextNumber == currentNumber)
{
ConnectedPairs.Add((row, col, nextRow, nextCol, currentNumber));
break;
}
if (nextNumber != 0)
{
break;
}
nextRow += rowStep;
nextCol += colStep;
}
}
}
}
return new List<(int startRow, int startCol, int endRow, int endCol, int number)>(ConnectedPairs);
}
}Editor is loading...
Leave a Comment