Пример #1
0
    private static void Main()
    {
        string[] inputs = Console.ReadLine().Split(' ');
        int      starty = int.Parse(inputs[0]);
        int      startx = int.Parse(inputs[1]);

        inputs = Console.ReadLine().Split(' ');
        int endy = int.Parse(inputs[0]);
        int endx = int.Parse(inputs[1]);

        inputs = Console.ReadLine().Split(' ');
        int h = int.Parse(inputs[0]);
        int w = int.Parse(inputs[1]);

        var m = new Map();

        for (int mY = 0; mY < h; mY++)
        {
            var line = Console.ReadLine().ToCharArray();
            for (int mX = 0; mX < w; mX++)
            {
                if (line[mX] == '#')
                {
                    continue;
                }
                if (line[mX] == '.')
                {
                    Node n = new Node(new Point(mX, mY), ".");
                    m.Nodes.Add(n);
                    var northernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX && x.Coordinate.Y == mY - 1 &&
                            new List <string> {
                        "|", "."
                    }.Contains(x.Type));
                    if (northernNode != null)
                    {
                        var l = new Link(n, northernNode);
                        n.Links.Add(l);
                        northernNode.Links.Add(l);
                        n.Neighbors.Add(northernNode);
                        northernNode.Neighbors.Add(n);
                    }

                    Node westernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX - 1 && x.Coordinate.Y == mY &&
                            new List <string> {
                        "-", "."
                    }.Contains(x.Type));
                    if (westernNode != null)
                    {
                        var l = new Link(n, westernNode);
                        n.Links.Add(l);
                        westernNode.Links.Add(l);
                        n.Neighbors.Add(westernNode);
                        westernNode.Neighbors.Add(n);
                    }
                }
                else if (line[mX] == '+')
                {
                    Node n = new Node(new Point(mX, mY), "+");
                    m.Nodes.Add(n);
                    var northernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX && x.Coordinate.Y == mY - 1 &&
                            new List <string> {
                        "|", "+", "X"
                    }.Contains(x.Type));
                    if (northernNode != null)
                    {
                        var l = new Link(n, northernNode);
                        n.Links.Add(l);
                        northernNode.Links.Add(l);
                        n.Neighbors.Add(northernNode);
                        northernNode.Neighbors.Add(n);
                    }

                    Node westernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX - 1 && x.Coordinate.Y == mY &&
                            new List <string> {
                        "-", "+", "X"
                    }.Contains(x.Type));
                    if (westernNode != null)
                    {
                        var l = new Link(n, westernNode);
                        n.Links.Add(l);
                        westernNode.Links.Add(l);
                        n.Neighbors.Add(westernNode);
                        westernNode.Neighbors.Add(n);
                    }
                }
                else if (line[mX] == '|')
                {
                    Node n = new Node(new Point(mX, mY), "|");
                    m.Nodes.Add(n);
                    var northernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX && x.Coordinate.Y == mY - 1 &&
                            new List <string> {
                        ".", "+"
                    }.Contains(x.Type));
                    if (northernNode != null)
                    {
                        var l = new Link(n, northernNode);
                        n.Links.Add(l);
                        northernNode.Links.Add(l);
                        n.Neighbors.Add(northernNode);
                        northernNode.Neighbors.Add(n);
                    }
                }
                else if (line[mX] == '-')
                {
                    Node n = new Node(new Point(mX, mY), "-");
                    m.Nodes.Add(n);

                    Node westernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX - 1 && x.Coordinate.Y == mY &&
                            new List <string> {
                        "+", "."
                    }.Contains(x.Type));
                    if (westernNode != null)
                    {
                        var l = new Link(n, westernNode);
                        n.Links.Add(l);
                        westernNode.Links.Add(l);
                        n.Neighbors.Add(westernNode);
                        westernNode.Neighbors.Add(n);
                    }
                }
                else if (line[mX] == 'X')
                {
                    Node n1 = new Node(new Point(mX, mY), "X");
                    m.Nodes.Add(n1);
                    var northernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX && x.Coordinate.Y == mY - 1 &&
                            new List <string> {
                        "+", "X"
                    }.Contains(x.Type));
                    if (northernNode != null)
                    {
                        var l = new Link(n1, northernNode);
                        n1.Links.Add(l);
                        northernNode.Links.Add(l);
                        n1.Neighbors.Add(northernNode);
                        northernNode.Neighbors.Add(n1);
                    }

                    Node westernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX - 1 && x.Coordinate.Y == mY &&
                            new List <string> {
                        "+", "X"
                    }.Contains(x.Type));
                    if (westernNode != null)
                    {
                        var l = new Link(n1, westernNode);
                        n1.Links.Add(l);
                        westernNode.Links.Add(l);
                        n1.Neighbors.Add(westernNode);
                        westernNode.Neighbors.Add(n1);
                    }

                    Node n2 = new Node(new Point(mX, mY), ".");
                    m.Nodes.Add(n2);
                    northernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX && x.Coordinate.Y == mY - 1 &&
                            new List <string> {
                        "."
                    }.Contains(x.Type));
                    if (northernNode != null)
                    {
                        var l = new Link(n2, northernNode);
                        n2.Links.Add(l);
                        northernNode.Links.Add(l);
                        n2.Neighbors.Add(northernNode);
                        northernNode.Neighbors.Add(n2);
                    }

                    westernNode =
                        m.Nodes.Find(
                            x =>
                            x.Coordinate.X == mX - 1 && x.Coordinate.Y == mY &&
                            new List <string> {
                        "."
                    }.Contains(x.Type));
                    if (westernNode != null)
                    {
                        var l = new Link(n2, westernNode);
                        n2.Links.Add(l);
                        westernNode.Links.Add(l);
                        n2.Neighbors.Add(westernNode);
                        westernNode.Neighbors.Add(n2);
                    }
                }
            }
        }

        BreadthFirstSearch breadthFirstSearch = new BreadthFirstSearch();

        breadthFirstSearch.CalculateDistances(m.Nodes.Find(x => x.Coordinate.X == startx && x.Coordinate.Y == starty));
        NodeDistance exitNode = breadthFirstSearch.NodeDistances.First(x => x.Node.Coordinate.X == endx && x.Node.Coordinate.Y == endy);

        Console.WriteLine(exitNode.Distance);
    }