Beispiel #1
0
        public string GetPath()
        {
            int   firstId    = _rows[_from].GetId();
            int   secondId   = _rows[_from].GetElement(_to).GetId();
            ICity firstCity  = null;
            ICity secondCity = null;

            for (int i = 0; i < _graph.Count; i++)
            {
                ICity currentCity1 = _graph[i];
                if (currentCity1.Id == firstId)
                {
                    firstCity = currentCity1;
                }
                if (currentCity1.Id == secondId)
                {
                    secondCity = currentCity1;
                }
            }
            if (firstCity == null | secondCity == null)
            {
                return("");
            }
            int sum = firstCity.GetDistTo(secondCity.Id);

            if (sum >= 5000)
            {
                return("Отсутствует какой-либо путь между данными вершинами");
            }
            string        path        = "";
            ICityStorage  qeue        = secondCity.GetNeighbors();//new
            ICity         currentCity = secondCity;
            List <string> pathList    = new List <string>();

            while (sum > 0)
            {
                for (int i = 0; i < qeue.Count; i++)
                {
                    ICity tmpCity = qeue[i];
                    int   weight  = tmpCity.GetWeightToNeighbor(currentCity.Id);
                    if (firstCity.GetDistTo(tmpCity.Id) == firstCity.GetDistTo(currentCity.Id) - weight)
                    {
                        //path += currentCity.GetId() + " <- ";
                        pathList.Add(currentCity.Id.ToString());
                        sum        -= weight;
                        currentCity = tmpCity;
                        break;
                    }
                }
                qeue = currentCity.GetNeighbors(); //new
            }
            for (int i = pathList.Count - 1; i >= 0; i--)
            {
                path += " -> " + pathList[i];
            }
            return(firstCity.Id.ToString() + path);
        }
        public void Draw(Graphics g)
        {
            Font       font  = new Font("Times New Roman", _width / 3 - 1);//Arial
            SolidBrush brush = new SolidBrush(Color.Green);

            int x = _startX;
            int y = _startY;

            for (int i = 0; i <= _size; i++)
            {
                x += _width;
                if (i < _size)
                {
                    g.DrawString(_graph[i].Id.ToString(), font, brush, new Point(x, y));
                    g.DrawString(_graph[i].Id.ToString(), font, brush, new Point(y, x));
                }
                else
                {
                    g.DrawString("Sum", font, brush, new Point(x, y));
                }

                g.DrawLine(new Pen(Color.Black, 2), new Point(x, y), new Point(x, _width * (_size + 1) + _startY));
                g.DrawLine(new Pen(Color.Black, 2), new Point(y, x), new Point(_width * (_size + 2) + _startX, x));
            }
            x     = _startX + _width;
            y     = _startY + _width;
            brush = new SolidBrush(Color.Black);
            for (int i = 0; i < _size; i++)
            {
                ICity MainCity   = _graph[i];
                int   currentSum = 0;

                for (int j = 0; j <= _size; j++)
                {
                    int value;
                    if (j < _size)
                    {
                        ICity Guest = _graph[j];
                        value       = MainCity.GetDistTo(Guest.Id);
                        currentSum += value;
                    }
                    else
                    {
                        value = currentSum;
                    }
                    g.DrawString(value.ToString(), font, brush, new Point(x, y));
                    x += _width;
                }
                x  = _startX + _width;
                y += _width;
            }
        }