好久没弄C++了,上学期颓废了半学期,这学期开学就搞课程设计快疯了。待会要考试CSP,所以弄点代码储备,待会到了考场说不定能省点功夫!
#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:
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.
[进程已完成]
祝我好运!发誓这次考试后一定苦学!上学期太飘了。