static private void AddNeighbors(int[][] matrix, Queue <XYCor> todo, XYCor current) { int x = current.x; int y = current.y; if (x - 1 > -1 && matrix[x - 1][y] == 1) { XYCor temp = new XYCor(x - 1, y); todo.Enqueue(temp); } if (x + 1 < matrix.Length && matrix[x + 1][y] == 1) { XYCor temp = new XYCor(x + 1, y); todo.Enqueue(temp); } if (y - 1 > -1 && matrix[x][y - 1] == 1) { XYCor temp = new XYCor(x, y - 1); todo.Enqueue(temp); } if (y + 1 < matrix.Length && matrix[x][y + 1] == 1) { XYCor temp = new XYCor(x, y + 1); todo.Enqueue(temp); } }
static private void FindRegion(int[][] matrix, XYCor start) { Queue <XYCor> todo = new Queue <XYCor>(); todo.Enqueue(start); while (todo.Count() != 0) { XYCor current = todo.Dequeue(); AddNeighbors(matrix, todo, current); matrix[current.x][current.y] = -1; } }
static void Main(string[] args) { //process input into assci values string inputStringPrefix = "hfdlxzhv"; int[][] matrix = new int[128][]; FillMatrix(matrix, inputStringPrefix); int count = 0; for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < matrix[0].Length; j++) { if (matrix[i][j] == 1) { count++; XYCor start = new XYCor(i, j); FindRegion(matrix, start); } } } Console.WriteLine(count); }