예제 #1
0
        public short[] backtracking(Hanoi Son)
        {
            int from = 0, to = 0;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    int index = i * size + j;
                    if (this.arr[index] != Son.arr[index])
                    {
                        if (Son.arr[index] == 0)
                        {
                            from = i;
                            j    = size;
                        }
                        else
                        {
                            to = i;
                            j  = size;
                        }
                    }
                }
            }

            return(new short[2] {
                (short)(from + 1), (short)(to + 1)
            });
        }
예제 #2
0
        public Hanoi get_answer(Hanoi h, bool mode)
        {
            Glist.setSize(h.getSize());

            push(h, null);

            Hanoi answer = h.answer();

            while (q.Count != 0)
            {
                if (stop)
                {
                    break;
                }
                Hanoi current = q.Dequeue();

                if (current.equal(answer))
                {
                    return(current);
                }

                Hanoi clone = current.Clone() as Hanoi;

                if (mode)
                {
                    for (int i = 1; i <= 3; i++)
                    {
                        if (clone.Req_move(i))
                        {
                            push(clone, current);
                            clone = current.Clone() as Hanoi;
                        }
                    }
                }
                else
                {
                    for (int i = 1; i <= 3; i++)
                    {
                        for (int j = 1; j <= 3; j++)
                        {
                            if (i == j)
                            {
                                continue;
                            }

                            if (clone.Req_move(i, j))
                            {
                                InsertQueue(clone, current);
                                clone = current.Clone() as Hanoi;
                            }
                        }
                    }
                }
            }

            stop = false;
            return(null);
        }
예제 #3
0
        public object Clone()
        {
            Hanoi newhanoi = new Hanoi(size);

            newhanoi.arr         = arr.Clone() as short[];
            newhanoi.movecount   = this.movecount;
            newhanoi.Parentindex = Parentindex;
            newhanoi.thisindex   = thisindex;
            return(newhanoi);
        }
예제 #4
0
 public bool equal(Hanoi b)
 {
     for (int i = 0; i < size * 3; i++)
     {
         if (b.arr[i] != arr[i])
         {
             return(false);
         }
     }
     return(true);
 }
예제 #5
0
        //답지 만들어서 반환
        public Hanoi answer()
        {
            Hanoi new_hanoi = new Hanoi(size);

            for (int i = 0; i < size; i++)
            {
                new_hanoi.arr[i]            = 0;
                new_hanoi.arr[size + i]     = 0;
                new_hanoi.arr[size * 2 + i] = (short)(size - i);
            }

            return(new_hanoi);
        }
예제 #6
0
        void push(Hanoi h, Hanoi parent)
        {
            if (parent == null)
            {
                h.pushindex(Glist.Count, -1);
            }
            else
            {
                h.pushindex(Glist.Count, parent.getindex());
            }
            Glist.Add(h);

            q.Enqueue(h);
        }
예제 #7
0
        void InsertQueue(Hanoi h, Hanoi parent)
        {
            int score = h.getScore();

            if (score < maxscore)
            {
                return;
            }
            else if (score > maxscore)
            {
                maxscore = score;
            }
            if (!Glist.checkExist(h))
            {
                push(h, parent);
            }
        }
예제 #8
0
        public void setHanoi(int size)
        {
            this.size = size;
            hanoi     = new Hanoi(size);
            glist     = Helper.getGlist();
            tv.set(size, glist);

            tv.Startcal();
            Task t = Task.Factory.StartNew(() => {
                sw.Start();
                try
                {
                    ans = Helper.get_answer(hanoi, mode);
                }
                catch (Exception e)
                {
                }

                sw.Stop();
                calend();
            });
        }