示例#1
0
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Point             start       = new Point(500, 600);
            int               init_length = 150;
            CQueue <m_struct> m_struct1   = new CQueue <m_struct>();
            m_struct          m1          = new m_struct(new Point(Convert.ToInt16(500 - init_length * 0.5 * Math.Sqrt(2)),
                                                                   Convert.ToInt16(600 - init_length - init_length * 0.5 * Math.Sqrt(2))), Convert.ToInt16(init_length * 0.5), 1);
            m_struct m2 = new m_struct(new Point(Convert.ToInt16(500 + init_length * 0.5 * Math.Sqrt(2)),
                                                 Convert.ToInt16(600 - init_length - init_length * 0.5 * Math.Sqrt(2))), Convert.ToInt16(init_length * 0.5), 2);

            DrawMainTree(start, init_length);
            //DrawTree(m1.m_point, m1.length, m1.heading);
            //kDrawTree(m2.m_point, m2.length, m2.heading);
            m_struct1.In(m1); m_struct1.In(m2);
            init_length = Convert.ToInt16(init_length * 0.5);
            while (init_length > 2)
            {
                init_length = Convert.ToInt16(init_length * 0.5);
                CQueue <m_struct> m_queue_bak = new CQueue <m_struct>();
                while (!m_struct1.IsEmpty())
                {
                    m_struct m12 = m_struct1.Out();
                    m_struct m_1, m_2, m_3, m_4;
                    if (m12.heading == 1 || m12.heading == 2)
                    {
                        m_1.heading = 1; m_2.heading = 2;
                        m_1.length  = init_length; m_2.length = init_length;
                        m_1.m_point = new Point(Convert.ToInt16(m12.m_point.X - m12.length * 0.5 * Math.Sqrt(2)), Convert.ToInt16(m12.m_point.Y - m12.length - m12.length * 0.5 * Math.Sqrt(2)));
                        m_2.m_point = new Point(Convert.ToInt16(m12.m_point.X + m12.length * 0.5 * Math.Sqrt(2)), Convert.ToInt16(m12.m_point.Y - m12.length - m12.length * 0.5 * Math.Sqrt(2)));
                    }
                    else
                    {
                        m_1.heading = 3; m_2.heading = 4;
                        m_1.length  = init_length; m_2.length = init_length;
                        m_1.m_point = new Point(Convert.ToInt16(m12.m_point.X - m12.length * 0.5 * Math.Sqrt(2)), Convert.ToInt16(m12.m_point.Y + m12.length + m12.length * 0.5 * Math.Sqrt(2)));
                        m_2.m_point = new Point(Convert.ToInt16(m12.m_point.X + m12.length * 0.5 * Math.Sqrt(2)), Convert.ToInt16(m12.m_point.Y + m12.length + m12.length * 0.5 * Math.Sqrt(2)));
                    }
                    if (m12.heading == 1 || m12.heading == 3)
                    {
                        m_3.heading = 1; m_4.heading = 3;
                        m_3.length  = init_length; m_4.length = init_length;
                        m_3.m_point = new Point(Convert.ToInt16(m12.m_point.X - m12.length - m12.length * 0.5 * Math.Sqrt(2)), Convert.ToInt16(m12.m_point.Y - m12.length * 0.5 * Math.Sqrt(2)));
                        m_4.m_point = new Point(Convert.ToInt16(m12.m_point.X - m12.length - m12.length * 0.5 * Math.Sqrt(2)), Convert.ToInt16(m12.m_point.Y + m12.length * 0.5 * Math.Sqrt(2)));
                    }
                    else
                    {
                        m_3.heading = 2; m_4.heading = 4;
                        m_3.length  = init_length; m_4.length = init_length;
                        m_3.m_point = new Point(Convert.ToInt16(m12.m_point.X + m12.length + m12.length * 0.5 * Math.Sqrt(2)), Convert.ToInt16(m12.m_point.Y - m12.length * 0.5 * Math.Sqrt(2)));
                        m_4.m_point = new Point(Convert.ToInt16(m12.m_point.X + m12.length + m12.length * 0.5 * Math.Sqrt(2)), Convert.ToInt16(m12.m_point.Y + m12.length * 0.5 * Math.Sqrt(2)));
                    }
                    DrawTree(m12.m_point, m12.length, m12.heading);
                    m_queue_bak.In(m_1);
                    m_queue_bak.In(m_2);
                    m_queue_bak.In(m_3);
                    m_queue_bak.In(m_4);
                }
                m_struct1 = m_queue_bak;
            }
        }
        public void Fun_Permute_Queue(int n, int m)
        {
            CQueue <int> m_cqueue = new CQueue <int>();

            int[] zijishulie = new int[n];
            for (int i = 0; i < n; i++)
            {
                zijishulie[i] = i + 1;
            }
            int i1 = 0;

            while (i1 < n)
            {
                while (m_cqueue.Rear() - m_cqueue.Front() < m && i1 < n)
                {
                    m_cqueue.In(zijishulie[i1]);
                    i1++;
                }
                for (int j = 0; j < m; j++)
                {
                    int[] k1 = new int[m];
                    for (int i = 0; i < m; i++)
                    {
                        k1[i] = i1 - m + i + 1;
                    }
                    int count = 0;
                    int[,] k2 = new int[1000, 10];
                    for (int i = 0; i < m; i++)
                    {
                        k2[count, i] = i1 - m + i + 1;
                    }
                    for (int j1 = j + 1; j1 < m; j1++)
                    {
                        for (int j2 = 0; j2 <= count; j2++)
                        {
                            int   count1 = 0;
                            int[] k3     = new int[10];
                            for (int i12 = 0; i12 < m; i12++)
                            {
                                k3[i12] = k2[j2, i12];
                            }
                            swap(k3, j, j1);
                            for (int i13 = 0; i13 < m; i13++)
                            {
                                k2[count1++, i13] = k3[i13];
                            }
                            for (int i = 0; i < m; i++)
                            {
                                m_strout += Convert.ToString(k3[i]);
                            }
                            m_strout += "\r\n";
                            count     = count1;
                        }
                    }
                }
                m_cqueue.Out();
            }
        }
示例#3
0
      public string ShortPath()  //最短路径
      {
          move[0].x = 0; move[0].y = +1;
          move[1].x = +1; move[1].y = +1;
          move[2].x = +1; move[2].y = 0;
          move[3].x = +1; move[3].y = -1;
          move[4].x = 0; move[4].y = -1;
          move[5].x = -1; move[5].y = -1;
          move[6].x = -1; move[6].y = 0;
          move[7].x = -1; move[7].y = +1;    //初始化位置坐标增量
          sq        = new CQueue <sqtype>(); //创建队列
          if (rows == 0 || cols == 0)
          {
              return("");
          }
          sqtype temp = new sqtype();

          temp.x = temp.y = 1; temp.pre = 0; sq.In(temp); Setelems(1, 1, -1); //起点进队
          while (!sq.IsEmpty())                                               //队不空时循环
          {
              temp = sq.Getfront(); int x = temp.x; int y = temp.y;           //取队头
              for (int k = 0; k < 8; k += 2)                                  //查找八个方向
              {
                  int i = x + move[k].x; int j = y + move[k].y;
                  if (Getelems(i, j) == 0)  //路通
                  {
                      temp.x   = i;
                      temp.y   = j;
                      temp.pre = sq.Front() + 1; //前一个结点
                      sq.In(temp);               //进队
                      Setelems(i, j, -1);        //走过的设置为-1
                  }
                  if (i == rows && j == cols)
                  {
                      int j1  = sq.Rear();
                      int len = 0;
                      for (int i1 = sq.Rear(); i1 >= 1; i1--)
                      {
                          temp = sq.Getdata(i1);
                          if (i1 == j1)
                          {
                              Setelems(temp.x, temp.y, -1);
                              j1 = temp.pre;
                              len++;
                          }
                          else
                          {
                              Setelems(temp.x, temp.y, 0);
                          }
                      }
                      return("");
                  }
              }
              sq.Out();  //出队
          }
          return("");
      }