Untitled
unknown
plain_text
6 months ago
806 B
8
Indexable
#include <stdio.h> #include <stdlib.h> #define MAX_VERTICES 100 void bfs(int graph[MAX_VERTICES][MAX_VERTICES], int m) { int visited[MAX_VERTICES] = {0}; int queue[MAX_VERTICES], front = 0, rear = 0; visited[0] = 1; queue[rear++] = 0; while (front < rear) { int current = queue[front++]; printf("%d ", current + 1); for (int i = 0; i < m; i++) { if (graph[current][i] == 1 && !visited[i]) { visited[i] = 1; queue[rear++] = i; } } } } int main() { int m; scanf("%d", &m); int graph[MAX_VERTICES][MAX_VERTICES]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { scanf("%d", &graph[i][j]); } } bfs(graph, m); return 0; }
Editor is loading...
Leave a Comment