ShovelingLife
A Game Programmer
ShovelingLife
전체 방문자
오늘
어제
  • 분류 전체보기 (1072)
    • 그래픽스 (57)
      • 공통 (19)
      • 수학 물리 (22)
      • OpenGL & Vulkan (1)
      • DirectX (14)
    • 게임엔진 (183)
      • Unreal (69)
      • Unity (103)
      • Cocos2D-X (3)
      • 개인 플젝 (8)
    • 코딩테스트 (221)
      • 공통 (7)
      • 프로그래머스 (22)
      • 백준 (162)
      • LeetCode (19)
      • HackerRank (2)
      • 코딩테스트 알고리즘 (8)
    • CS (235)
      • 공통 (21)
      • 네트워크 (44)
      • OS & 하드웨어 (55)
      • 자료구조 & 알고리즘 (98)
      • 디자인패턴 (6)
      • UML (4)
      • 데이터베이스 (7)
    • 프로그래밍 언어 (348)
      • C++ (167)
      • C# (90)
      • Java (9)
      • Python (33)
      • SQL (30)
      • JavaScript (8)
      • React (7)
    • 그 외 (9)
      • Math (5)
      • 일상 (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Source Code 좌측 상단에 복사 버튼 추가 완료
  • 언리얼 엔진 C++ 빌드시간 단축 꿀팁
  • 게임 업계 코딩테스트 관련
  • 1인칭 시점으로 써내려가는 글들

인기 글

태그

  • 언리얼
  • 문자열
  • c#
  • 그래픽스
  • 클래스
  • Unity
  • 프로그래머스
  • C
  • 함수
  • SQL
  • string
  • 티스토리챌린지
  • 포인터
  • C++
  • 파이썬
  • 배열
  • 오블완
  • 백준
  • 알고리즘
  • 유니티

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

CS/자료구조 & 알고리즘

C++ 그래프를 이용한 BFS / DFS 계속 업데이트할 예정

2022. 6. 18. 23:02

출처 : https://www.youtube.com/watch?v=tWVWeAqZ0WU

 

첫번째 문제

#include <iostream>
#include <format>
#include <vector>
#include <stack>
#include <queue>
 
using namespace std;
 
#define TOTAL_SIZE 1000
#define y first
#define x second
 
vector<char> graph[TOTAL_SIZE];
bool visited[TOTAL_SIZE]{ false };
int edge = 0;
 
void DFS(int _start)
{
    cout << "DFS 시작" << endl;
    // 출력 순서 > a b d f c e
    stack<char> s;
    s.push(_start);
    visited[_start] = true;
 
    while (!s.empty())
    {
        auto top = s.top();
        cout << format("{} ", top);
        s.pop();
 
        for (int i = 0; i < graph[top].size(); i++)
        {
            auto current = graph[top][i];
 
            if (!visited[current])
            {
                visited[current] = true;
                s.push(current);
            }
        }
    }
}
 
void BFS(int _start)
{
    cout << "\n\nBFS 시작" << endl;
    memset(visited, 0, sizeof(visited));
    // 출력 순서 > a b c d e f
    queue<char> q;
    q.push(_start);
    visited[_start] = true;
 
    while (!q.empty())
    {
        char front = q.front();
        cout << format("{} ", front);
        q.pop();
 
        for (int i = 0; i < graph[front].size(); i++)
        {
            auto current = graph[front][i];
 
            if (!visited[current])
            {
                visited[current] = true;
                q.push(current);
            }
        }
    }
}
 
int main()
{
    // 그래프 생성
    // a > c
    // v   v
    // b   e
    // v  
    // d > f
    pair<char, char> tmp_pair[]
    {
        { 'a' , 'c'},
        { 'a' , 'b'},
        { 'c' , 'e'},
        { 'b' , 'd'},
        { 'd' , 'f'}
    };
    for (int i = 0; i < 5; i++)
    {
        auto data = tmp_pair[i];        
        graph[data.y].push_back(data.x);
        graph[data.x].push_back(data.y);
    }
    DFS(tmp_pair[0].y);
    BFS(tmp_pair[0].y);
    return 0;
}

두번째 문제

#include <iostream>
#include <format>
#include <vector>
#include <stack>
#include <queue>
 
using namespace std;
 
#define TOTAL_SIZE 1000
#define y first
#define x second
 
vector<char> graph[TOTAL_SIZE];
bool visited[TOTAL_SIZE]{ false };
int edge = 0;
 
bool DFS(int _start, char _find)
{
    cout << "DFS 시작" << endl;
    // 출력 순서 > a b d f c e
    stack<char> s;
    s.push(_start);
    visited[_start] = true;
 
    while (!s.empty())
    {
        auto top = s.top();
        cout << format("{} ", top);
        s.pop();
 
        if (top == _find)
            return true;
 
        for (int i = 0; i < graph[top].size(); i++)
        {
            auto current = graph[top][i];
 
            if (!visited[current])
            {
                visited[current] = true;
                s.push(current);
            }
        }
    }
    return false;
}
 
bool BFS(int _start, char _find)
{
    cout << "\n\nBFS 시작" << endl;
    memset(visited, 0, sizeof(visited));
    // 출력 순서 > a b c d e f
    queue<char> q;
    q.push(_start);
    visited[_start] = true;
 
    while (!q.empty())
    {
        char front = q.front();
        cout << format("{} ", front);
        q.pop();
 
        if (front == _find)
            return true;
 
        for (int i = 0; i < graph[front].size(); i++)
        {
            auto current = graph[front][i];
 
            if (!visited[current])
            {
                visited[current] = true;
                q.push(current);
            }
        }
    }
    return false;
}
 
int main()
{
    // 그래프 생성
    // f > g > h
    // v /> 
    // i < j
    // v  
    // k
    pair<char, char> tmp_pair[]
    {
        { 'f' , 'g'},
        { 'f' , 'i'},
        { 'i' , 'k'},
        { 'i' , 'g'},
        { 'j' , 'i'},
        { 'g' , 'h'}
    };
    char src = 'j', dst = 'f';
    for (int i = 0; i < sizeof(tmp_pair) / sizeof(int); i++)
    {
        auto data = tmp_pair[i];        
        graph[data.y].push_back(data.x);
        graph[data.x].push_back(data.y);
    }
    cout << DFS(src, dst) << endl;
    cout << BFS(src, dst) << endl;
    return 0;
}
저작자표시 (새창열림)

'CS > 자료구조 & 알고리즘' 카테고리의 다른 글

그래프 개념  (0) 2022.07.01
시간 복잡도 (Time Complexity)와 공간 복잡도 (Space Complexity)  (0) 2022.06.26
BOIDS (군중 알고리즘) RTS AI  (0) 2022.06.18
세그먼트 트리 (Segment Tree)  (0) 2022.06.16
이터레이터 Iterator (반복자)  (0) 2022.06.16
    'CS/자료구조 & 알고리즘' 카테고리의 다른 글
    • 그래프 개념
    • 시간 복잡도 (Time Complexity)와 공간 복잡도 (Space Complexity)
    • BOIDS (군중 알고리즘) RTS AI
    • 세그먼트 트리 (Segment Tree)
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바