struct Point { publicPoint(int x, int y) { X = x; Y = y; }
publicint X { get; set; } publicint Y { get; set; } }
staticboolGetPath(int x, int y, List<Point> path, Dictionary<Point, bool> cache) { Point point = new Point(x, y); if (cache.ContainsKey(point)) { // Already visited this cell return cache[point]; }
if (x == 0 && y == 0) { // found a path returntrue; }
bool success = false;
// Try left if (x >= 1 && IsFree(x - 1, y)) { success = GetPath(x - 1, y, path, cache); }
// Try up if (!success && y >= 1 && IsFree(x, y - 1)) { success = GetPath(x, y - 1, path, cache); }
if (success) { // Right way! Add to path path.Add(point); }
// Cache result cache.Add(point, success);
return success; }
privatestaticboolIsFree(int x, int y) { return !(x == 0 && y == 1 || x == 0 && y == 2 || x == 1 && y == 2 || x == 2 && y == 2); }