void fillgrid(System.Drawing.Point pt, Brush b) { listRect[md.p2i(pt)].Fill = b; }
void find_way_depth()//深度优先搜索 { System.Drawing.Point end_point = md.endPoint; System.Drawing.Point now_point = md.startPoint; if (md.MazeString[md.p2i(now_point)] == '0')//入口是否可通 { MessageBox.Show(noWay); ++th_num; th1.Abort(); return; } Stack <System.Drawing.Point> stp = new Stack <System.Drawing.Point>(); //储存当前路径 Stack <Direction> std = new Stack <Direction>(); //储存各点方向 Stack <System.Drawing.Point> stp_not = new Stack <System.Drawing.Point>(); //储存无法通过的点 bool is_turn = false; //标志当前点是否转向 stp.Push(now_point); std.Push(Direction.up); strDepthPath = "M " + (stp.Peek().X *MCommon.MazeGridLenght + MCommon.MazeGridLenght / 2) + "," + (stp.Peek().Y *MCommon.MazeGridLenght + MCommon.MazeGridLenght / 2) + " L"; Thread.Sleep(speed); while (now_point != end_point) { if (stp.Count == 0) { MessageBox.Show(noWay); ++th_num; th1.Abort(); return; } if (is_turn == true && std.Peek() == Direction.up)//4个方向探索完毕,无法通过,退回上一步 { std.Pop(); now_point = stp.Pop(); stp_not.Push(now_point); back_path(); MainWindow.mw.FillGridInvoke(now_point, Brushes.DarkRed); //g.FillRectangle(Brushes.DarkRed, now_point.X, now_point.Y, 10, 10); if (stp.Count == 0) { MessageBox.Show(noWay); ++th_num; th1.Abort(); return; } //MainWindow.mw.FillGridInvoke(stp.Peek(), Brushes.Blue); //g.FillRectangle(backpen.Brush, stp.Peek().X, stp.Peek().Y, 10, 10); } System.Drawing.Point pt = Next_way(stp.Peek(), std.Peek()); if (pt.X >= md.Width || pt.X < 0 || pt.Y >= md.Height || pt.Y < 0 ||//超出边界 md.MazeString[md.p2i(pt)] == '0' ||//障碍物 stp.Contains(pt) || (stp_not.Count > 0 && stp_not.Contains(pt))) //路径重复 { //转向 Direction dir = std.Pop(); dir = (Direction)((Convert.ToInt32(dir) + 1) % 4); std.Push(dir); is_turn = true; } else { //"进入" now_point = pt; draw_path(stp.Peek(), std.Peek()); stp.Push(now_point); std.Push(Direction.up); is_turn = false; } Thread.Sleep(speed); } std.Pop(); draw_path(end_point);//画出最后一个点 MessageBox.Show("搜索完成,成功找到了星星"); ++th_num; th1.Abort(); return; }