private static bool IsCellUsable(int[,] matrix, Cell cell) { bool isCellUsable = cell.X >= 0 && cell.X < matrix.GetLength(0) && cell.Y >= 0 && cell.Y < matrix.GetLength(1) && matrix[cell.X, cell.Y] == 0; return isCellUsable; }
public static int[,] TraverseTheMatrix(int[,] matrix, Cell startingCell) { var queue = new Queue<Cell>(); queue.Enqueue(startingCell); while (queue.Any()) { var cell = queue.Dequeue(); matrix[cell.X, cell.Y] = cell.Value; for (int index = 0; index < Direction.Length; index += 2) { if (IsNextCellUsable(matrix,cell,index)) { queue.Enqueue(new Cell ( cell.X + Direction[index], cell.Y + Direction[index + 1], cell.Value + 1 )); } } } return matrix; }
static Cell GetCellPosition() { int startRow = int.Parse(Console.ReadLine()); int startCol = int.Parse(Console.ReadLine()); Cell cell = new Cell(startRow, startCol); return cell; }
private static bool IsNextCellUsable(int[,] matrix, Cell cell, int index) { var nextCell = new Cell() { X = cell.X + Direction[index], Y = cell.Y + Direction[index + 1] }; return IsCellUsable(matrix, nextCell); }
private static Cell Input() { int rows = int.Parse(Console.ReadLine()); int cols = int.Parse(Console.ReadLine()); int startRow = int.Parse(Console.ReadLine()); int startCol = int.Parse(Console.ReadLine()); arr = new int[rows, cols]; q = new Queue<Cell>(); Cell p = new Cell() { Row = startRow, Col = startCol, Val = 1 }; return p; }
private static void FillBoard(Cell startCell) { q.Enqueue(startCell); arr[startCell.Row, startCell.Col] = 1; while(q.Count > 0) { Cell p = q.Dequeue(); VisitCell(p, -1, -2); VisitCell(p, -1, 2); VisitCell(p, 1, -2); VisitCell(p, 1, 2); VisitCell(p, -2, -1); VisitCell(p, -2, 1); VisitCell(p, 2, -1); VisitCell(p, 2, 1); } }
static void Main() { matrixRows = int.Parse(Console.ReadLine()); matrixColumns = int.Parse(Console.ReadLine()); matrix = new int[matrixRows, matrixColumns]; int startPositonRow = int.Parse(Console.ReadLine()); int startPositionColumn = int.Parse(Console.ReadLine()); var startCell = new Cell() { X = startPositonRow, Y = startPositionColumn, Value = 1 }; var queue = new Queue<Cell>(); queue.Enqueue(startCell); while (queue.Count > 0) { var cell = queue.Dequeue(); visited.Add(cell); matrix[cell.X, cell.Y] = cell.Value; if (IsFreeCell(cell.X - 2, cell.Y + 1)) { queue.Enqueue(new Cell() { X = cell.X - 2, Y = cell.Y + 1, Value = cell.Value + 1 }); matrix[cell.X - 2, cell.Y + 1] = cell.Value + 1; } if (IsFreeCell(cell.X - 1, cell.Y + 2)) { queue.Enqueue(new Cell() { X = cell.X - 1, Y = cell.Y + 2, Value = cell.Value + 1 }); matrix[cell.X - 1, cell.Y + 2] = cell.Value + 1; } if (IsFreeCell(cell.X + 1, cell.Y + 2)) { queue.Enqueue(new Cell() { X = cell.X + 1, Y = cell.Y + 2, Value = cell.Value + 1 }); matrix[cell.X + 1, cell.Y + 2] = cell.Value + 1; } if (IsFreeCell(cell.X + 2, cell.Y + 1)) { queue.Enqueue(new Cell() { X = cell.X + 2, Y = cell.Y + 1, Value = cell.Value + 1 }); matrix[cell.X + 2, cell.Y + 1] = cell.Value + 1; } if (IsFreeCell(cell.X + 2, cell.Y - 1)) { queue.Enqueue(new Cell() { X = cell.X + 2, Y = cell.Y - 1, Value = cell.Value + 1 }); matrix[cell.X + 2, cell.Y - 1] = cell.Value + 1; } if (IsFreeCell(cell.X + 1, cell.Y - 2)) { queue.Enqueue(new Cell() { X = cell.X + 1, Y = cell.Y - 2, Value = cell.Value + 1 }); matrix[cell.X + 1, cell.Y - 2] = cell.Value + 1; } if (IsFreeCell(cell.X - 1, cell.Y - 2)) { queue.Enqueue(new Cell() { X = cell.X - 1, Y = cell.Y - 2, Value = cell.Value + 1 }); matrix[cell.X - 1, cell.Y - 2] = cell.Value + 1; } if (IsFreeCell(cell.X - 2, cell.Y - 1)) { queue.Enqueue(new Cell() { X = cell.X - 2, Y = cell.Y - 1, Value = cell.Value + 1 }); matrix[cell.X - 2, cell.Y - 1] = cell.Value + 1; } } int middleColumn = matrixColumns / 2; for (int i = 0; i < matrixRows; i++) { Console.WriteLine(matrix[i, middleColumn]); } }
private static void VisitCell(Cell c, int oR, int oC) { int newRow = c.Row + oR; int newCol = c.Col + oC; if(IsAvailable(newRow, newCol)) { int newVal = c.Val + 1; arr[newRow, newCol] = newVal; Cell newCell = new Cell(){Row = newRow, Col = newCol, Val = newVal}; q.Enqueue(newCell); } }