コード例 #1
0
ファイル: Algorithms.cs プロジェクト: mjcs-95/MazeGenerator
        private static int Kill(int current)
        {
            bool deadend = false;

            while (!deadend)
            {
                List <int> noAdj       = g.UnconnectedNeighbors(current / g.cols, current % g.cols);
                bool       unconnected = false;
                while (0 < noAdj.Count && !unconnected)
                {
                    int idx = rand.Next(noAdj.Count);
                    if (!visited[noAdj[idx]])
                    {
                        g.addEdge(current, noAdj[idx], default(T));
                        g.addEdge(noAdj[idx], current, default(T));
                        current          = noAdj[idx];
                        visited[current] = true;
                        unconnected      = true;
                        --counter;
                    }
                    else
                    {
                        noAdj.RemoveAt(idx);
                    }
                }
                if (!unconnected)
                {
                    deadend = true;
                }
            }
            return(-1);
        }
コード例 #2
0
ファイル: Algorithms.cs プロジェクト: mjcs-95/MazeGenerator
 public static MazeGraph <T> Execute(MazeGraph <T> G)
 {
     InitializeVariables(G);
     for (int i = 0; i < g.rows; ++i)
     {
         List <int> group = new List <int>();
         for (int j = 0; j < g.cols; ++j)
         {
             group.Add(j);
             if (!(j < g.cols - 1) || ((i < g.rows - 1) && rand.Next(2) == 0))
             {
                 int connectAt = group[rand.Next(0, group.Count)];
                 g.addEdge(g.GetNode(i, connectAt), g.GetNode(i + 1, connectAt), default(T));
                 g.addEdge(g.GetNode(i + 1, connectAt), g.GetNode(i, connectAt), default(T));
                 group.Clear();
             }
             else
             {
                 g.addEdge(g.GetNode(i, j), g.GetEast(i, j), default(T));
                 g.addEdge(g.GetEast(i, j), g.GetNode(i, j), default(T));
             }
         }
     }
     return(g);
 }
コード例 #3
0
ファイル: Algorithms.cs プロジェクト: mjcs-95/MazeGenerator
        public static MazeGraph <T> Execute(MazeGraph <T> G)
        {
            InitializeVariables(G);

            for (int i = 0; i < g.numVert(); ++i)
            {
                List <MazeGraph <T> .VertexCost> neighbors = new List <MazeGraph <T> .VertexCost>();
                List <MazeGraph <T> .VertexCost> adj       = G[i];
                foreach (MazeGraph <T> .VertexCost v in adj)
                {
                    if (v.vertex > i)
                    {
                        neighbors.Add(v);
                    }
                }
                if (neighbors.Count > 0)
                {
                    int idx = rand.Next(0, neighbors.Count);
                    MazeGraph <T> .VertexCost neighbor = neighbors[idx];
                    g.addEdge(i, neighbor.vertex, neighbor.cost);
                    g.addEdge(neighbor.vertex, i, neighbor.cost);
                }
            }
            return(g);
        }
コード例 #4
0
ファイル: Algorithms.cs プロジェクト: mjcs-95/MazeGenerator
 static public MazeGraph <T> Execute(MazeGraph <T> G)
 {
     initializeVariables(G);
     enqueueAdjEdges(G, 0);
     for (int i = 1; i < N; i++)
     {
         do
         {
             edge = queue.top();
             queue.delete();
         } while (added[edge.dest]);
         g.addEdge(edge.orig, edge.dest, edge.cost);
         g.addEdge(edge.dest, edge.orig, edge.cost);
         added[edge.dest] = true;
         enqueueAdjEdges(G, edge.dest);
     }
     return(g);
 }
コード例 #5
0
ファイル: Algorithms.cs プロジェクト: mjcs-95/MazeGenerator
 public static MazeGraph <T> Execute(MazeGraph <T> G)
 {
     initializeVariables(G);
     for (int count = 1; count < n;)
     {
         Edge <T> edge = queue.top();
         queue.delete();
         int leader1 = P.find(edge.orig);
         int leader2 = P.find(edge.dest);
         if (leader1 != leader2)
         {
             P.join(leader1, leader2);
             g.addEdge(edge.orig, edge.dest, edge.cost);
             g.addEdge(edge.dest, edge.orig, edge.cost);
             ++count;
         }
     }
     return(g);
 }
コード例 #6
0
ファイル: Algorithms.cs プロジェクト: mjcs-95/MazeGenerator
 public static MazeGraph <T> Execute(MazeGraph <T> G)
 {
     InitializeVariables(G);
     int[] conjuntos = Enumerable.Range(0, g.cols).ToArray <int>();
     for (int i = 0; i < g.rows; ++i)
     {
         for (int j = 0; j < g.cols - 1; ++j)
         {
             if ((i == g.rows - 1 || rand.Next(2) == 1) && !g.hasEdge(g.GetNode(i, j), g.GetNode(i, j + 1)))
             {
                 conjuntos[j + 1] = conjuntos[j];
                 g.addEdge(g.GetNode(i, j), g.GetNode(i, j + 1), default(T));
                 g.addEdge(g.GetNode(i, j + 1), g.GetNode(i, j), default(T));
             }
         }
         if (i < g.rows - 1)
         {
             int[]         siguientesconjuntos = Enumerable.Range((i + 1) * g.cols, g.cols).ToArray <int>();
             HashSet <int> todoslosconjuntos   = new HashSet <int>(conjuntos);
             HashSet <int> conjuntosmovidos    = new HashSet <int>();
             while (!todoslosconjuntos.SetEquals(conjuntosmovidos))
             {
                 for (int j = 0; j < g.cols; ++j)
                 {
                     if (rand.Next(2) == 1 && !conjuntosmovidos.Contains(conjuntos[j]))
                     {
                         conjuntosmovidos.Add(conjuntos[j]);
                         siguientesconjuntos[j] = conjuntos[j];
                         g.addEdge(g.GetNode(i, j), g.GetNode(i + 1, j), default(T));
                         g.addEdge(g.GetNode(i + 1, j), g.GetNode(i, j), default(T));
                     }
                 }
             }
             conjuntos = siguientesconjuntos;
         }
     }
     return(g);
 }
コード例 #7
0
ファイル: Algorithms.cs プロジェクト: mjcs-95/MazeGenerator
        public static MazeGraph <T> Execute(MazeGraph <T> G)
        {
            InitializeVariables(G);
            Random rand    = new Random();
            int    idx     = rand.Next(0, unvisited.Count);
            int    current = unvisited[idx];

            unvisited.Remove(idx);
            //int count = 0;
            while (unvisited.Count > 0)   //&& count < 100000) {
            {
                MazeGraph <T> .VertexCost adj = G[current][rand.Next(0, G[current].Count)];
                if (unvisited.IndexOf(adj.vertex) != -1)
                {
                    g.addEdge(current, adj.vertex, adj.cost);
                    g.addEdge(adj.vertex, current, adj.cost);
                    unvisited.Remove(adj.vertex);
                }
                current = adj.vertex;
                //count++;
            }
            return(g);
        }
コード例 #8
0
ファイル: Algorithms.cs プロジェクト: mjcs-95/MazeGenerator
        public static MazeGraph <T> Execute(MazeGraph <T> G)
        {
            InitializeVariables(G);
            //int count = 0;
            int idx   = rand.Next(0, unvisited.Count);
            int first = unvisited[idx];

            unvisited.RemoveAt(idx);
            while (unvisited.Count > 0) //&& count<500000) {
            {
                int        current = unvisited[rand.Next(0, unvisited.Count)];
                List <int> path    = new List <int>();
                path.Add(current);
                while (unvisited.IndexOf(current) != -1)  //&& count < 500000) {
                {
                    current = G.Adjacents(current)[rand.Next(0, G.Adjacents(current).Count)].vertex;
                    int pos = path.IndexOf(current);
                    if (pos == -1)
                    {
                        path.Add(current);
                    }
                    else
                    {
                        path = path.GetRange(0, pos + 1);
                    }
                    //count++;
                }
                for (int i = 0; i < path.Count - 1; ++i)
                {
                    g.addEdge(path[i], path[i + 1], default(T));
                    g.addEdge(path[i + 1], path[i], default(T));
                    unvisited.Remove(path[i]);
                }
            }
            return(g);
        }
コード例 #9
0
ファイル: AdjListGraph.cs プロジェクト: mjcs-95/MazeGenerator
    public static MazeGraph <int> createNoWallsGraph4(int rows, int cols)
    {
        int n = rows * cols;
        /*Build the Full Graph*/
        MazeGraph <int> G = new MazeGraph <int>(n, rows, cols);

        System.Random rand = new System.Random();
        int           aux  = 0;

        for (int i = 0; i < rows; ++i)
        {
            for (int j = 0; j < cols; ++j, ++aux)
            {
                int cost = rand.Next(1, 10);
                //Random values to use with prim and kruskal but it could be 1; Still not undirected
                if (j < cols - 1)
                {
                    G.addEdge(aux, aux + 1, 1);  //Random.Range(1, n)
                    G.addEdge(aux + 1, aux, 1);
                }// North
                if (i > 0)
                {
                    G.addEdge(aux, aux - cols, 1); // west
                    G.addEdge(aux - cols, aux, 1); // west
                }
                if (j > 0)
                {
                    G.addEdge(aux, aux - 1, 1); // South
                    G.addEdge(aux - 1, aux, 1); // South
                }
                if (i < rows - 1)
                {
                    G.addEdge(aux, aux + cols, 1); // East
                    G.addEdge(aux + cols, aux, 1); // East
                }
            }
        }
        return(G);
    }