Algorithm for finding all paths in a NxN grid

前端 未结 10 1436
忘了有多久
忘了有多久 2020-11-28 08:27

Imagine a robot sitting on the upper left hand corner of an NxN grid. The robot can only move in two directions: right and down. How many possible paths are there for the ro

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 08:59

    Here is c# version (just for reference) to find unique paths (note here is the version which returns number of paths using dynamic programming (memorization - lazy) - Calculating number of moves from top left corner to bottom right with move in any direction) (you may refer to my blog for more details: http://codingworkout.blogspot.com/2014/08/robot-in-grid-unique-paths.html)

    Tuple[][] GetUniquePaths(int N)
            {
                var r = this.GetUniquePaths(1, 1, N);
                return r;
            }
            private Tuple[][] GetUniquePaths(int row, int column, int N)
            {
                if ((row == N) && (column == N))
                {
                    var r = new Tuple[1][];
                    r[0] = new Tuple[] { new Tuple(row, column) };
                    return r;
                }
                if ((row > N) || (column > N))
                {
                    return new Tuple[0][];
                }
                var uniquePathsByMovingDown = this.GetUniquePaths(row + 1, column, N);
                var uniquePathsByMovingRight = this.GetUniquePaths(row, column + 1, N);
                List[]> paths = this.MergePaths(uniquePathsByMovingDown,
                    row, column).ToList();
                paths.AddRange(this.MergePaths(uniquePathsByMovingRight, row, column));
                return paths.ToArray();
            }
    

    where

    private Tuple[][] MergePaths(Tuple[][] paths, 
                int row, int column)
            {
                Tuple[][] mergedPaths = new Tuple[paths.Length][];
                if (paths.Length > 0)
                {
                    Assert.IsTrue(paths.All(p => p.Length > 0));
                    for (int i = 0; i < paths.Length; i++)
                    {
                        List> mergedPath = new List>();
                        mergedPath.Add(new Tuple(row, column));
                        mergedPath.AddRange(paths[i]);
                        mergedPaths[i] = mergedPath.ToArray();
                    }
                }
                return mergedPaths;
            }
    

    Unit Tests

    [TestCategory(Constants.DynamicProgramming)]
            public void RobotInGridTests()
            {
                int p = this.GetNumberOfUniquePaths(3);
                Assert.AreEqual(p, 6);
                int p1 = this.GetUniquePaths_DP_Memoization_Lazy(3);
                Assert.AreEqual(p, p1);
                var p2 = this.GetUniquePaths(3);
                Assert.AreEqual(p1, p2.Length);
                foreach (var path in p2)
                {
                    Debug.WriteLine("===================================================================");
                    foreach (Tuple t in path)
                    {
                        Debug.Write(string.Format("({0}, {1}), ", t.Item1, t.Item2));
                    }
                }
                p = this.GetNumberOfUniquePaths(4);
                Assert.AreEqual(p, 20);
                p1 = this.GetUniquePaths_DP_Memoization_Lazy(4);
                Assert.AreEqual(p, p1);
                p2 = this.GetUniquePaths(4);
                Assert.AreEqual(p1, p2.Length);
                foreach (var path in p2)
                {
                    Debug.WriteLine("===================================================================");
                    foreach (Tuple t in path)
                    {
                        Debug.Write(string.Format("({0}, {1}), ", t.Item1, t.Item2));
                    }
                }
            }
    

提交回复
热议问题