Exemplo n.º 1
0
        public FifteenSolver(Fifteen initial, string path)
        {
            try
            {
                String input = File.ReadAllText(@path);

                int i = 0, j = 0;
                int[,] fileOut = new int[4, 4];
                foreach (var row in input.Split('\n'))
                {
                    j = 0;
                    foreach (var col in row.Split(','))
                    {
                        if (!String.IsNullOrEmpty(col))
                        {
                            fileOut[i, j] = int.Parse(col);
                        }
                        j++;
                    }
                    i++;
                }
                initial.fifteen = fileOut;
                this.initial    = initial;
                ItemComp <Item>      comparer = new ItemComp <Item>();
                PriorityQueue <Item> priorityQueue = new PriorityQueue <Item>(10, comparer);
                priorityQueue.Enqueue(new Item(null, initial));
                while (true)
                {
                    Item fifteen = priorityQueue.Dequeue();
                    if (fifteen.fifteen.isGoal())
                    {
                        itemToList(new Item(fifteen, fifteen.fifteen));
                        return;
                    }

                    foreach (var item in fifteen.fifteen.Neighbors())
                    {
                        Fifteen tempFif = item;
                        if (item != null && !containsInPath(fifteen, tempFif))
                        {
                            priorityQueue.Enqueue(new Item(fifteen, tempFif));
                        }
                    }
                }
            }
            catch (OutOfMemoryException ex)
            {
            }
        }
Exemplo n.º 2
0
        private bool containsInPath(Item item, Fifteen fifteen)
        {
            Item item2 = item;

            while (true)
            {
                if (item2.fifteen.Equals(fifteen))
                {
                    return(true);
                }
                item2 = item2.prevFifteen;
                if (item2 == null)
                {
                    return(false);
                }
            }
        }
Exemplo n.º 3
0
 public Item(Item prevFifteen, Fifteen fifteen)
 {
     this.prevFifteen = prevFifteen;
     this.fifteen     = fifteen;
 }