예제 #1
0
        //Caculate hn
        public static int nonSuited(Chart trans, Chart goal)
        {
            int result = 0;
            for (int i = 0; i < goal.positions.Length; i++)
            {
                if (goal.positions[i].getNum() != trans.positions[i].getNum())
                {
                    result++;
                }

            }

            return result;
        }
예제 #2
0
 public static int Manhattan(Chart trans,Chart goal)
 {
     int result = 0;
     for(int i=0;i<9;i++)
     {
         if(trans.positions[i].getNum()!=0)
         {
             for(int j=0;j<9;j++)
             {
                 if(trans.positions[i].getNum()==goal.positions[j].getNum())
                 {
                     result += Math.Abs(i - j);
                     break;
                 }
             }
         }
     }
     return result;
 }
        public int BreadthFirst()
        {
            closeList.Clear();
            openList.Clear();

            openList.Add(initial);

            Chart transNode=new Chart();

            while (true)
            {
                if (openList.Count == 0)
                {
                    MessageBox.Show("open list is empty");
                    return fail;
                }
                transNode = openList.ElementAt(0);
                if (hit(transNode))
                {
                    MessageBox.Show("成功!遍历的节点数为:" + closeList.Count.ToString());

                    return success;

                }

                closeList.Add(transNode);
                openList.Remove(transNode);

                transNode.expandNode();

                foreach (Chart item in transNode.children)
                {
                    item.parent = transNode;

                }
                appendOpenList(transNode.children);

            }
        }
예제 #4
0
 public bool equals(Chart chart2)
 {
     bool flag=true;
     for(int i=0;i<9;i++)
     {
         if (this.positions[i].getNum() != chart2.positions[i].getNum())
             flag = false;
     }
     return flag;
 }
예제 #5
0
        public Chart clone()
        {
            Chart newchart = new Chart();

            for (int i = 0; i < this.positions.Length; i++)
            {
                newchart.positions[i] = (Position)this.positions[i].clone();
            }

            return newchart;
        }
예제 #6
0
 internal static int CalculateFn(Chart chart1, Chart chart2,int method)
 {
     switch(method)
     {
         case 0:
             return nonSuited(chart1, chart2);
         case 1:
             return Manhattan(chart1, chart2);
         default:
             return nonSuited(chart1, chart2);
     }
 }
예제 #7
0
 public bool parentContains(Chart chart)
 {
     if (this.parent != null)
     {
         if (this.parent.equals(chart))
             return true;
         else
             return this.parent.parentContains(chart);
     }
     return false;
 }
        private void ModifyReference(Chart transNode, List<Chart> children)
        {
            foreach(Chart item in children)
            {
               bool flag1=false;
               bool flag2=false;
               foreach(Chart item1 in openList)
               {
                   if(item.equals(item1))
                       flag1=true;
               }
               foreach(Chart item2 in closeList)
               {
                   if(item.equals(item2))
                       flag2=true;
               }
               if(!flag1&&!flag2)
               {

                    openList.Add(item);
                    item.parent = transNode;
                    item.gn = item.parent.gn + 1;
                    item.hn = Chart.CalculateFn((Chart)item, (Chart)goal,ComboBox2.SelectedIndex);
                    item.fn = item.gn + item.hn;

                }
                else if(flag1&&!flag2)
                {
                    if(transNode.gn+1+item.hn<item.fn)
                    {
                        item.parent = transNode;
                        item.fn=transNode.gn+1+item.hn;
                        item.gn=transNode.gn+1;
                    }
                }
                else if(!flag1&&flag2)
                {
                    if (transNode.gn + 1 + item.hn < item.fn)
                    {

                        item.fn = transNode.gn + 1 + item.hn;
                        item.gn = transNode.gn + 1;
                        item.parent = transNode;
                        openList.Add(item);
                        closeList.Remove(item);
                    }
                }
            }
        }
 private bool hit(Chart transNode)
 {
     if (Chart.nonSuited((Chart)transNode, (Chart)goal) == 0)
         return true;
     return false;
 }
        private void ButtonStart_Click(object sender, RoutedEventArgs e)
        {
            int []numForInitial=new int[9];
            int []numForGoal=new int[9];
            int i=0;
            foreach(TextBox _textBox in uniformgrid1.Children)
            {
                numForInitial[i] = int.Parse(_textBox.Text);
                i++;
            }
            i = 0;
            foreach(TextBox _textBox in uniformgrid2.Children)
            {
                numForGoal[i] = int.Parse(_textBox.Text);
                i++;
            }
            if (!isSolve(numForInitial, numForGoal))
            {
                MessageBox.Show("该情况下无解");
            }
            initial = new Chart(numForInitial);
            goal = new Chart(numForGoal);
            openList = new List<Chart>();
            closeList = new List<Chart>();

            //method=0,A*启发性搜索
            //method=1,广度优先
            int method = ComboBox1.SelectedIndex;

            switch(method)
            {
                case 0:
                    //A star here;
                    AStar();
                    break;
                case 1:
                    //BreadthFirst Here;
                    BreadthFirst();
                    break;

            }
        }