public IEnumerable<Cell> GetCellsInRange(Cell cell, string[,] matrix) { if (!char.IsDigit(cell.Value[0])) { return null; } List<Cell> cellsinRange = new List<Cell>(); int cellValue = int.Parse(cell.Value); Cell upperCell = new Cell(matrix[cell.Row + cellValue, cell.Col], cell.Row, cell.Col); Cell lowerCell = new Cell(matrix[cell.Row - cellValue, cell.Col], cell.Row, cell.Col); Cell leftCell = new Cell(matrix[cell.Row, cell.Col - cellValue], cell.Row, cell.Col); Cell rightCell = new Cell(matrix[cell.Row, cell.Col + cellValue], cell.Row, cell.Col); if (IsValid(upperCell, matrix)) { cellsinRange.Add(upperCell); } if (IsValid(lowerCell, matrix)) { cellsinRange.Add(lowerCell); } if (IsValid(leftCell, matrix)) { cellsinRange.Add(leftCell); } if (IsValid(rightCell, matrix)) { cellsinRange.Add(rightCell); } return cellsinRange; }
static void Main() { // get initial input data string[] startLocation = Console.ReadLine().Split(' '); int startingRow = int.Parse(startLocation[0]); int startingCol = int.Parse(startLocation[1]); string[] matrixSize = Console.ReadLine().Split(' '); int rows = int.Parse(matrixSize[0]); int cols = int.Parse(matrixSize[1]); // fill matrix string[,] matrix = new string[rows, cols]; for (int row = 0; row < rows; row++) { string[] rowElements = Console.ReadLine().Split(' '); for (int col = 0; col < cols; col++) { matrix[row, col] = rowElements[col]; } } Cell startCell = new Cell(matrix[startingRow, startingCol], startingRow, startingCol); Tree tree = new Tree(new TreeNode(int.Parse(startCell.Value), startingRow, startingCol)); }
public static bool IsValid(Cell cell, string[,] matrix) { return cell.Row >= 0 && cell.Row < matrix.GetLength(0) && cell.Col >= 0 && cell.Col < matrix.GetLength(1) && cell.Value != ImpassableSymbol && !cell.Visited; }
public Tree TraverseTree(Tree tree, TreeNode startCell, string[,] matrix) { foreach (TreeNode child in startCell.GetChildren()) { Cell currentCell = new Cell(child.Value.ToString(), child.Row, child.Col); IEnumerable<Cell> cellsInRange = GetCellsInRange(currentCell, matrix); foreach (Cell cell in cellsInRange) { } } return tree; }