Exemplo n.º 1
0
        static void Main(string[] args)
        {
            #region Parse input
            outputMode           = Console.ReadLine() == "0" ? false : true;
            int height           = int.Parse(Console.ReadLine());
            int targetY          = int.Parse(Console.ReadLine());
            int targetX          = int.Parse(Console.ReadLine());
            targetLocation       = new Point() { X = targetX, Y = targetY };
            var initialConfig = new string[height];
            for (int i = 0; i < height; i++)
                initialConfig[i] = Console.ReadLine();
            map = new Map(initialConfig);
            #endregion

            // Here goes parallel search for the holy grail
            var tree = new Tree(map);

            // testing
            // var move1 = map.makeMove(Globals.targetCar, Direction.Down, 1);
            // var move2 = map.makeMove(Globals.targetCar, Direction.Down, 2).makeMove(Globals.targetCar, Direction.Up, 1);
            // tree.AddNeighbor(map, move1);
            // var test = tree.Find(move2);
            // Console.WriteLine("It does {0}work", test == null ? "not " : "");

            var queue = new ConcurrentQueue<Tuple<Map, char>>();
            queue.Enqueue(new Tuple<Map, char>(map, '.'));
            Map solution = null;
            while (solution == null) solution = Iterate(queue, tree);
            if (solution != Globals.NoSolutions) {
                // Winning solution inside
                Console.WriteLine("We found a winning solution");
                Console.WriteLine(solution.ToString());
                Console.WriteLine("Testing shortest path lookup");
                var stack = tree.FindShortest(solution);
                while (stack.Count > 0)
                    Console.WriteLine(stack.Pop().value);
            }
            else {
                // No solutions
                Console.WriteLine("We did not find a winning solution");
            }

            // Console.WriteLine("Tree debugging");
            // var treeQueue = new Queue<Node<Map>>();
            // var visited = new List<Node<Map>>();
            // treeQueue.Enqueue(tree.root);
            // using (var writer = new StreamWriter("Tree.txt")) {
            //     while (treeQueue.Count > 0) {
            //         var current = treeQueue.Dequeue();
            //         visited.Add(current);
            //         writer.WriteLine(current.value);
            //         foreach (var nb in current.neighbors.Where(x => !visited.Contains(x))) treeQueue.Enqueue(nb);
            //     }
            // }
            //Iterate(ref queue);
            //Console.WriteLine(queue.Count());
            //foreach (var m in queue) {
            //    Console.WriteLine("Permutation of input:");
            //    Console.WriteLine(m.Item1.ToString());
            //}
            // Testing input parsing
            while (true) {
                Console.WriteLine(map.ToString());
                Console.WriteLine("Please enter a command:");
                var input = Console.ReadLine();
                if (input.StartsWith("M")) {
                    Direction dir = Direction.Default;
                    switch(input[4]){
                        case 'U':
                            dir = Direction.Up;
                            break;
                        case 'D':
                            dir = Direction.Down;
                            break;
                        case 'L':
                            dir = Direction.Left;
                            break;
                        case 'R':
                            dir = Direction.Right;
                            break;
                    }
                    var temp = map.makeMove(input[2], dir, int.Parse(input[6].ToString()));
                    if (temp != null) map = temp;

                    continue;
                }

                if (input.StartsWith("L")) {
                    foreach (var kvp in map.Parse())
                        Console.WriteLine("Car {0}'s top-left is at {1}, with a {3} orientation and length {2}.", kvp.Key, kvp.Value.Item1, kvp.Value.Item2, kvp.Value.Item3 == Direction.Down ? "vertical" : "horizontal");
                    continue;
                }

                if (input.StartsWith("H")) {
                    Console.WriteLine(map.GetHashCode());
                }
            }
        }