using System;
using System.Collections.Generic;
class MainClass
{
// Function to check if coordinates are valid
static bool isValid(int x, int y, int N)
{
return x >= 0 && x < N && y >= 0 && y < N;
}
// Function to find the shortest path from the first
// cell to the last cell
static int shortestPath(int[][] matrix, int N)
{
bool[,] visited = new bool[N, N];
int[,] distance = new int[N, N];
Queue<int[]> queue = new Queue<int[]>();
queue.Enqueue(new int[] { 0, 0 });
visited[0, 0] = true;
// Start Iterating
while (queue.Count > 0)
{
int[] cell = queue.Dequeue();
int x = cell[0];
int y = cell[1];
int k = matrix[x][y];
if (x == N - 1 && y == N - 1)
{
return distance[x, y];
}
int[] dx = { k, -k, 0, 0 };
int[] dy = { 0, 0, k, -k };
for (int i = 0; i < 4; i++)
{
int newX = x + dx[i];
int newY = y + dy[i];
// If coordinates are valid
if (isValid(newX, newY, N) && !visited[newX, newY])
{
queue.Enqueue(new int[] { newX, newY });
visited[newX, newY] = true;
distance[newX, newY] = distance[x, y] + 1;
}
}
}
return -1;
}
// Driver code
public static void Main(string[] args)
{
int N = 3;
int[][] matrix =
{
new[] { 1, 1, 1 },
new[] { 1, 1, 1 },
new[] { 1, 1, 1 }
};
// Function call
int result = shortestPath(matrix, N);
Console.WriteLine(result);
}
}