void SquareMaze::bottomRemove(int x, int y, DisjointSets & maze,int & count,int rindex) { if (maze.find(rindex) != maze.find(rindex+maze_width)) { setWall(x, y, 1, false); maze.setunion(rindex, rindex+maze_width); count++; } }
void SquareMaze::rightRemove(int x, int y, DisjointSets & maze,int & count,int rindex) { if (maze.find(rindex) != (maze.find(rindex+1))) { setWall(x, y, 0, false); maze.setunion(rindex, rindex+1); count++; } }
/** * Finds a minimal spanning tree on a graph. * THIS FUNCTION IS GRADED. * * @param graph - the graph to find the MST of * * @todo Label the edges of a minimal spanning tree as "MST" * in the graph. They will appear blue when graph.savePNG() is called. * * @note Use your disjoint sets class from MP 7.1 to help you with * Kruskal's algorithm. Copy the files into the libdsets folder. * @note You may call std::sort (http://www.cplusplus.com/reference/algorithm/sort/) * instead of creating a priority queue. */ void GraphTools::findMST(Graph & graph) { vector<Edge> eds = graph.getEdges(); sort(eds.begin(), eds.end(),myfunction); DisjointSets vers; vector <Vertex> vertex_list = graph.getVertices(); vers.addelements(vertex_list.size()); for (int i = 0; i < eds.size(); i++) { Vertex u = eds[i].source; Vertex v = eds[i].dest; if (vers.find(u) != vers.find(v)) { vers.setunion(u,v); graph.setEdgeLabel(u,v,"MST"); } } }
/** * Finds a minimal spanning tree on a graph. * THIS FUNCTION IS GRADED. * * @param graph - the graph to find the MST of * * @todo Label the edges of a minimal spanning tree as "MST" * in the graph. They will appear blue when graph.savePNG() is called. * * @note Use your disjoint sets class from MP 7.1 to help you with * Kruskal's algorithm. Copy the files into the libdsets folder. * @note You may call std::sort (http://www.cplusplus.com/reference/algorithm/sort/) * instead of creating a priority queue. */ void GraphTools::findMST(Graph & graph) { vector<Edge> theEdges = graph.getEdges(); std::sort(theEdges.begin(), theEdges.end()); DisjointSets theVertexSet; vector<Vertex> theVertices = graph.getVertices(); theVertexSet.addelements(theVertices.size()); int size = theVertices.size(); int count = 0; vector<Edge>::iterator iter; for(iter = theEdges.begin(); iter != theEdges.end(); iter++) { if(count == size - 1) break; if(theVertexSet.find(iter->source) != theVertexSet.find(iter->dest)) { theVertexSet.setunion(iter->source, iter->dest); graph.setEdgeLabel(iter->source, iter->dest, "MST"); count++; } } }
void SquareMaze::makeMaze(int width, int height) { right.clear(); bottom.clear(); maze_width = width; maze_height = height; blocks = maze_width * maze_height; right.resize(blocks,true); bottom.resize(blocks,true); DisjointSets maze; maze.addelements(blocks); srand(time(NULL)); //int x, y; bool rightmost=true, bottommost=true; int count = 0; //int rindex; while (count< (blocks - 1)) { int rindex = rand() % blocks; int x = rindex%maze_width; int y = rindex/maze_width; if(x==(maze_width-1)) rightmost=true; else rightmost=false; if(y==(maze_height-1)) bottommost=true; else bottommost=false; if (( rightmost == true ) && ( bottommost == true )) { rightmost=rightmost; } else if (!rightmost && !bottommost) { int action=rand()%3; if (action == 0) rightRemove(x,y, maze,count,rindex); else if (action == 1) bottomRemove(x,y, maze,count,rindex); else { if ((maze.find(rindex) != maze.find(rindex+1)) && (maze.find(rindex) != maze.find(rindex+maze_width))&& (maze.find(rindex+1) != maze.find(rindex+1))) { setWall(x, y, 0, false); setWall(x, y, 1, false); maze.setunion(rindex, rindex+1); maze.setunion(rindex, rindex+maze_width); count+=2; } } } else if(bottommost) { int action=rand()%2; if (action== 1)rightRemove(x,y, maze,count,rindex); } else { int action =rand()%2; if (action == 1)bottomRemove(x,y, maze,count,rindex); } } }
void SquareMaze::makeMaze(int width,int height) { width1=width; height1=height; int size=width*height; right.clear(); down.clear();//clearing right and down walls just in case right=vector<bool>(size, true);//set all walls so they are there we will remove later down=vector<bool>(size, true); vector<int> blocks(size);//vector for indicies // map<int, int> maps; for(int i=0;i<size; i++) { blocks.push_back(i);//pushing back the indidces for the maze // for(int j=0; j<height1; j++) // { // // maps.insert(make_pair(i,j)); // } } DisjointSets set; set.addelements(size);//used to stop cycles from being created srand(time(0)); // std::random_shuffle(maps.begin(), maps.end()); std::random_shuffle(blocks.begin(), blocks.end());//randomly shuffles indicies so it doesnt create the same maze // map<int, int>::iterator it; vector<int> :: iterator it; for(it=blocks.begin(); it!=blocks.end(); it++) { int x=*it%width;//x and y coords for the index int y=*it/width; int index=*it; //actual index if(x!=width1-1 ) //cant go any farther to the right in this case so dont want to do it { int index1=index+1; if(set.find(index)!=set.find(index1)) //checking if the one to the right of the index is in the same set { setWall(x,y,0,false);//if they are not in the same set we can remove the wall set.setunion(index, index1); //we connect them so they are in the same set //works because if we try to remove something in the same set it will create a cycle } } if(y!=height1-1) { int index2=index+width1; if(set.find(index)!= set.find(index2)) { setWall(x,y,1,false); //do the same for if we want to go down set.setunion(index, index2); } } } }