从 C# 中的文本文件中读取矩阵

我需要读取一个包含矩阵的文本文件,而不是像我一样将它放在代码中,但我尝试过但我无法弄清楚。我在文本文件上有矩阵,就像我在代码上有的一样,但没有昏迷


这是一个dijkstra程序,它读取矩阵以找到最短路径,谢谢


{


    private static int MinimumDistance(int[] distance, bool[] shortestPathTreeSet, int verticesCount)

    {

        int min = int.MaxValue;

        int minIndex = 0;


        for (int v = 0; v < verticesCount; ++v)

        {

            if (shortestPathTreeSet[v] == false && distance[v] <= min)

            {

                min = distance[v];

                minIndex = v;

            }

        }


        return minIndex;

    }


    private static void Print(int[] distance, int verticesCount)

    {

        Console.WriteLine("Vertex    Distance from source");


        for (int i = 0; i < verticesCount; ++i)

            Console.WriteLine("{0}\t  {1}", i, distance[i]);

    }


    public static void DijkstraAlgo(int[,] graph, int source, int verticesCount)

    {

        int[] distance = new int[verticesCount];

        bool[] shortestPathTreeSet = new bool[verticesCount];


        for (int i = 0; i < verticesCount; ++i)

        {

            distance[i] = int.MaxValue;

            shortestPathTreeSet[i] = false;

        }


        distance[source] = 0;


        for (int count = 0; count < verticesCount - 1; ++count)

        {

            int u = MinimumDistance(distance, shortestPathTreeSet, verticesCount);

            shortestPathTreeSet[u] = true;


            for (int v = 0; v < verticesCount; ++v)

                if (!shortestPathTreeSet[v] && Convert.ToBoolean(graph[u, v]) && distance[u] != int.MaxValue && distance[u] + graph[u, v] < distance[v])

                    distance[v] = distance[u] + graph[u, v];

        }


        Print(distance, verticesCount);

    }


这是文本文件矩阵:


0 6 8 12 0 0 0 0 0 0

6 0 0 5 0 0 0 0 0 0

8 0 0 1 0 0 0 0 0 0

12 5 1 0 9 10 14 16 15 0

0 0 0 3 0 0 0

0 0 0 10 0 0 0 0 13 0

0 0 0 14 3 0 0 3 0 6

0 0 0 16 0 0 3 0 1 4

0 0 0 15 0 13 0 1 0 7

0 0 0 0 0 4 0


人到中年有点甜
浏览 267回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP