C++

[C++] 4월 21일 코딩 테스트 수업

k-codestudy 2025. 4. 23. 04:35

DFS

#pragma once

#include <stdio.h>

class C_Grape
{
private:
	int m_arNode[9][9];
	bool m_arVisit[9];
private:
	void visitNode(int nIndex);
public:
	C_Grape();
	void DFS(int nIndex);
};
#include "gragh.h"

C_Grape::C_Grape():
	m_arNode{
		{0,1,0,0,0,0,0,0,0},
		{1,0,1,1,0,0,0,0,0},
		{0,1,0,1,1,0,0,0,0},
		{0,1,1,0,1,1,0,0,0},
		{0,0,1,1,0,0,0,0,0},
		{0,0,0,1,0,0,1,1,0},
		{0,0,0,0,0,1,0,0,1},
		{0,0,0,0,0,1,0,0,0},
		{0,0,0,0,0,0,1,0,0} },
		m_arVisit{}
{
}

void C_Grape::visitNode(int nIndex)
{
	if (m_arVisit[nIndex]) // 이미 방문한 노드면 탈출
		return;

	printf("%d", nIndex);  // 노드 출력
	m_arVisit[nIndex] = true; // 방문 체크

	for (int i = 0; i < 9; i++) // 연결된 노드 탐색
	{
		if(m_arNode[nIndex][i] == 1) // 인접하다면
		visitNode(i);                // 제귀 호출
	}
}

void C_Grape::DFS(int nIndex)
{
	visitNode(nIndex);
}
#include <iostream>
#include "gragh.h"

int main()
{
	C_Grape c_Graph{};

	c_Graph.DFS(0);
}

'C++' 카테고리의 다른 글

[C++] 4월 25일 코딩 테스트 수업  (0) 2025.04.27
[C++] 4월 23일 코딩 테스트 수업  (0) 2025.04.24
[C++] 그래프, DFS, BFS 탐색  (0) 2025.03.17
[강의] 2월 11일 수업정리  (0) 2025.02.11
[강의] 2월 10일 수업정리  (0) 2025.02.10