// C# Code to count walks from src to dest
// with exactly k edges using DP
using System;
class GfG {
// Function to count all walks from src to dest
// using exactly k edges with bottom-up DP
public static int countWalks(int[][] adj,
int src, int dest, int k) {
int v = adj.Length;
// dp[e][i][j]: number of walks from i to j using e edges
int[,,] dp = new int[k + 1, v, v];
// Base case: walks from i to j with 0 edges
for (int i = 0; i < v; i++) {
dp[0, i, i] = 1;
}
// Build the table for all edge counts from 1 to k
for (int e = 1; e <= k; e++) {
for (int i = 0; i < v; i++) {
for (int j = 0; j < v; j++) {
// Consider all intermediate vertices
for (int a = 0; a < v; a++) {
// If there's an edge from i to a
if (adj[i][a] == 1) {
dp[e, i, j] += dp[e - 1, a, j];
}
}
}
}
}
return dp[k, src, dest];
}
static void Main() {
int[][] adj = new int[][] {
new int[] {0, 1, 1, 1},
new int[] {0, 0, 0, 1},
new int[] {0, 0, 0, 1},
new int[] {0, 0, 0, 0}
};
int src = 0, dest = 3, k = 2;
Console.WriteLine(countWalks(adj, src, dest, k));
}
}