public void Test_Maze_Travel() { var graph = new SquireGridGraph(row, col, false); DisjSet ds = new DisjSet(graph.VerticesNum()); Random r = new Random(Environment.TickCount); while (ds.Find(0) != ds.Find(graph.VerticesNum() - 1)) { int v1 = r.Next(0, graph.VerticesNum()); if (graph.GetDegree(v1) >= 3) { continue; } Direction direction = getRandomDirection(); int v2 = graph.GetNeighborVertex(v1, direction); if (v2 != -1) { graph.SetEdge(v1, v2, 1); ds.UnionSets(v1, v2); } } AStar aStar = new AStar(graph); if (aStar.Travel(0, graph.VerticesNum() - 1)) { IList <int> result = aStar.reconstruct_path(graph.VerticesNum() - 1); Assert.IsTrue(result.Count > 0); } else { Assert.IsTrue(false); } }
public void Test_Maze_generate() { /** * 1. Éú³ÉSquareGridͼ * * */ var graph = new SquireGridGraph(row, col, false); DisjSet ds = new DisjSet(graph.VerticesNum()); Random r = new Random(Environment.TickCount); while (ds.Find(0) != ds.Find(graph.VerticesNum() - 1)) { int v1 = r.Next(0, graph.VerticesNum()); if (graph.GetDegree(v1) >= 3) { continue; } Direction direction = getRandomDirection(); int v2 = graph.GetNeighborVertex(v1, direction); if (v2 != -1) { graph.SetEdge(v1, v2, 1); ds.UnionSets(v1, v2); } } Assert.IsTrue(graph.EdgeNum() > 1); Assert.IsTrue(ds.GetConnectedComponentNumber() > 1); }
private void astarTravelToolStripMenuItem_Click(object sender, EventArgs e) { if (!timer1.Enabled) { if (_graph == null) { return; } Toggle(); _aStar = new AStar(_graph); if (_aStar.Travel(0, _graph.VerticesNum() - 1)) { _result = _aStar.reconstruct_path(_graph.VerticesNum() - 1); timer1.Start(); } } else { timer1.Stop(); } }
public void Test_Graph_DisjSet() { var g = new SquireGridGraph(ROW, COL, false); DisjSet ds = new DisjSet(g.VerticesNum()); for (int i = 0; i < g.VerticesNum(); i++) { int row, col, v; g.GetRowAndColFromVertex(i, out row, out col); g.SetNeighbor(row, col, Direction.East, 1); if ((v = g.GetNeighborVertex(i, Direction.East)) != -1) { ds.UnionSets(i, v); } g.SetNeighbor(row, col, Direction.South, 1); if ((v = g.GetNeighborVertex(i, Direction.South)) != -1) { ds.UnionSets(i, v); } g.SetNeighbor(row, col, Direction.West, 1); if ((v = g.GetNeighborVertex(i, Direction.West)) != -1) { ds.UnionSets(i, v); } g.SetNeighbor(row, col, Direction.North, 1); if ((v = g.GetNeighborVertex(i, Direction.North)) != -1) { ds.UnionSets(i, v); } } Assert.AreEqual(1, ds.GetConnectedComponentNumber()); }
public void Test_SGG_Creation() { var g = new SquireGridGraph(ROW, COL, false); for (int i = 0; i < g.VerticesNum(); i++) { int row, col; g.GetRowAndColFromVertex(i, out row, out col); g.SetNeighbor(row, col, Direction.East, 1); g.SetNeighbor(row, col, Direction.South, 1); g.SetNeighbor(row, col, Direction.West, 1); g.SetNeighbor(row, col, Direction.North, 1); } ITravel dfs = new Bfs(g, preVisit); dfs.Travel(16); Assert.AreEqual(6280, g.EdgeNum()); }
private void genMazeToolStripMenuItem_Click(object sender, EventArgs e) { _graph = new SquireGridGraph(Row, Col, false); var ds = new DisjSet(_graph.VerticesNum()); var r = new Random(Environment.TickCount); while (ds.Find(0) != ds.Find(_graph.VerticesNum() - 1)) { int v1 = r.Next(0, _graph.VerticesNum()); if (_graph.GetDegree(v1) >= 3) { continue; } Direction direction = getRandomDirection(); int v2 = _graph.GetNeighborVertex(v1, direction); if (v2 != -1) { _graph.SetEdge(v1, v2, 1); ds.UnionSets(v1, v2); } } var pen = new Pen(Color.Brown, 1); Brush brush = new SolidBrush(Color.Blue); var bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics gg = Graphics.FromImage(bitmap); for (int i = 1; i < _graph.VerticesNum() - 1; i++) { int row, col; _graph.GetRowAndColFromVertex(i, out row, out col); int v2 = _graph.GetNeighborVertex(i, Direction.West); if (v2 == -1 || !_graph.IsConnected(i, v2)) { // Draw Line gg.DrawLine(pen, _margin + col * Radius, _margin + row * Radius, _margin + col * Radius, _margin + row * Radius + Radius); } v2 = _graph.GetNeighborVertex(i, Direction.North); if (v2 == -1 || !_graph.IsConnected(i, v2)) { // Draw Line gg.DrawLine(pen, _margin + col * Radius, _margin + row * Radius, _margin + col * Radius + Radius, _margin + row * Radius); } v2 = _graph.GetNeighborVertex(i, Direction.East); if (v2 == -1 || !_graph.IsConnected(i, v2)) { // Draw Line gg.DrawLine(pen, _margin + col * Radius + Radius, _margin + row * Radius, _margin + col * Radius + Radius, _margin + row * Radius + Radius); } v2 = _graph.GetNeighborVertex(i, Direction.South); if (v2 == -1 || !_graph.IsConnected(i, v2)) { // Draw Line gg.DrawLine(pen, _margin + col * Radius, _margin + row * Radius + Radius, _margin + col * Radius + Radius, _margin + row * Radius + Radius); } } pictureBox1.Image = bitmap; pictureBox1.Refresh(); pen.Dispose(); brush.Dispose(); gg.Dispose(); }