// C# code to find number of ways in a maze
// with blocked cells using BFS
using System;
using System.Collections.Generic;
class GfG {
// Function to find ways with blocked cells
static int findWays(int[,] mat) {
int n = mat.GetLength(0), m = mat.GetLength(1);
// If starting or ending cell is blocked, return 0
if (mat[0, 0] == -1 || mat[n - 1, m - 1] == -1) {
return 0;
}
// Queue to store the current cell
Queue<(int, int)> q = new Queue<(int, int)>();
q.Enqueue((0, 0));
// Variable to store the number of ways
int ways = 0;
// Directions for moving right and down
int[][] directions = new int[][] {
new int[] { 1, 0 },
new int[] { 0, 1 }
};
while (q.Count > 0) {
var (i, j) = q.Dequeue();
// If reached the bottom-right cell, increment ways
if (i == n - 1 && j == m - 1) {
ways++;
continue;
}
// Explore adjacent cells (right and down)
foreach (var dir in directions) {
int ni = i + dir[0], nj = j + dir[1];
// Check if within bounds and not blocked
if (ni >= 0 && ni < n && nj >= 0
&& nj < m && mat[ni, nj] != -1) {
q.Enqueue((ni, nj));
}
}
}
return ways;
}
static void Main() {
int[,] mat = { { 0, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, 0 } };
Console.WriteLine(findWays(mat));
}
}