【ProjectEuler】ProjectEuler_015

// Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner.
// 
// 
// How many routes are there through a 2020 grid?

using System;
using System.Collections.Generic;
using System.Text;

namespace projecteuler015
{
    class Program
    {
        static long[][] data = new long[20][];
        static void Main(string[] args)
        {
            F1();
        }

        private static void F1()
        {
            Console.WriteLine(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod());
            DateTime timeStart = DateTime.Now;

            /*
             * 草稿区域
             * 1*1 = 1+1 = 2
             * 1*2 = 1+2 = 3
             * 1*3 = 1+3 = 4
             * 2*2 = 1*2 + 2*1 = 6
             * 2*3 = 1*3 + 2*2 = 4 + 6 = 8 
             * 
             */

            for (int i = 0; i < 20; i++)
            {
                data[i] = new long[20];
            }

            Console.WriteLine(GetRouteCount(20, 20));

            Console.WriteLine("Total Milliseconds is " + DateTime.Now.Subtract(timeStart).TotalMilliseconds + "\n\n");
        }

        /// <summary>
        /// 获取x*y的方框的路径数
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        private static long GetRouteCount(int x, int y)
        {
            //使x小于y
            if (x > y)
            {
                int t = x;
                x = y;
                y = t;
            }

            if (data[x-1][y-1] != 0)
            {
                return data[x-1][y-1];
            }

            if (x == 1)
            {
                data[x-1][y-1] = x + y;
                return data[x-1][y-1];
            }
            else
            {
                data[x-1][y-1] = GetRouteCount(x - 1, y) + GetRouteCount(x, y - 1);
                return data[x-1][y-1];
            }
        }
    }
}

/*
Void F1()
137846528820
Total Milliseconds is 10.0012

By GodMoon
*/

你可能感兴趣的:(【ProjectEuler】ProjectEuler_015)