Disclaimer: This article is a blogger original article, reproduced, please attach Ming source ^ _ ^ https://blog.csdn.net/cprimesplus/article/details/90183133
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int Graph[5][5] = {
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
};
struct node{
int x, y;
};
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void bfs()
{
int vis[5][5];memset(vis, 0, sizeof(vis));
queue<node> q;
struct node p;
p.x = p.y = 0;
p.data = Graph[0][0];
q.push(p);
while(!q.empty())
{
struct node t = q.front();
q.pop();
for(int i = 0;i < 4;i++)
{
int x = t.x + dir[i][0];
int y = t.y + dir[i][1];
if(!vis[x][y] && x>=0 && y>=0 && x<=4 && y<=4)
{
cout<<Graph[x][y]<<' ';
vis[x][y] = 1;
struct node b;
b.x = x;
b.y = y;
q.push(b);
}
}
}
}
int main()
{
bfs();
return 0;
}