Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        public void Test_Squire_Graph_neighbor()
        {
            var g = new SquireGridGraph(ROW, COL, false);

            int v;

            v = g.GetNeighborVertex(17, Direction.West);
            Assert.AreEqual(16, v);

            v = g.GetNeighborVertex(17, Direction.North);
            Assert.AreEqual(-1, v);

            v = g.GetNeighborVertex(17, Direction.East);
            Assert.AreEqual(18, v);

            v = g.GetNeighborVertex(17, Direction.South);
            Assert.AreEqual(97, v);
        }
Beispiel #4
0
        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());
        }
Beispiel #5
0
        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();
        }