前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【图】广度优先算法(BFS)

【图】广度优先算法(BFS)

作者头像
用户1687088
发布2018-07-24 16:02:34
9490
发布2018-07-24 16:02:34
举报

正文之前

好久没弄C++了,上学期颓废了半学期,这学期开学就搞课程设计快疯了。待会要考试CSP,所以弄点代码储备,待会到了考场说不定能省点功夫!

正文

代码语言:javascript
复制
#include <iostream>
#include<queue>
#define Max 1000

using namespace std;
struct Graph
{
   int  a[10][10];
};


bool visited[10];
queue<int> path;
int width=0;

void BFS(Graph tu,int start)
{
    visited[start]=true;
    path.push(start);
    while(path.size()!=0)
    {
        int top=path.front();
        path.pop();
        visited[top]=true;
        cout<<top<<endl;
        for(int i=0;i<10;++i)
        {
            if(tu.a[top][i]!=Max && visited[i]==false)
            {
                visited[i]=true;
                path.push(i);
            }
        }

    }
}

int main()
{
    Graph tu;
    for(auto &s:tu.a)
        for(auto &x:s)
            x=Max;
    int a,b;
    for(auto &s:visited)
        s=false;
    for(int i=0;i<8;++i)
    {
        cin>>a>>b;
        tu.a[a][b]=tu.a[b][a]=1;
    }
    BFS(tu,1);
}

Output:

代码语言:javascript
复制
Last login: Sun Mar 18 11:46:37 on ttys000


= * = * = * = * = * = * = * = * = * = * = * = * = * = * 
✧。٩(ˊᗜˋ)و✧* Hello! Welcome 张照博!!开启愉快的一天吧!
= * = * = * = * = * = * = * = * = * = * = * = * = * = * 


/Users/zhangzhaobo/program/C++/BFS ; exit;
HustWolf:~ zhangzhaobo$ /Users/zhangzhaobo/program/C++/BFS ; exit;
1 4 1 3 1 5 2 5 2 3 4 5 5 6 5 7 
1
3
4
5
2
6
7
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[进程已完成]

正文之后

祝我好运!发誓这次考试后一定苦学!上学期太飘了。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 工科狗和生物喵 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 正文之前
  • 正文
  • 正文之后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档