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 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(""); }