Ejemplo n.º 1
0
        public void TestMethod1()
        {
            MyListGraph graph = new MyListGraph();

            graph.AddNode(new MyGraphNode(0));
            graph.AddNode(new MyGraphNode(1));
            graph.AddNode(new MyGraphNode(2));
            graph.AddNode(new MyGraphNode(3));
            graph.AddNode(new MyGraphNode(4));

            graph.AddEdge(new MyGraphEdge(0, 2));
            graph.AddEdge(new MyGraphEdge(2, 1));

            graph.AddEdge(new MyGraphEdge(2, 3));
            graph.AddEdge(new MyGraphEdge(4, 3));

            Assert.AreEqual(1, graph.SoMienDoThiLienThong());
        }
Ejemplo n.º 2
0
        public bool ReadMap(string fileaddress)
        {
            string line;

            string[] words;

            // Read the file and display it line by line.
            StreamReader file = new System.IO.StreamReader(fileaddress);

            // read width and height
            words = file.ReadLine().Split(' ');

            // if file doesnt containt width and height value, return false;
            if (words.Count() != 2)
            {
                return(false);
            }

            short f_MapWidth, f_MapHeight;

            if (!Int16.TryParse(words[0], out f_MapWidth) ||
                !Int16.TryParse(words[1], out f_MapHeight))
            {
                return(false);
            }

            MyMapNode[,] temp_map = new MyMapNode[f_MapWidth, f_MapHeight];
            // read map infor
            for (int i = 0; i < f_MapHeight; i++)
            {
                words = file.ReadLine().Split(' ');
                for (int j = 0; j < f_MapWidth; j++)
                {
                    MyMapNode node = new MyMapNode();
                    node.x = i;
                    node.y = j;

                    // Add Node
                    node.index           = m_cGraph.AddNode(new MyGraphNode());
                    temp_map[i, j]       = new MyMapNode();
                    temp_map[i, j].index = node.index;
                    node.cost            = float.Parse(words[j]);
                    temp_map[i, j].cost  = node.cost;

                    // Add Edge
                    if (node.cost > 0)
                    {
                        CheckToAddEdge(f_MapWidth, temp_map, i, j);
                    }

                    if (i == 0)
                    {
                        Console.Write(node.index.ToString() + "  ");
                    }
                    else
                    {
                        Console.Write(node.index.ToString() + ' ');
                    }
                }
                Console.WriteLine();
            }

            file.Close();

            return(true);
        }