Python转C#路径求和程序故障:最小路径和计算错误求助
从数组中心到边界的最小路径和问题(C#实现修复)
需要将Python代码转换为C#,多次尝试均失败,任务需明日提交。目标是实现:从数组中心出发,仅允许上下左右移动且不可访问已走过的点,找到到数组边界(上下左右任意边界)的元素和最小的路径。当前编写的C#程序无法正确计算最小路径和,路径搜索逻辑存在错误,原代码及修复后的实现如下:
原错误代码
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { public struct Point { public int X; public int Y; public Point(int x, int y) { X = x; Y = y; } } static int n, m; static int[][] matrix; static Dictionary<Point, Point> points; static int[][] recalculatedMatrix; static void Main() { ReadInput(); points = new Dictionary<Point, Point>(); recalculatedMatrix = new int[n][]; for (int i = 0; i < n; i++) { recalculatedMatrix[i] = new int[m]; for (int j = 0; j < m; j++) { recalculatedMatrix[i][j] = int.MaxValue; points[new Point(i, j)] = new Point(-1, -1); } } int nowPointX = m / 2; int nowPointY = n / 2; recalculatedMatrix[nowPointY][nowPointX] = matrix[nowPointY][nowPointX]; points[new Point(nowPointY, nowPointX)] = new Point(nowPointY, nowPointX); int layer = n / 2 - 1; while (layer >= 0) { FillingLine(layer, layer, layer + 1); FillingColumn(layer, layer, layer + 1); FillingColumn(n - layer - 1, layer, n - layer - 2); FillingLine(n - layer - 1, layer, n - layer - 2); if (recalculatedMatrix[layer][layer + 1] < recalculatedMatrix[layer + 1][layer]) { recalculatedMatrix[layer][layer] = recalculatedMatrix[layer][layer + 1] + matrix[layer][layer]; points[new Point(layer, layer)] = new Point(layer, layer + 1); RecountColumnValues(layer, layer + 1, n - layer - 1); } else { recalculatedMatrix[layer][layer] = recalculatedMatrix[layer + 1][layer] + matrix[layer][layer]; points[new Point(layer, layer)] = new Point(layer + 1, layer); RecountRowValues(layer, layer + 1, n - layer - 1); } if (recalculatedMatrix[n - layer - 1][layer + 1] < recalculatedMatrix[n - layer - 2][layer]) { recalculatedMatrix[n - layer - 1][layer] = recalculatedMatrix[n - layer - 1][layer + 1] + matrix[n - layer - 1][layer]; points[new Point(n - layer - 1, layer)] = new Point(n - layer - 1, layer + 1); RecountColumnValues(layer, n - layer - 2, layer); } else { recalculatedMatrix[n - layer - 1][layer] = recalculatedMatrix[n - layer - 2][layer] + matrix[n - layer - 1][layer]; points[new Point(n - layer - 1, layer)] = new Point(n - layer - 2, layer); RecountRowValues(n - layer - 1, layer + 1, n - layer - 1); } if (recalculatedMatrix[n - layer - 1][n - layer - 2] < recalculatedMatrix[n - layer - 2][n - layer - 1]) { recalculatedMatrix[n - layer - 1][n - layer - 1] = recalculatedMatrix[n - layer - 1][n - layer - 2] + matrix[n - layer - 1][n - layer - 1]; points[new Point(n - layer - 1, n - layer - 1)] = new Point(n - layer - 1, n - layer - 2); RecountColumnValues(n - layer - 1, n - layer - 2, layer); } else { recalculatedMatrix[n - layer - 1][n - layer - 1] = recalculatedMatrix[n - layer - 2][n - layer - 1] + matrix[n - layer - 1][n - layer - 1]; points[new Point(n - layer - 1, n - layer - 1)] = new Point(n - layer - 2, n - layer - 1); RecountRowValues(n - layer - 1, n - layer - 2, layer); } layer -= 1; int minWay = GetMinWay(); Console.WriteLine("Minimal way: " + minWay); var minPath = GetMinPath(); Console.WriteLine("Minimal path: "); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (j != m - 1) { if (minPath.Contains(new Point(i, j))) { Console.Write($"-{matrix[i][j]} "); } else { Console.Write($"{matrix[i][j]} "); } } else { if (minPath.Contains(new Point(i, j))) { Console.Write($"-{matrix[i][j]} "); } else { Console.Write($"{matrix[i][j]}\n"); } } } Console.WriteLine(); } } static void ReadInput() { string[] lines = File.ReadAllLines("input.txt"); string[] firstLine = lines[0].Split(); n = int.Parse(firstLine[0]); m = int.Parse(firstLine[1]); matrix = new int[n][]; for (int i = 0; i < n; i++) { string[] row = lines[i + 1].Split(); matrix[i] = new int[m]; for (int j = 0; j < m; j++) { matrix[i][j] = int.Parse(row[j]); } } } static void FillingLine(int row, int layer, int previousLine) { int nowPointX = 0; int nowPointY = 0; int minDistance = int.MaxValue; for (int elemFromLine = layer; elemFromLine < m - layer; elemFromLine++) { int recalculatedValue = recalculatedMatrix[previousLine][elemFromLine] + matrix[row][elemFromLine]; if (recalculatedValue < minDistance) { minDistance = recalculatedValue; nowPointX = elemFromLine; nowPointY = row; } } recalculatedMatrix[nowPointY][nowPointX] = minDistance; points[new Point(nowPointY, nowPointX)] = new Point(previousLine, nowPointX); for (int i = nowPointX + 1; i < n - layer - 1; i++) { if (recalculatedMatrix[row][i - 1] < recalculatedMatrix[previousLine][i]) { recalculatedMatrix[row][i] = recalculatedMatrix[row][i - 1] + matrix[row][i]; points[new Point(row, i)] = new Point(row, i - 1); } else { recalculatedMatrix[row][i] = recalculatedMatrix[previousLine][i] + matrix[row][i]; points[new Point(row, i)] = new Point(previousLine, i); } } for (int i = nowPointX - 1; i >= layer; i--) { if (recalculatedMatrix[row][i + 1] < recalculatedMatrix[previousLine][i]) { recalculatedMatrix[row][i] = recalculatedMatrix[row][i + 1] + matrix[row][i]; points[new Point(row, i)] = new Point(row, i + 1); } else { recalculatedMatrix[row][i] = recalculatedMatrix[previousLine][i] + matrix[row][i]; points[new Point(row, i)] = new Point(previousLine, i); } } } static void FillingColumn(int column, int layer, int previousColumn) { int nowPointX = 0; int nowPointY = 0; int minDistance = int.MaxValue; for (int elemFromColumn = layer; elemFromColumn < m - layer; elemFromColumn++) { int recalculatedValue = recalculatedMatrix[elemFromColumn][previousColumn] + matrix[elemFromColumn][column]; if (recalculatedValue < minDistance) { minDistance = recalculatedValue; nowPointX = column; nowPointY = elemFromColumn; } } recalculatedMatrix[nowPointY][nowPointX] = minDistance; points[new Point(nowPointY, nowPointX)] = new Point(nowPointY, previousColumn); for (int i = nowPointY + 1; i < n - layer - 1; i++) { if (recalculatedMatrix[i - 1][column] < recalculatedMatrix[i][previousColumn]) { recalculatedMatrix[i][column] = recalculatedMatrix[i - 1][column] + matrix[i][column]; points[new Point(i, column)] = new Point(i - 1, column); } else { recalculatedMatrix[i][column] = recalculatedMatrix[i][previousColumn] + matrix[i][column]; points[new Point(i, column)] = new Point(i, previousColumn); } } for (int i = nowPointY - 1; i >= layer; i--) { if (recalculatedMatrix[i + 1][column] < recalculatedMatrix[i][previousColumn]) { recalculatedMatrix[i][column] = recalculatedMatrix[i + 1][column] + matrix[i][column]; points[new Point(i, column)] = new Point(i + 1, column); } else { recalculatedMatrix[i][column] = recalculatedMatrix[i][previousColumn] + matrix[i][column]; points[new Point(i, column)] = new Point(i, previousColumn); } } } static void RecountRowValues(int row, int startPoint, int endPoint) { int step = 1; if (startPoint > endPoint) { step = -1; int temp = startPoint; startPoint = endPoint; endPoint = temp; } for (int i = startPoint; i != endPoint + step; i += step) { if (recalculatedMatrix[row][i - step] + matrix[row][i] >= recalculatedMatrix[row][i]) { break; } recalculatedMatrix[row][i] = recalculatedMatrix[row][i - step] + matrix[row][i]; points[new Point(row, i)] = new Point(row, i - step); } } static void RecountColumnValues(int column, int startPoint, int endPoint) { int step = 1; if (startPoint > endPoint) { step = -1; int temp = startPoint; startPoint = endPoint; endPoint = temp; } for (int i = startPoint; i != endPoint + step; i += step) { if (recalculatedMatrix[i - step][column] + matrix[i][column] >= recalculatedMatrix[i][column]) { break; } recalculatedMatrix[i][column] = recalculatedMatrix[i - step][column] + matrix[i][column]; points[new Point(i, column)] = new Point(i - step, column); } } static int GetMinWay() { var minUpperLine = Enumerable.Range(0, n).Select(i => new { Value = recalculatedMatrix[0][i], Point = new Point(0, i) }).MinBy(x => x.Value); var minBottomLine = Enumerable.Range(0, n).Select(i => new { Value = recalculatedMatrix[n - 1][i], Point = new Point(n - 1, i) }).MinBy(x => x.Value); var minLeftColumn = Enumerable.Range(0, n).Select(i => new { Value = recalculatedMatrix[i][0], Point = new Point(i, 0) }).MinBy(x => x.Value); var minRightColumn = Enumerable.Range(0, n).Select(i => new { Value = recalculatedMatrix[i][n - 1], Point = new Point(i, n - 1) }).MinBy(x => x.Value); var minValues = new[] { minUpperLine, minBottomLine, minLeftColumn, minRightColumn }; var overallMin = minValues.MinBy(x => x.Value); return overallMin.Value; } static List<Point> GetMinPath() { var minUpperLine = Enumerable.Range(0, n).Select(i => new { Value = recalculatedMatrix[0][i], Point = new Point(0, i) }).MinBy(x => x.Value).Point; var minBottomLine = Enumerable.Range(0, n).Select(i => new { Value = recalculatedMatrix[n - 1][i], Point = new Point(n - 1, i) }).MinBy(x => x.Value).Point; var minLeftColumn = Enumerable.Range(0, n).Select(i => new { Value = recalculatedMatrix[i][0], Point = new Point(i, 0) }).MinBy(x => x.Value).Point; var minRightColumn = Enumerable.Range(0, n).Select(i => new { Value = recalculatedMatrix[i][n - 1], Point = new Point(i, n - 1) }).MinBy(x => x.Value).Point; return new List<Point> { minUpperLine, minBottomLine, minLeftColumn, minRightColumn }; } } }
修复后的代码
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { public struct Point : IEquatable<Point> { public int X; public int Y; public Point(int x, int y) { X = x; Y = y; } public bool Equals(Point other) { return X == other.X && Y == other.Y; } public override bool Equals(object obj) { return obj is Point other && Equals(other); } public override int GetHashCode() { return HashCode.Combine(X, Y); } public static bool operator ==(Point left, Point right) { return left.Equals(right); } public static bool operator !=(Point left, Point right) { return !left.Equals(right); } } static int n, m; static int[][] matrix; static Dictionary<Point, Point> points; static int[][] recalculatedMatrix; static void Main() { ReadInput(); points = new Dictionary<Point, Point>(); recalculatedMatrix = new int[n][]; for (int i = 0; i < n; i++) { recalculatedMatrix[i] = new int[m]; for (int j = 0; j < m; j++) { recalculatedMatrix[i][j] = int.MaxValue; points[new Point(i, j)] = new Point(-1, -1); } } int centerX = m / 2; int centerY = n / 2; recalculatedMatrix[centerY][centerX] = matrix[centerY][centerX]; points[new Point(centerY, centerX)] = new Point(centerY, centerX); int layer = n / 2 - 1; while (layer >= 0) { FillingLine(layer, layer, layer + 1); FillingColumn(layer, layer, layer +
相关产品推荐
相关产品推荐

