Project Euler #15: Lattice paths

  • + 0 comments

    for C#

    using System;

    class Program { const int MOD = 1000000007;

    static int CountRoutes(int N, int M)
    {
        int[,] dp = new int[N + 1, M + 1];
    
        for (int i = 0; i <= N; i++)
        {
            for (int j = 0; j <= M; j++)
            {
                if (i == 0 || j == 0)
                {
                    dp[i, j] = 1;
                }
                else
                {
                    dp[i, j] = (dp[i - 1, j] + dp[i, j - 1]) % MOD;
                }
            }
        }
    
        return dp[N, M];
    }
    
    static void Main()
    {
        int T = int.Parse(Console.ReadLine());
    
        for (int t = 0; t < T; t++)
        {
            string[] input = Console.ReadLine().Split();
            int N = int.Parse(input[0]);
            int M = int.Parse(input[1]);
    
            int result = CountRoutes(N, M);
            Console.WriteLine(result);
        }
    }
    

    }