int main() { std::queue <int> q; std::vector <int> nodes; std::vector <int> visitedNodes; for(int i= 0; i < 7; i++) { nodes.push_back(i); } q.push(nodes[0] ); int frontNode= 0; while( !q.empty() ) { frontNode= q.front(); q.pop(); if( isVisited( visitedNodes, frontNode) ) continue; else { getAllNeighborsAndPush2Q(q, frontNode); visitedNodes.push_back(frontNode); } } printVector( visitedNodes); return -1; }
vector<string> findWords2(vector<vector<char>>& board, vector<string>& words) { vector<string>result; if ( 0 == board.size() || 0 == board[0].size()) { return result; } unordered_set<string>hash_set; int max_wordLen = 0; for ( int i = 0; i < words.size(); i++) { hash_set.insert(words[i]); max_wordLen = max(max_wordLen, words[i].size()); } int n = board.size(); int m = board[0].size(); for ( int i = 0; i < n; i++) { for ( int j = 0; j < m; j++) { vector<vector<int>>isVisited(n, vector<int>(m, 0)); string subset; //wordSearchII_helper1(board, hash_set, subset, result, isVisited, i, j, max_wordLen); wordSearchII_helper2(board, hash_set, subset, result, isVisited, i, j, max_wordLen, 0, words); } } return result; }
void setBoardtoMove(Board inboard, Board outboard, int loc_apex, int block_type, DIR dir, int steps, BOOL settomemo, Node *prenode) { Node *node = NULL; Board board = NULL; DEBUG("IN"); if (settomemo) { board = (Board)malloc(BOARD_CELLS); memcpy(board, inboard, BOARD_CELLS); } else { board = outboard; } switch(block_type) { case SB: board[loc_apex] = NB; board[loc_apex + dir * steps] = block_type; break; case VG: board[loc_apex] = NB; board[loc_apex + DOWN] = NB; board[loc_apex + dir * steps] = block_type; board[loc_apex + dir * steps + DOWN] = block_type; break; case HG: board[loc_apex] = NB; board[loc_apex + RIGHT] = NB; board[loc_apex + dir * steps] = block_type; board[loc_apex + dir * steps + RIGHT] = block_type; break; case CC: board[loc_apex] = NB; board[loc_apex + RIGHT] = NB; board[loc_apex + DOWN] = NB; board[loc_apex + DOWN + RIGHT] = NB; board[loc_apex + dir * steps] = block_type; board[loc_apex + dir * steps + RIGHT] = block_type; board[loc_apex + dir * steps + DOWN] = block_type; board[loc_apex + dir * steps + DOWN + RIGHT] = block_type; break; default: ERROR("invalid block_type"); break; } if (settomemo && !isVisited(board)) { memcpy(outboard, board, BOARD_CELLS); node = ht->put(board); node->data->prev = prenode; enQue(que, node); #if FALSE YIELD("move to board"); printBoard(board); #endif } DEBUG("OUT"); }
ProgramStrings Print::flatten() const { if (isVisited()){ return ProgramStrings(); } setVisited(true); ProgramStrings prog; foreach(Sink *sink, sinks()){ Data* sinkData = sink->sourceData(); //ps += sinkData->flatten(); prog = prog + sinkData->flatten(); }
virtual void visitStructure(VLXStructure* obj) { if (isVisited(obj)) return; declareID(obj); for(size_t i=0; i<obj->value().size(); ++i) { if (obj->value()[i].value().type() == VLXValue::Structure) obj->value()[i].value().getStructure()->acceptVisitor(this); else if (obj->value()[i].value().type() == VLXValue::List) obj->value()[i].value().getList()->acceptVisitor(this); } }
/* * 用遞迴尋訪Glyphs (DFS) * *targetGlyph : 要找尋的終點Glyph * *nextGlyph : 當前Glyph * *visitedGlyphs : 記錄走訪過的Glyph (防止迴圈路徑) */ bool ActivityDiagram::visitGlyphs(Glyph *goalGlyph,Glyph *currentGlyph, vector<Glyph*> *visitedGlyphs) { if (goalGlyph == currentGlyph) // 找到目標Glyph return true; visitedGlyphs->push_back(currentGlyph); // 記錄拜訪過的Glyph vector<Glyph*> targetGlyphs = currentGlyph->getTargets(); // 拿到當前Glyphs所有target for (unsigned int i = 0; i < targetGlyphs.size(); i++) if (!isVisited(targetGlyphs[i], visitedGlyphs)) // 是沒拜訪過的Glyph if (visitGlyphs(goalGlyph, targetGlyphs[i], visitedGlyphs)) // 找到目標Glyph return true; //沒找到目標Glyph時將繼續尋訪 visitedGlyphs->pop_back(); //退回上一個Glyph return false; }
bool exist(vector<vector<char>>& board, string word) { //DFS //TC:TC:O(m*n*4^k); SC:O(m*n) if(board.empty()){ return false; } int row = board.size(), col = board[0].size(); vector<vector<bool>> isVisited(row, vector<bool>(col, false)); for(int i = 0; i < row; i++){ for(int j = 0; j < col; j++){ if(dfs(board, word, isVisited, i, j, 0)){ return true; } } } return false; }
virtual void visitList(VLXList* list) { // this should happen only if the user manually creates loops if (isVisited(list)) { Log::warning("VLXVisitorLinkMapper: cycle detected on VLXList.\n"); return; } for(size_t i=0; i<list->value().size(); ++i) { if (list->value()[i].type() == VLXValue::Structure) list->value()[i].getStructure()->acceptVisitor(this); else if (list->value()[i].type() == VLXValue::List) list->value()[i].getList()->acceptVisitor(this); } }
/* is visited cell */ bool Kingdom::isVisited(const Maps::Tiles & tile) const { return isVisited(tile.GetIndex(), tile.GetObject()); }
/* u and v are the number of vertices in sets U, and V, respectively, * filling up bipgraph[0..u-1][0..v-1]. * result: * matching[u0]==v0 iff u0 and v0 are in the matching, * otherwise matching[u0] = -1 */ void match(int u, int v) { int i,j, head,tail, bad, last, increased; for( i = 0; i < u; i++ ) { matching[i] = -1; flagUmatched[i] = 0; } for( i = 0; i < v; i++ ) flagVmatched[i] = 0; do { /* find alternating paths by repeated bfs. */ for( i = 0; i < MAXU+MAXV; i++ ) predecessor[i] = -1; for( i = 0; i < MAXU; i++ ) flagUused[i] = flagUvisited[i] = 0; for( i = 0; i < MAXV; i++ ) flagVused[i] = flagVvisited[i] = 0; head = tail = 0; /* put all the unmatched u's on the queue. They start the * alternating path. */ for( i = 0; i < u; i++ ) { if( ! isMatched(U(i))) { queue[tail++] = U(i); predecessor[i] = -1; /* redundant statement */ setVisited(U(i)); } } /* flag that at least one path was found by the bfs. * when the bfs does not find an alternating path we are done. */ increased = 0; while( head != tail ) { i = queue[head++]; /* this node appeared on some previously found alternating path. */ if( isUsed(i) ) continue; if( isV(i) && !isMatched(i) ) { /* we got to the end of an alternating path. see if * it is disjoint with other paths found so far. only * then can we mess it up a bit. */ bad = 0; for( j = i; j != -1; j = predecessor[j]) { if( isUsed(j)) { bad = 1; break; } } if( ! bad ) { /* this path is pristine. switch "polarity" of edges * in the matching on this path. */ /* flag and instrumention - whether (not) to quit, * and how many paths we found this bfs. */ increased++; for( j = i; j != -1; last = j, j = predecessor[j] ) { if( isV(j) && !isMatched(j)) { /* the only unmatched v - actually this means we * are on the first iteration of this loop. */ setMatched(j); } else if( isU(j) ) { if( isMatched(j) ) { /* the node we saw in the previous iteration of * this loop must be a V. We will match with it * instead of the one we used to match with, which * must be the next node visited in this loop. */ assert(isV(last)); matching[j] = last - MAXU; } else { /* we are the very first u, one of the ones the * bfs queue was "seeded" with. We should have ...*/ assert(predecessor[j] == -1); setMatched(j); assert(isV(last)); matching[j] = last - MAXU; } } setUsed(j); /* this node cannot be used for other * paths we might run across in the future * on this bfs. */ } /* for */ } /* if ! bad */ } /* isV and !isMatched */ else if( isV(i) ) { /* this must be a matched V - find the matching U and put it on * the queue if it is not visited or used. */ bad = 1; for( j = 0; j < u; j++ ) { if( isMatched(U(j)) && matching[j] == i - MAXU ) { /* this is the one. */ if( ! isVisited(U(j)) && !isUsed(U(j))) { setVisited(U(j)); queue[tail++] = U(j); predecessor[U(j)] = i; } bad = 0; break; } } assert(!bad); } /* isV */ else if( isU(i) ) { /* we are at U. whether it is unmatched (a "seed"), * or matched, we do the same thing - put on the queue * all V's which it is connected to in the graph but * which it is _not_ paired to in the current matching. */ for( j = 0; j < v; j++ ) { if( bipgraph[i][j] && !isVisited(V(j)) && !isUsed(V(j)) && matching[i] != j ) { /* we can put this one on the queue. */ queue[tail++] = V(j); predecessor[V(j)] = i; setVisited(V(j)); } } } else { assert(0); /* should be no other cases. */ } /* this is the end of the bfs. */ } } while( increased ); return; }
IGL_INLINE void igl::boundary_loop( const Eigen::MatrixXd& V, const Eigen::MatrixXi& F, Eigen::VectorXi& b) { std::vector<int> bnd; bnd.clear(); std::vector<bool> isVisited(V.rows(),false); // Actually mesh only needs to be manifold near boundary, so this is // over zealous (see gptoolbox's outline_loop for a more general // (and probably faster) implementation) assert(is_edge_manifold(V,F) && "Mesh must be manifold"); Eigen::MatrixXi TT,TTi; std::vector<std::vector<int> > VF, VFi; igl::triangle_triangle_adjacency(V,F,TT,TTi); igl::vertex_triangle_adjacency(V,F,VF,VFi); // Extract one boundary edge bool done = false; for (int i = 0; i < TT.rows() && !done; i++) { for (int j = 0; j < TT.cols(); j++) { if (TT(i,j) < 0) { int idx1, idx2; idx1 = F(i,j); idx2 = F(i,(j+1) % F.cols()); bnd.push_back(idx1); bnd.push_back(idx2); isVisited[idx1] = true; isVisited[idx2] = true; done = true; break; } } } // Traverse boundary while(1) { bool changed = false; int lastV; lastV = bnd[bnd.size()-1]; for (int i = 0; i < (int)VF[lastV].size(); i++) { int curr_neighbor = VF[lastV][i]; if (TT.row(curr_neighbor).minCoeff() < 0.) // Face contains boundary edge { int idx_lastV_in_face; if (F(curr_neighbor,0) == lastV) idx_lastV_in_face = 0; if (F(curr_neighbor,1) == lastV) idx_lastV_in_face = 1; if (F(curr_neighbor,2) == lastV) idx_lastV_in_face = 2; int idx_prev = (idx_lastV_in_face + F.cols()-1) % F.cols(); int idx_next = (idx_lastV_in_face + 1) % F.cols(); bool isPrevVisited = isVisited[F(curr_neighbor,idx_prev)]; bool isNextVisited = isVisited[F(curr_neighbor,idx_next)]; bool gotBndEdge = false; int next_bnd_vertex; if (!isNextVisited && TT(curr_neighbor,idx_lastV_in_face) < 0) { next_bnd_vertex = idx_next; gotBndEdge = true; } else if (!isPrevVisited && TT(curr_neighbor,(idx_lastV_in_face+2) % F.cols()) < 0) { next_bnd_vertex = idx_prev; gotBndEdge = true; } if (gotBndEdge) { changed = true; bnd.push_back(F(curr_neighbor,next_bnd_vertex)); isVisited[F(curr_neighbor,next_bnd_vertex)] = true; break; } } } if (!changed) break; } b.resize(bnd.size()); for(unsigned i=0;i<bnd.size();++i) b(i) = bnd[i]; }
//search (6) or (26) connected neighborhood for similiar voxel as starting point void Floodfill::search_neighborhood(int x, int y, int z, vector<Point3D>* stack, int rule, int connectivity, int visitmode) { //resolution of steps int steps = 1; //setup our searh indices int indexNorth = y + steps; int indexSouth = y - steps; int indexEast = x + steps; int indexWest = x - steps; int indexUp = z + steps; int indexDown = z - steps; //helper Point3D temp; // look around for connected voxels marked as data (ie: equal to -1) // 6 CONNECTED //----------------- //EAST if(indexEast < width ) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(indexEast, y, z, visitmode)==false) { if(apply_rule(x,y,z, indexEast,y,z, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = y; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(indexEast, y, z, visitmode); } } } //WEST if(indexWest >= 0 ) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(indexWest, y, z, visitmode)==false) { if( apply_rule(x,y,z, indexWest,y,z, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = y; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(indexWest, y, z, visitmode); } } } //NORTH if(indexNorth < height ) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(x, indexNorth, z, visitmode)==false) { if(apply_rule(x,y,z, x,indexNorth,z, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexNorth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(x, indexNorth, z, visitmode); } } } //SOUTH if(indexSouth >= 0) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(x, indexSouth, z, visitmode)==false) { if(apply_rule(x,y,z, x,indexSouth,z, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexSouth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(x, indexSouth, z, visitmode); } } } //UP if(indexUp < depth ) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(x, y, indexUp, visitmode)==false) { if(apply_rule(x,y,z, x,y,indexUp, rule)) { //then voxel is in the same region temp.x = x; temp.y = y; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(x, y, indexUp, visitmode); } } } //DOWN if(indexDown >= 0) //if we aint out of bounds { //if the voxel has not been visited if(isVisited(x, y, indexDown, visitmode)==false) { if(apply_rule(x,y,z, x,y,indexDown, rule)) { //then voxel is in the same region temp.x = x; temp.y = y; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info markVisited(x, y, indexDown, visitmode); } } } /* if(connectivity==CONNECT26) //CONNECT26 { // 26 CONNECTED (20 additional cases) //---------------------------------- //NORTH WEST if(indexNorth < height && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexNorth][z]==false) if(apply_rule(x,y,z, indexWest,indexNorth,z, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexNorth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexNorth][z] = true; } } //SOUTH WEST if(indexSouth >= 0 && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexSouth][z]==false) if(apply_rule(x,y,z, indexWest,indexSouth,z, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexSouth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexSouth][z] = true; } } //NORTH EAST if(indexNorth < height && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexNorth][z]==false) if(apply_rule(x,y,z, indexEast,indexNorth,z, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexNorth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexNorth][z] = true; } } //SOUTH EAST if(indexSouth >= 0 && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexSouth][z]==false) if(apply_rule(x,y,z, indexEast,indexSouth,z, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexSouth; temp.z = z; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexSouth][z] = true; } } //------------------------------------------------------------------------- //UP NORTH if(indexUp < depth && indexNorth < height) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[x][indexNorth][indexUp]==false) if(apply_rule(x,y,z, x,indexNorth,indexUp, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexNorth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[x][indexNorth][indexUp] = true; } } //UP SOUTH if(indexUp < depth && indexSouth >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[x][indexSouth][indexUp]==false) if(apply_rule(x,y,z, x,indexSouth,indexUp, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexSouth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[x][indexSouth][indexUp] = true; } } //UP EAST if(indexUp < depth && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][y][indexUp]==false) if(apply_rule(x,y,z, indexEast,y,indexUp, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = y; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][y][indexUp] = true; } } //UP WEST if(indexUp < depth && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][y][indexUp]==false) if(apply_rule(x,y,z, indexWest,y,indexUp, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = y; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][y][indexUp] = true; } } //UP SOUTH WEST if(indexUp < depth && indexSouth >= 0 && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexSouth][indexUp]==false) if(apply_rule(x,y,z, indexWest,indexSouth,indexUp, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexSouth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexSouth][indexUp] = true; } } //UP SOUTH EAST if(indexUp < depth && indexSouth >= 0 && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexSouth][indexUp]==false) if(apply_rule(x,y,z, indexEast,indexSouth,indexUp, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexSouth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexSouth][indexUp] = true; } } //UP NORTH EAST if(indexUp < depth && indexNorth < height && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexNorth][indexUp]==false) if(apply_rule(x,y,z, indexEast,indexNorth,indexUp, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexNorth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexNorth][indexUp] = true; } } //UP NORTH WEST if(indexUp < depth && indexNorth < height && indexWest >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexNorth][indexUp]==false) if(apply_rule(x,y,z, indexWest,indexNorth,indexUp, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexNorth; temp.z = indexUp; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexNorth][indexUp] = true; } } //------------------------------------------------------------------------- //DOWN NORTH if(indexDown >= 0 && indexNorth < height) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[x][indexNorth][indexDown]==false) if(apply_rule(x,y,z, x,indexNorth,indexDown, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexNorth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[x][indexNorth][indexDown] = true; } } //DOWN SOUTH if(indexDown >= 0 && indexSouth >= 0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[x][indexSouth][indexDown]==false) if(apply_rule(x,y,z, x,indexSouth,indexDown, rule)) { //then voxel is in the same region temp.x = x; temp.y = indexSouth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[x][indexSouth][indexDown] = true; } } //DOWN WEST if(indexDown >= 0 && indexWest >=0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][y][indexDown]==false) if(apply_rule(x,y,z, indexWest,y,indexDown, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = y; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][y][indexDown] = true; } } //DOWN EAST if(indexDown >= 0 && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][y][indexDown]==false) if(apply_rule(x,y,z, indexEast,y,indexDown, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = y; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][y][indexDown] = true; } } //DOWN SOUTH WEST if(indexDown >= 0 && indexSouth >= 0 && indexWest >=0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexSouth][indexDown]==false) if(apply_rule(x,y,z, indexWest,indexSouth,indexDown, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexSouth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexSouth][indexDown] = true; } } //DOWN SOUTH EAST if(indexDown >= 0 && indexSouth >= 0 && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexSouth][indexDown]==false) if(apply_rule(x,y,z, indexEast,indexSouth,indexDown, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexSouth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexSouth][indexDown] = true; } } //DOWN NORTH WEST if(indexDown >= 0 && indexNorth < height && indexWest >=0) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexWest][indexNorth][indexDown]==false) if(apply_rule(x,y,z, indexWest,indexNorth,indexDown, rule)) { //then voxel is in the same region temp.x = indexWest; temp.y = indexNorth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexWest][indexNorth][indexDown] = true; } } //DOWN NORTH EAST if(indexDown >= 0 && indexNorth < height && indexEast < width) //if we aint out of bounds { //if the voxel has not been visited if(visited_voxels[indexEast][indexNorth][indexDown]==false) if(apply_rule(x,y,z, indexEast,indexNorth,indexDown, rule)) { //then voxel is in the same region temp.x = indexEast; temp.y = indexNorth; temp.z = indexDown; //we can add it to the stack stack->push_back(temp); //and mark the voxel with region info visited_voxels[indexEast][indexNorth][indexDown] = true; } } }*/ }
void Floodfill::eval_connectivity(void) { if(volobj==NULL) return; edges.resize(1); edges[0].resize(region_colours_tokeep.size()); for(int j=0; j<edges[0].size(); j++) edges[0][j].resize(region_colours_tokeep.size()); for(int i=0; i<edges[0].size(); i++) for(int j=0; j<edges[0][i].size(); j++) edges[0][i][j]=false; //holds voxel indices of the current region of interest vector< vector<Point3D> > stack; vector<Point3D> s; Point3D temp; int tempi; int x, y, z; int border = 0; init(volobj->texwidth, volobj->texheight, volobj->texdepth, VISITEDARRAY); Vector rgb, region_rgb; Vector black = Vector(0,0,0); Vector yellow = Vector(255,255,0); int current_regionindex; int regionindex; int recursiondepth=0; int maxrecursiondepth = 25; int currentlvl=0; int go = 1; //for each x for (x =border; x < width-border; x++) { progress(x, width-border); //for each y for (y =border; y < height-border; y++) { for (z = 0; z < depth; z++) { //save current position temp.x = x; temp.y = y; temp.z = z; tempi = get1Dindex(temp); region_rgb.x = volobj->texture3d[3*tempi+0]; region_rgb.y = volobj->texture3d[3*tempi+1]; region_rgb.z = volobj->texture3d[3*tempi+2]; //if voxel is data and not visited //----------------------------------- //we can mark our voxels space with -1 or 0 //if -1 then it is a voxel we want to apply our flood fill to //how we mark which voxels are data is done elsewhere. if(region_rgb!=yellow && region_rgb!=black && isVisited(x,y,z,VISITEDARRAY)==false) // { for(int i=0; i<region_colours_tokeep.size(); i++) { if(region_rgb==region_colours_tokeep[i]) { current_regionindex = i; } } s.push_back(temp); stack.push_back(s); //printf("regionsize: %d\n", region_sizes[current_regionindex]); //printf("setting maxrecursiondepth to: %d\n", maxrecursiondepth); recursiondepth=0; currentlvl=0; go = 1; while(go) { s.clear(); //while stack not empty while(stack[currentlvl].size()) { //save item and pop off stack //printf("currentlvl: %d, currentlvlstacksize: %d\n", currentlvl, stack[currentlvl].size()); temp = stack[currentlvl][stack[currentlvl].size()-1]; tempi = get1Dindex(temp); stack[currentlvl].pop_back(); //mark the voxel markVisited(temp.x, temp.y, temp.z, VISITEDARRAY); /* rgb.x = volobj->texture3d[3*tempi+0]; rgb.y = volobj->texture3d[3*tempi+1]; rgb.z = volobj->texture3d[3*tempi+2]; */ volobj->texture3d[3*tempi+0] = region_rgb.x; volobj->texture3d[3*tempi+1] = region_rgb.y; volobj->texture3d[3*tempi+2] = region_rgb.z; //search neighborhood for the next pixel search_neighborhood((int)temp.x, (int)temp.y, (int)temp.z, &s, FILL_RULE, CONNECT26, VISITEDARRAY); //if it isnt marked as an edge /*if(rgb.x!=255.0 && rgb.y!=255.0 && rgb.z !=0.0) { volobj->texture3d[3*tempi+0] = region_rgb.x; volobj->texture3d[3*tempi+1] = region_rgb.y; volobj->texture3d[3*tempi+2] = region_rgb.z; }*/ /*if(region_rgb!=rgb) { for(int i=0; i<region_colours_tokeep.size(); i++) { if(rgb==region_colours_tokeep[i]) { printf("FOUND EDGE\n"); regionindex = i; edges[0][current_regionindex][regionindex] = true; edges[0][regionindex][current_regionindex] = true; visited_voxels[(int)temp.x][(int)temp.y][(int)temp.z] = false; } } }*/ } //printf("recursiondepth: %d, stacksize: %d\n", recursiondepth, s.size()); stack.push_back(s); stack[currentlvl].clear(); currentlvl++; recursiondepth++; if(recursiondepth>=maxrecursiondepth || s.empty()) { /*for(int j=0; j<stack.size(); j++) { for(int i=0; i<stack[j].size(); i++) { temp = stack[j][i]; visited_voxels[(int)temp.x][(int)temp.y][(int)temp.z] = false; } stack[j].clear(); }*/ stack.clear(); /*for(int i=0; i<s.size(); i++) { visited_voxels[(int)s[i].x][(int)s[i].y][(int)s[i].z] = false; }*/ s.clear(); go=0; //return; } /*else { stack[currentlvl].clear(); }*/ } } } } } // printf("\n Regions Found: %i \n", regions); printf("\n Regions Found: %i \n", regions); }
void Floodfill::floodfill(int rule) { if(volobj==NULL) return; //holds voxel indices of the current region of interest vector<Point3D> stack; vector<int> curr_region; Point3D temp; int tempi; int x, y, z; regions = 0; region_sizes.clear(); region_colours.clear(); region_indices.clear(); init(volobj->texwidth, volobj->texheight, volobj->texdepth, VISITEDARRAY); int border = 0; printf("Labelling Background\n"); //floodfillseeded(Point3D(0,0,0), rule); printf("done...\n"); Vector rgb, hsv; int recursiondepth=0; //for each x for (x =border; x < width-border; x++) { progress(x, width-border); //for each y for (y =border; y < height-border; y++) { for (z = 0; z < depth; z++) { temp.x = x; temp.y = y; temp.z = z; tempi = get1Dindex(temp); rgb.x = volobj->texture3d[3*tempi+0]; rgb.y = volobj->texture3d[3*tempi+1]; rgb.z = volobj->texture3d[3*tempi+2]; //if voxel is data and not visited //----------------------------------- //we can mark our voxels space with -1 or 0 //if -1 then it is a voxel we want to apply our flood fill to //how we mark which voxels are data is done elsewhere. if(isVisited(x,y,z,VISITEDARRAY)==false && rgb!=0.0) { //we found a region regions++; /*hsv.x = rand()%360; hsv.y = (float)(rand()%255)/255.0; hsv.z = 1.0; HSVtoRGB(hsv, &rgb);*/ int cont=1; while(cont) { rgb.x = (float)(rand()%255)/255.0; rgb.y = 0.0; rgb.z = (float)(rand()%255)/255.0; rgb *= 255; cont = 0; for(int i=0; i<region_colours.size(); i++) { if(rgb==region_colours[i]) cont = 1; } } //save current position temp.x = x; temp.y = y; temp.z = z; recursiondepth=0; stack.push_back(temp); curr_region.push_back(get1Dindex(temp)); //while stack not empty while(stack.size()) { //save item and pop off stack temp = stack[stack.size()-1]; stack.pop_back(); //mark the voxel markVisited(temp.x, temp.y, temp.z, VISITEDARRAY); curr_region.push_back(get1Dindex(temp)); //search neighborhood for the next pixel search_neighborhood((int)temp.x, (int)temp.y, (int)temp.z, &stack, rule, CONNECT26, VISITEDARRAY); //curr_region.push_back(get1Dindex(temp)); tempi = get1Dindex(temp); volobj->texture3d[3*tempi+0] = rgb.x; volobj->texture3d[3*tempi+1] = rgb.y; volobj->texture3d[3*tempi+2] = rgb.z; recursiondepth++; } region_colours.push_back(Vector((int)rgb.x, (int)rgb.y, (int)rgb.z)); //region_sizes.push_back(recursiondepth); region_indices.push_back(curr_region); curr_region.clear(); } } } } // printf("\n Regions Found: %i \n", regions); printf("\n Regions Found: %i \n", regions); }
/* set visited cell */ void Kingdom::SetVisited(const s32 index, const MP2::object_t object) { if(!isVisited(index, object) && object != MP2::OBJ_ZERO) visit_object.push_front(IndexObject(index, object)); }
int main(int argc, char* argv[]) { if ( argc != 4 ) { cout << "Usage: CMOrderedTreeMiner support input_file output_file" << endl; exit (1); } int support; istringstream iss(argv[1]); iss >> support; if(!iss) { cerr << "invalid argument, not an integer value" << endl; exit (1); } vector<int> frequency(1000,0); //assuming the max frequent tree size is 1000 vector<int> checked(1000,0); vector<int> closed(1000,0); vector<int> maximal(1000,0); currentPatternTree.initialSize(); time_t start_time; time_t stop_time; /****************************************************************** step1: read in the database, and find the MIN_VERTEX and MAX_VERTEX ******************************************************************/ string inputFile = argv[2]; string outputFile = argv[3]; ofstream outFile(outputFile.c_str()); if(!outFile) { cerr << "cannot open OUTPUT file!" << endl; exit(1); } ifstream inFile(inputFile.c_str()); if(!inFile) { cerr << "cannot open INPUT file!" << endl; exit(1); } vector<TextTree> database; int myTid = 0; while ( !inFile.eof() ) { TextTree tt; inFile >> tt; if ( !inFile.eof() ) { tt.tid = myTid++; for ( short i = 0; i < tt.vNumber; i++ ) { if ( tt.vLabel[i] < MIN_VERTEX ) MIN_VERTEX = tt.vLabel[i]; if ( tt.vLabel[i] > MAX_VERTEX ) MAX_VERTEX = tt.vLabel[i]; } database.push_back(tt); } } inFile.close(); /****************************************************************** step2.1: scan the database once, find frequent node labels ******************************************************************/ vector<bool> isFrequent(MAX_VERTEX - MIN_VERTEX + 1, false); map<short,int> count; map<short,int>::iterator pos; start_time = time(0); for ( int i = 0; i < database.size(); i++ ) { vector<bool> isVisited(MAX_VERTEX - MIN_VERTEX + 1, false); for ( short j = 0; j < database[i].vNumber; j++ ) { short temp = database[i].vLabel[j] - MIN_VERTEX; if ( !isVisited[temp] ) { isVisited[temp] = true; pos = count.find(temp); if ( pos != count.end() ) count[temp]++; else count.insert(make_pair(temp, 1)); } } } for ( int i = 0; i < isFrequent.size(); i++ ) { if ( count[i] >= support ) isFrequent[i] = true; } /****************************************************************** step2.2: scan the database another time, to get occurrenceList for all frequent nodes ******************************************************************/ map<short,OccLongList> occLongList; map<short,OccLongList>::iterator pos2; vector<short> dummy; for ( int i = 0; i < database.size(); i++ ) { for ( short j = 0; j < database[i].vNumber; j++ ) { if ( isFrequent[database[i].vLabel[j] - MIN_VERTEX] == true ) { occLongList[database[i].vLabel[j] - MIN_VERTEX].insert(i,dummy,j); } } } /****************************************************************** step2.3: explore each frequent item ******************************************************************/ for ( pos2 = occLongList.begin(); pos2 != occLongList.end(); ++pos2 ) { if ( pos2->second.mySupport >= support ) { currentPatternTree.addRightmost(pos2->first + MIN_VERTEX,0); pos2->second.explore(isFrequent,database,support,checked,closed,maximal); currentPatternTree.deleteRightmost(); } } stop_time = time(0); /****************************************************************** step2.4: output the results ******************************************************************/ for ( short j = 0; j < 1000; j++ ) { if ( checked[j] > 0 ) { outFile << "number of checked " << j << " trees: " << checked[j] << endl; } } outFile << endl << "************************" << endl; for ( short j = 0; j < 1000; j++ ) { if ( closed[j] > 0 ) { outFile << "number of closed " << j << " trees: " << closed[j] << endl; } } outFile << endl << "************************" << endl; for ( short j = 0; j < 1000; j++ ) { if ( maximal[j] > 0 ) { outFile << "number of maximal " << j << " trees: " << maximal[j] << endl; } } outFile << endl; outFile << "Total Running Time: " << difftime(stop_time, start_time) << endl; outFile.close(); return 0; }
bool StageManager::isVisited() { return isVisited( m_CurrentFloorNum ); }