private static void FindPath(int row, int col) { if (row == AllPaths.goalRowCoordinate && col == AllPaths.goalColCoordinate) { AllPaths.validPathsCount++; return; } if (row + 1 < AllPaths.MatrixSize && AllPaths.matrix[row + 1, col]) { AllPaths.matrix[row + 1, col] = false; AllPaths.FindPath(row + 1, col); AllPaths.matrix[row + 1, col] = true; } if (col + 1 < AllPaths.MatrixSize && AllPaths.matrix[row, col + 1]) { AllPaths.matrix[row, col + 1] = false; AllPaths.FindPath(row, col + 1); AllPaths.matrix[row, col + 1] = true; } if (row - 1 >= 0 && AllPaths.matrix[row - 1, col]) { AllPaths.matrix[row - 1, col] = false; AllPaths.FindPath(row - 1, col); AllPaths.matrix[row - 1, col] = true; } if (col - 1 >= 0 && AllPaths.matrix[row, col - 1]) { AllPaths.matrix[row, col - 1] = false; AllPaths.FindPath(row, col - 1); AllPaths.matrix[row, col - 1] = true; } }
public static void Main() { AllPaths.FindPath(0, 0); Console.WriteLine(AllPaths.validPathsCount); }