예제 #1
0
        //********************************************************************
        public void heapifydown()
        {
            int li = data.Count - 1; //last index (after removal)

            int pi = 0;              // parent index. start at front of pq

            while (true)
            {
                int ci = pi * 2 + 1; // left child index of parent
                if (ci > li)
                {
                    break;           // no children so done
                }
                int rc = ci + 1;     // right child
                // if there is a right child , and it is smaller than left child, use the rc instead
                if (rc <= li && data[rc].cost.CompareTo(data[ci].cost) < 0)
                {
                    ci = rc;
                }
                // If parent is smaller than (or equal to) smallest child so done
                if (data[pi].cost.CompareTo(data[ci].cost) <= 0)
                {
                    break;
                }
                // Else swap parent and child
                puzzel tmp = data[pi]; data[pi] = data[ci]; data[ci] = tmp;
                pi = ci;
            }
        }
예제 #2
0
        //********************************************************************
        public puzzel Dequeue()
        {
            // assumes pq is not empty; up to calling code
            int    li        = data.Count - 1; // last index (before removal)
            puzzel frontItem = data[0];        // fetch the front

            data[0] = data[li];                // last item be the root
            data.RemoveAt(li);
            heapifydown();
            return(frontItem);
        }
        //**************************************************
        public void Get_Path(puzzel goal)
        {
            // Get Path
            puzzel p = goal.parent;

            while (p.parent != null)
            {
                path.Add(p);
                p = p.parent;
            }
            // Add Start Board
            path.Add(p);
            // call Display path
            display_path();
        }
        //**********************************************
        public Boolean check_if_child_in_closed(puzzel c)
        {
            bool check = false;

            if (map_closed.ContainsKey(c.key))
            {
                puzzel _key = map_closed[c.key];
                if (_key.cost < c.cost)
                {
                    open_list.Enqueue(_key); map_open.Add(_key.key, _key);
                }
                check = true;
            }
            return(check);
        }
 //*******************************************************
 public void A_Star_Algorithm(puzzel start)
 {
     map_open.Add(start.key, start);
     open_list.Enqueue(start);
     while (!open_list.is_empty())
     {
         puzzel current = new puzzel(open_list.Dequeue(), 0);
         if (!check_if_child_in_closed(current))
         {
             map_closed.Add(current.key, current);
             closed_list.Add(current);
             // generate child from current
             generate_child(current);
         }
     }
 }
예제 #6
0
 //*******************************************************************
 //child constructor
 public puzzel(puzzel p)
 {
     this.N        = p.N;
     this.puzzel2d = new int[N, N];
     for (int i = 0; i < this.N; i++)
     {
         for (int j = 0; j < this.N; j++)
         {
             this.puzzel2d[i, j] = p.puzzel2d[i, j];
         }
     }
     this.blank_index_i = p.blank_index_i;
     this.blank_index_j = p.blank_index_j;
     this.num_of_level  = p.num_of_level + 1;
     this.parent        = p.parent;
 }
예제 #7
0
        //********************************************************************
        public void heapifyup()
        {
            int ci = data.Count - 1; // child index; start at end

            while (ci > 0)
            {
                int pi = (ci - 1) / 2; // parent index

                // If child item is larger than (or equal) parent so we're done
                if (data[ci].cost.CompareTo(data[pi].cost) >= 0)
                {
                    break;
                }
                // Else swap parent & child
                puzzel tmp = data[ci]; data[ci] = data[pi]; data[pi] = tmp;
                ci = pi;
            }
        }
예제 #8
0
 //*******************************************************************
 //Equal constructor
 public puzzel(puzzel p, int _default)
 {
     this.N        = p.N;
     this.puzzel2d = new int[N, N];
     for (int i = 0; i < this.N; i++)
     {
         for (int j = 0; j < this.N; j++)
         {
             this.puzzel2d[i, j] = p.puzzel2d[i, j];
         }
     }
     this.blank_index_i = p.blank_index_i;
     this.blank_index_j = p.blank_index_j;
     this.num_of_level  = p.num_of_level;
     this.hamming_val   = p.hamming_val;
     this.manhattan_val = p.manhattan_val;
     this.cost          = p.cost;
     this.parent        = p;
     this.key           = p.key;
 }
예제 #9
0
 //***************************************************************
 //initial constructor
 public puzzel(int size, int[,] puzzel_array, int blank_tile_i, int blank_tile_j)
 {
     this.N        = size;
     this.puzzel2d = new int[N, N];
     for (int i = 0; i < this.N; i++)
     {
         for (int j = 0; j < this.N; j++)
         {
             this.puzzel2d[i, j] = puzzel_array[i, j];
             this.key           += puzzel_array[i, j];
         }
     }
     this.direction     = "Start";
     this.blank_index_i = blank_tile_i;
     this.blank_index_j = blank_tile_j;
     this.num_of_level  = 0;
     this.hamming();
     this.manhattan();
     this.parent = null;
 }
        //*******************************************************
        public void generate_child(puzzel p)
        {
            // check L||R||U||D
            if (p.check_left())
            {
                puzzel c = new puzzel(p);
                c.move_left();
                c.manhattan();
                c.calc_min_cost_using_manhattan();
                if (c.is_goal_M())
                {
                    c.direction = "Goal";
                    // Add Goal Board
                    path.Add(c);
                    Get_Path(c);
                }
                c.direction = "Left";

                //check if child in open list or not
                Boolean boo2;
                boo2 = check_if_child_in_open(c);
                if (boo2 == false)
                {
                    open_list.Enqueue(c); map_open.Add(c.key, c);
                }
            }
            if (p.check_right())
            {
                puzzel c = new puzzel(p);
                c.move_right();
                c.manhattan();
                c.calc_min_cost_using_manhattan();
                if (c.is_goal_M())
                {
                    c.direction = "Goal";
                    // Add Goal Board
                    path.Add(c);
                    Get_Path(c);
                }
                c.direction = "Right";
                //check if child in open list or not
                Boolean boo2;
                boo2 = check_if_child_in_open(c);
                if (boo2 == false)
                {
                    open_list.Enqueue(c); map_open.Add(c.key, c);
                }
            }
            if (p.check_up())
            {
                puzzel c = new puzzel(p);
                c.move_up();
                c.manhattan();
                c.calc_min_cost_using_manhattan();
                if (c.is_goal_M())
                {
                    c.direction = "Goal";
                    // Add Goal Board
                    path.Add(c);
                    Get_Path(c);
                }
                c.direction = "Up";
                //check if child in open list or not
                Boolean boo2;
                boo2 = check_if_child_in_open(c);
                if (boo2 == false)
                {
                    open_list.Enqueue(c); map_open.Add(c.key, c);
                }
            }
            if (p.check_down())
            {
                puzzel c = new puzzel(p);
                c.move_down();
                c.manhattan();
                c.calc_min_cost_using_manhattan();
                if (c.is_goal_M())
                {
                    c.direction = "Goal";
                    // Add Goal Board
                    path.Add(c);
                    Get_Path(c);
                }
                c.direction = "Down";
                //check if child in open list or not
                Boolean boo2;
                boo2 = check_if_child_in_open(c);
                if (boo2 == false)
                {
                    open_list.Enqueue(c); map_open.Add(c.key, c);
                }
            }
        }
        //**********************************************
        public Boolean check_if_child_in_open(puzzel c)
        {
            bool check = map_open.ContainsKey(c.key);

            return(check);
        }
예제 #12
0
        static void Main(string[] args)
        {
            // Read Board From File
            FileStream   fs = new FileStream("8 Puzzle (1).txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            while (sr.Peek() != -1)
            {
                String   s = sr.ReadLine();
                String[] fields;
                fields = s.Split(' ');
                int N;
                N = int.Parse(fields[0]);
                int   val;
                int[] puzzel1d = new int[N * N];
                int[,] puzzel2d = new int[N, N];
                int counter = 0;
                for (int i = 0; i < N; i++)
                {
                    s      = sr.ReadLine();
                    fields = s.Split(' ');
                    for (int j = 0; j < N; j++)
                    {
                        val = int.Parse(fields[j]);
                        if (val == 0)
                        {
                            blank_i = i; blank_j = j;
                        }
                        puzzel2d[i, j]    = val;
                        puzzel1d[counter] = val;
                        counter++;
                    }
                }
                //*****************************************************

                char ch;
                Console.WriteLine(" - Press [1] To Using A_Star With Hamming .");
                Console.WriteLine(" - Press [2] To Using A_Star With Manhattan .");
                Console.Write(" - Enter Your Choice : ");
                ch = char.Parse(Console.ReadLine());

                switch (ch)
                {
                //-----------------------------------------------------------------------------------
                case '1':
                {
                    // Check If Board Is Solvable Or Not
                    if (check_solvable(puzzel1d, N, blank_i))
                    {
                        Console.WriteLine();
                        Console.WriteLine(" - The Given Board Is < Solvable >");
                        Console.WriteLine("    __________________________     ");
                        Console.WriteLine();
                        puzzel start           = new puzzel(N, puzzel2d, blank_i, blank_j);
                        A_Star_Using_Hamming A = new A_Star_Using_Hamming();
                        A.A_Star_Algorithm(start);
                        A.display_path();
                    }
                    else
                    {
                        Console.WriteLine("No Feasible Solution For The Given Board ");
                    }
                }
                break;

                //-----------------------------------------------------------------------------------
                case '2':
                {
                    // Check If Board Is Solvable Or Not
                    if (check_solvable(puzzel1d, N, blank_i))
                    {
                        Console.WriteLine();
                        Console.WriteLine(" - The Given Board Is < Solvable >");
                        Console.WriteLine("    __________________________     ");
                        Console.WriteLine();
                        puzzel start             = new puzzel(N, puzzel2d, blank_i, blank_j);
                        A_Star_Using_Manhattan A = new A_Star_Using_Manhattan();
                        A.A_Star_Algorithm(start);
                    }
                    else
                    {
                        Console.WriteLine(" - No Feasible Solution For The Given Board ");
                    }
                }
                break;

                //-----------------------------------------------------------------------------------
                default:
                    break;
                    //-----------------------------------------------------------------------------------
                }
            }
        }
예제 #13
0
        //********************************************************************

        public void Enqueue(puzzel item)
        {
            data.Add(item);
            heapifyup();
        }