本文整理汇总了C++中BFS函数的典型用法代码示例。如果您正苦于以下问题:C++ BFS函数的具体用法?C++ BFS怎么用?C++ BFS使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了BFS函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: BFS
void BFS(vector<vector<TreeNode *>> &cash, vector<TreeNode *> level) {
int len = level.size();
if (len == 0) return;
vector<TreeNode *> childs;
for (int i = 0; i < len; ++i) {
if (level[i]->left) childs.push_back(level[i]->left);
if (level[i]->right) childs.push_back(level[i]->right);
}
if (childs.size() > 0) cash.push_back(childs);
BFS(cash, childs);
}
示例2: BFSTraversal
void BFSTraversal(graph *g)
{
int visited[g->v];
for(int i=0;i<g->v;i++)
visited[i]=0;
for(int i=0;i<g->v;i++)
{
if(!visited[i])
BFS(g,i,visited);
}
}
开发者ID:arjunvijayvargiya,项目名称:DataStructureAndAlgorithmNK,代码行数:11,代码来源:topologicalsortermyversion.cpp
示例3: BFS_All
void BFS_All(AdjList *a)
{
int i;
for(i=0;i<a->vexnum;i++)
{
if(!a->vertex[i].sign)
{
BFS(a,i);
}
}
}
示例4: LOG4CXX_DEBUG
//gap filling
void GapFiller::fill() {
LOG4CXX_DEBUG(logger, "Begin Gap Filling ... ");
size_t num_failed_gaps = 0, num_uniq_gaps = 0, num_multi_gaps = 0, num_overlaps = 0;
for (size_t i = 0; i < _scaffolds.size(); ++i) {
if (_scaffolds[i].contigs.size() <= 1) { // no gaps
continue;
}
Component::ContigIdList contigs = _scaffolds[i].contigs;
for (size_t j = 1; j < contigs.size(); ++j) {
int left_index = contigs[j - 1];
int right_index = contigs[j];
const std::string& lsequence = _uniq_graph._indexer[left_index].seq;
const std::string& rsequence = _uniq_graph._indexer[right_index].seq;
std::string suffix = lsequence.substr(lsequence.length() - (_K - 1), _K - 1);
std::string prefix = rsequence.substr(0, _K - 1);
int overlap = alignment(suffix, prefix);
int gap = _scaffolds[i].gaps[j - 1];
if (overlap >= _OVERLAP) {
_gapinfo_tbl[std::make_pair(i, j)] = GapInfo(-1, -overlap);
++num_overlaps;
} else if (gap > _INSERT_SIZE + 3*_DELTA) {
_gapinfo_tbl[std::make_pair(i, j)] = GapInfo(-1, gap);
++num_failed_gaps;
} else {
try {
GapInfo gapinfo(-1, gap);
BFS(left_index, right_index, gap, &gapinfo);
_gapinfo_tbl[std::make_pair(i, j)] = gapinfo;
if (gapinfo.pathlist.empty()) {
++num_failed_gaps;
} else if (gapinfo.pathlist.size() == 1) {
++num_uniq_gaps;
} else {
++num_multi_gaps;
}
} catch(std::bad_alloc) {
LOG4CXX_WARN(logger, "BFS is too memory-intensive, ignoring...");
_gapinfo_tbl[std::make_pair(i, j)] = GapInfo(-1, -gap);
++num_failed_gaps;
}
}
}
}
LOG4CXX_DEBUG(logger, boost::format("The number of gaps = %d") % _gapinfo_tbl.size());
LOG4CXX_DEBUG(logger, boost::format("The number of failed gaps = %d") % num_failed_gaps);
LOG4CXX_DEBUG(logger, boost::format("The number of unique gaps = %d") % num_uniq_gaps);
LOG4CXX_DEBUG(logger, boost::format("The number of multiple gaps = %d") % num_multi_gaps);
LOG4CXX_DEBUG(logger, boost::format("The number of overlap = %d") % num_overlaps);
}
示例5: numIslands
int numIslands(vector<vector<char>>& grid) {
const int m = grid.size(), n = grid[0].size();
int ret = 0;
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j) {
if(1 == grid[i][j]) {
++ret;
BFS(grid, i, j);
}
}
return ret;
}
示例6: main
task main()
{
Queue Q;
CreateEmpty(&Q);
int color = searchSpot();
if(color==green) {
stepAhead(220);
turn(right,20);
cekSimpang(&Q);
}
BFS(first,&Q);
}
示例7: isap
int isap(int st, int ed, int N) {
BFS(st, ed);
memcpy(cur, head, sizeof(head));
int top = 0;
int u = st;
int ans = 0;
while(dep[st] < N) {
if(u == ed) {
int Min = INF;
int inser;
for(int i = 0; i < top; i++)
if(Min > edge[S[i]].cap - edge[S[i]].flow) {
Min = edge[S[i]].cap - edge[S[i]].flow;
inser = i;
}
for(int i = 0; i < top; i++) {
edge[S[i]].flow += Min;
edge[S[i] ^ 1].flow -= Min;
}
ans += Min;
top = inser;
u = edge[S[top] ^ 1].to;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -1; i = edge[i].next) {
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]) {
flag = true;
cur[u] = i;
break;
}
}
if(flag) {
S[top++] = cur[u];
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -1; i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) {
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
dep[u] = Min + 1;
gap[dep[u]]++;
if(u != st) u = edge[S[--top] ^ 1].to;
}
return ans;
}
示例8: main
int main()
{
//Algorithms::testQuickSort();
//Algorithms::testCountingSort();
//Algorithms::lis();
//Algorithms::editDistance();
//Algorithms::pocket();
//Algorithms::uniquePocket();
DFS();
BFS();
return 0;
}
示例9: main
int main()
{
LinkGraph **g = Create_Graph_List();
Input_Graph_Edge(g);
Print_Graph_List(g);
BFS(g,0);
return 0;
}
示例10: ExcluirAresta
static GRA_tpCondRet ExcluirAresta (GRA_tppGrafo grafo, tpVertice* v, tpVertice* u) {
RemoverAresta(v, u);//mexe só em v, ou deveria
RemoverAresta(u, v);
//BFS pra detectar se é necessário gerar nova componente.
if (BFS(v,u) != 1) { //Estão em componentes distintas
if (LIS_InserirElementoApos(grafo->componentes, u) != LIS_CondRetOK) {
return GRA_CondRetFaltouMemoria;
}
}
return GRA_CondRetOK;
}
示例11: feasible_placement
bool feasible_placement (std::vector<GraphNode>& nodes)
{
for (auto& node: nodes) {
if (node.d == -1) {
node.d = 0;
if (!BFS(&node)) {
return false;
}
}
}
return true;
}
示例12: main
int main()
{
for(int i=0; i<8; i++)
{
fscanf(fin,"%d",&target[i]);
target[i]--;
}
init();
BFS();
output();
return 0;
}
示例13: solve
void solve(unsigned start, unsigned end)
{ unsigned k;
for (k = 0; k < n; k++) { used[k] = 0; pred[k] = -1; }
BFS(start);
if (pred[end] > -1) {
printf("Намереният път е: \n");
printf("\nДължината на пътя е %u\n", printPath(end));
}
else {
printf("Път между двата върха не съществува! \n");
}
}
示例14: main
int main()
{
// freopen("xxxx.in", "r", stdin);
//freopen("xxxx.out", "w",stdout);
while (scanf("%d %d", &m, &n) == 2){
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j){
scanf("%d", &map[i][j]);
}
startHP = map[0][0];
map[0][0] = 0;
tot = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (map[i][j] > 0){
X[tot] = i, Y[tot] = j, getHP[tot] = map[i][j];
idx[i][j] = tot++;
}
else idx[i][j] = -1;
X[tot] = 0, Y[tot] = 0;
if (idx[0][0] == -1) idx[0][0] = tot;
++tot;
X[tot] = m-1, Y[tot] = n-1;
if (idx[m-1][n-1] == -1) idx[m-1][n-1] = tot;
++tot;
for (int i = 0; i < tot; ++i) BFS(X[i], Y[i], dis[i]);
tot-=2;
int max = 1<<tot;
for (int i = 0; i < tot; ++i)
for (int j = 0; j < max; ++j)
f[i][j] = -1;
for (int i = 0; i < tot; ++i)
if (startHP >= dis[tot][i])
f[i][1<<i] = startHP-dis[tot][i]+getHP[i];
int ans = -1;
if (ans < startHP - dis[tot][tot+1]) ans = startHP-dis[tot][tot+1];
for (int k = 0; k < max; ++k)
for (int i = 0; i < tot; ++i){
if (f[i][k] < 0) continue;
int tmp = f[i][k] - dis[i][tot+1];
if (tmp > ans) ans = tmp;
for (int j = 0, test = 1; j < tot; ++j, test <<= 1){
if (k & test) continue;
if (f[i][k] - dis[i][j] >= 0 && f[j][k|test] < f[i][k]-dis[i][j]+getHP[j])
f[j][k|test] = f[i][k]-dis[i][j]+getHP[j];
}
}
if (ans == -1) puts("you loss!");
else printf("%d\n", ans);
}
return 0;
}
示例15: BFSTraversal
void BFSTraversal(graph *p){
int node_num = p->node_num;
int i;
for(i = 0; i < node_num; i++){
visited[i] = 0;
}
for(i = 0; i < node_num; i++){
if(visited[i]==0){
BFS(p, i);
}
}
printf("\n");
}