曼哈顿游客问题的对角线计算

时间:2019-04-06 18:14:08

标签: c# algorithm

我需要使用下,右和对角矩阵来计算一个Mahattan Tourist问题的最大距离。

仅向下和向右的计算将返回正确的值。 尝试计算最大距离(包括具有3x3矩阵的对角线路径)时,将返回错误的值。

我认为问题出在用Math.Max()计算最大距离的那一行。

对于矩阵,我使用了以下测试矩阵(输入文件如下所示):

G_down 2 3
  0.1   0.5   0.3
  0.9   0.4   0.7  
---
G_right 3 2
  0.3   0.2
  0.4   0.6
  0.8   0.9
---
G_diag 2 2
  5.6   9.4
  3.2   4.8
---
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//bsp 20.68
namespace MHTP
{
    class Program
    {
        public static double ManhattanProblem(double[,] RightMatrix, double[,] DownMatrix, double[,] DiagonalMatrix)
        {
            int n = RightMatrix.GetLength(0);
            int m = DownMatrix.GetLength(1);
            int d = DiagonalMatrix.GetLength(1);
            double[,] ManhattanMatrix = new double[n, m];

            ManhattanMatrix[0, 0] = 0;

            for (int i = 1; i < n; i++)
            {
                ManhattanMatrix[i, 0] = ManhattanMatrix[i - 1, 0] + DownMatrix[i - 1, 0];
            }

            for (int j = 1; j < m; j++)
            {
                ManhattanMatrix[0, j] = ManhattanMatrix[0, j - 1] + RightMatrix[0, j - 1];
            }

            /*for (int k = 1; k < d; k++)
            {
                ManhattanMatrix[k, 0] = ManhattanMatrix[k - 1, 0] + DiagonalMatrix[k - 1, 0];
            }*/

            for (int i = 1; i < n; i++)
            {
                for (int j = 1; j < m; j++)
                {
                        ManhattanMatrix[i, j] =
                        Math.Max(ManhattanMatrix[i - 1, j] + DownMatrix[i - 1, j],
                        Math.Max(ManhattanMatrix[i, j - 1] + RightMatrix[i, j - 1],
                        ManhattanMatrix[i, j] + DiagonalMatrix[i-1, j-1]));
                }
            }

            return ManhattanMatrix.Cast<double>().Max();
        }

        static void Main(string[] args)
        {
            String line;
            int flag = 0;

            //cleanup
            try
            {
                File.Delete(@"right.txt");
                File.Delete(@"down.txt");
                File.Delete(@"diagonal.txt");
            }
            catch (Exception e)
            {
                //Exception Handling
            }


            while ((line = Console.ReadLine()) != null)
            {
                if (line.Contains("---"))
                {
                    flag++;
                }
                if (flag == 0)
                {
                    using (StreamWriter w2 = File.AppendText("down.txt"))
                    {
                        if (line.Contains("-") || line.Contains("G_"))
                        {
                            continue;
                        }
                        else
                        {
                            System.Text.RegularExpressions.Regex.Replace(line, @"\s+", " ");
                            w2.WriteLine(line);
                        }
                    }
                }
                    if (flag == 1)
                {
                    using (StreamWriter w1 = File.AppendText("right.txt"))
                    {
                        if (line.Contains("-") || line.Contains("G_"))
                        {
                            continue;
                        }
                        else
                        {
                            System.Text.RegularExpressions.Regex.Replace(line, @"\s+", " ");
                            w1.WriteLine(line);
                        }
                    }
                }
                if (flag == 2)
                {
                    using (StreamWriter w3 = File.AppendText("diagonal.txt"))
                    {
                        if (line.Contains("-") || line.Contains("G_"))
                        {
                            continue;
                        }
                        else
                        {
                            System.Text.RegularExpressions.Regex.Replace(line, @"\s+", " ");
                            w3.WriteLine(line);
                        }
                    }
                }      
            }

            // right
            var linesRight = File.ReadAllLines(@"right.txt")
               .Where(x => !string.IsNullOrWhiteSpace(x))
               .Select(x => x.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                            .Select(double.Parse)
                            .ToList())
               .ToList();

            var hRight = linesRight.Count();
            var wRight = linesRight.Max(x => x.Count);
            var multiArrayRight = new double[hRight, wRight];

            for (var i = 0; i < linesRight.Count; i++)
                for (var j = 0; j < linesRight[i].Count; j++)
                    multiArrayRight[i, j] = linesRight[i][j];

            // down
            var linesDown = File.ReadAllLines(@"down.txt")
               .Where(x => !string.IsNullOrWhiteSpace(x))
               .Select(x => x.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                            .Select(double.Parse)
                            .ToList())
               .ToList();

            var hDown = linesDown.Count();
            var wDown = linesDown.Max(x => x.Count);
            var multiArrayDown = new double[hDown, wDown];

            for (var i = 0; i < linesDown.Count; i++)
                for (var j = 0; j < linesDown[i].Count; j++)
                    multiArrayDown[i, j] = (linesDown[i][j]);

            // diagonal
            var linesDiagonal = File.ReadAllLines(@"diagonal.txt")
               .Where(x => !string.IsNullOrWhiteSpace(x))
               .Select(x => x.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                            .Select(double.Parse)
                            .ToList())
               .ToList();

            var hDiagonal = linesDiagonal.Count();
            var wDiagonal = linesDiagonal.Max(x => x.Count);
            var multiArrayDiagonal = new double[hDiagonal, wDiagonal];

            for (var i = 0; i < linesDiagonal.Count; i++)
                for (var j = 0; j < linesDiagonal[i].Count; j++)
                    multiArrayDiagonal[i, j] = linesDiagonal[i][j];

            double resultManhattan = ManhattanProblem(multiArrayRight, multiArrayDown, multiArrayDiagonal);
            Console.WriteLine(resultManhattan);

        }
    }
}

预期结果为10.4,我得到10.1。

1 个答案:

答案 0 :(得分:0)

乍一看像下面这样:

print("After", yr, "year(s) your balance will be $", compute_compound_interest(ibal, ir, yr),'.')

应为:

ManhattanMatrix[i, j] + DiagonalMatrix[i-1, j-1]));