Example #1
0
        public static void Start(int[,] src, int[,] des)
        {
            Src = src;
            Des = des;

            NodeP node = new NodeP(Src, 0);

            node.C = GetCost(node);

            PriorityQueue <NodeP> queue = new PriorityQueue <NodeP>();

            queue.Enqueue(node);

            while (queue.Count() > 0)
            {
                var n = queue.Dequeue();
                Console.WriteLine(n.C + ":" + n.Level);
                if (n.C == 0)
                {
                    Print(n);
                    break;
                }

                for (int i = 0; i < 4; i++)
                {
                    int tx = Xs[i] + n.X;
                    int ty = Ys[i] + n.Y;

                    if (n.Parent != null && n.Parent.X == tx && n.Parent.Y == ty)
                    {
                        continue;
                    }

                    if (IsSafe(tx, ty))
                    {
                        var nN = n.Clone();
                        nN.Level++;
                        nN.N[nN.X, nN.Y] = nN.N[tx, ty];
                        nN.N[tx, ty]     = 0;
                        nN.X             = tx;
                        nN.Y             = ty;
                        nN.C             = GetCost(nN);
                        queue.Enqueue(nN);
                    }
                }
            }
        }
Example #2
0
        static int GetCost(NodeP node)
        {
            int c = 0;

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (Des[i, j] != 0 && Des[i, j] != node.N[i, j])
                    {
                        c++;
                    }
                }
            }

            return(c);
        }
Example #3
0
        static void Print(NodeP node)
        {
            while (node != null)
            {
                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < N; j++)
                    {
                        Console.Write(node.N[i, j] + " ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();

                node = node.Parent;
            }
        }