Created
July 18, 2014 12:39
-
-
Save bencoveney/cb95071e473f3791fe62 to your computer and use it in GitHub Desktop.
Lattice Paths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Numerics; | |
using System.Threading.Tasks; | |
namespace EulerProject | |
{ | |
class Program | |
{ | |
private const long gridSize = 20; | |
private static long[,] Grid; | |
[STAThread] | |
static void Main(string[] args) | |
{ | |
// Create the grid and assign the start point | |
Grid = new long[gridSize + 1, gridSize + 1]; | |
Grid[0, 0] = 1; | |
// For each grid depth | |
for (long i = 0; i < gridSize * 2; i++) | |
{ | |
Console.WriteLine("Depth {0}", i); | |
// For each point at the specified grid depth | |
for (long j = 0; j <= i; j++) | |
{ | |
long x = j; | |
long y = -(j - i); | |
// Only use cells from within the grid boundaries | |
if (x <= gridSize && y <= gridSize) | |
{ | |
// If on the right edge | |
if (x == gridSize) | |
{ | |
// All routes go down | |
Grid[x, y + 1] += Grid[x, y]; | |
} | |
else | |
{ | |
// If on the bottom edge | |
if (y == gridSize) | |
{ | |
// All routes go right | |
Grid[x + 1, y] += Grid[x, y]; | |
} | |
else | |
{ | |
// Both directions free, thus move in both; | |
Grid[x, y + 1] += Grid[x, y]; | |
Grid[x + 1, y] += Grid[x, y]; | |
} | |
} | |
Console.WriteLine("Cell ({0}, {1}) - Count: {2}", x, y, Grid[x,y]); | |
} | |
} | |
} | |
Console.WriteLine("Depth {0}", (gridSize * 2)+1); | |
Console.WriteLine("Cell ({0}, {1}) - Count: {2}", gridSize, gridSize, Grid[gridSize, gridSize]); | |
System.Windows.Forms.Clipboard.SetText(Grid[gridSize, gridSize].ToString()); // 137846528820 | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment