示例#1
0
文件: map.cs 项目: kutagh/RushHour
        public static Tuple<char, Direction, int> GetMoveMade(Map origin, Map after)
        {
            var originCars = origin.Parse();
            var afterCars = after.Parse();

            foreach (var key in originCars.Keys) {
                var origPos = originCars[key].Item1;
                var afterPos = afterCars[key].Item1;
                if (origPos.Equals(afterPos))
                    continue;
                var dir = (origPos.X > afterPos.X || origPos.Y > afterPos.Y) ? originCars[key].Item3 : originCars[key].Item3.Invert();
                var dist = (origPos.X != afterPos.X) ? origPos.X - afterPos.X : origPos.Y - afterPos.Y;
                if (dist < 0) dist *= -1;

                return new Tuple<char, Direction, int>(key, dir, dist);
            }
            return null;
        }
示例#2
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());
                }
            }
        }
示例#3
0
 private static void NewMethod(ConcurrentQueue<Tuple<Map, char>> queue, Tree tree, Map currentMap, KeyValuePair<char, Tuple<Point, int, Direction>> kvp, Map move)
 {
     var moveNode = tree.Find(move);
     if (moveNode == null) {
         tree.AddNeighbor(currentMap, move);
         queue.Enqueue(new Tuple<Map, char>(move, kvp.Key));
         //Console.WriteLine("Queued:\n" + move);
     }
     else ;// tree.AddNeighbor(currentMap, moveNode);
 }