문제 번호 1841. -- [ 2024 데이터구조 실습 ] BFS

1841: [ 2024 데이터구조 실습 ] BFS

시간 제한: 1 Sec  메모리 제한: 128 MB
제출: 349  해결 문제 수: 104
[제출][채점 상황 열람][게시판]

문제 설명

#include <stdio.h>

#define NV 6    // number of vertices <-- 이것은 고정이니 수정 금지

/*
*  아래 2개 함수를 구현하시오. 전역변수 등은 필요에 따라 선언 가능
*
   1) void addEdge (int graph[][NV], int v1, int v2)
	  - vertex v1과 v2 사이에 weight 1인 edge를 추가한다.
	  - edge는 undirected이다.

   2) void BreadthFirstSearch(int graph[][NV], int start_vertex)

	  - graph에 대해 start_vertex부터 BFS를 수행
	  - vertex를 BFS를 이용해서 방문할 때 마다 vertex 번호 (0 ~ 5) 출력
		이때, vertex 번호 사이는 공백 1개로 구분.
		마지막 vertex 번호 뒤에도 공백 1개가 있어야 하고, 줄바꿈 문자는 없어야 함.
		start_vertex부터 출력되어야 함.

	  - BFS과정에서 vertex가 queue에 추가될 때 "vertex번호가 낮은 것을 먼저 enqueue"

*/

// ---------------------- 이하 절대 수정 금지 -----------------------------------

int main(void) {

	int v1 = 0;
	int v2 = 0;
	int graph[NV][NV] = { 0 };

	while (1) {

		scanf("%d %d", &v1, &v2);

		if (v1 < 0) {
			break;
		}
		addEdge(graph, v1, v2);

	}

	BreadthFirstSearch(graph, -v1);

	return 0;

}

입력

출력

도움말

출처

[제출][채점 상황 열람]