| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 데이터 통신과 컴퓨터 네트워크
- 밑바닥부터 만드는 컴퓨팅 시스템 2판
- C++
- 메타버스
- 게임 수학
- hanbit.co.kr
- HANBIT Academy
- 생능출판
- 주우석
- https://insightbook.co.kr/
- unity6
- 입출력과 사칙연산
- (주)책만
- 전공자를 위한 C언어 프로그래밍
- 이득우
- 알고리즘
- C
- BOJ
- 이득우의 게임수학
- C#
- 백준
- 김진홍 옮김
- 박기현
- 일기
- Noam Nisan
- booksr.co.kr
- cyphenengine
- JavaScript
- 잡생각 정리글
- The Elements of Computing Systems 2/E
Archives
- Today
- Total
cyphen156
백준-그래프와 순회 1260 DFS와 BFS 본문
간단하게 탐색한 결과를 출력하는 프로그램
단, 1:1이 아닌 다 : 다 관계로 연결될 수 있어 간선이 여러개 존재할 수 잇다.
이 경우 정점 번호가 작은 것을 먼저 방문한다.
더 이상 방문할 수 있는 점이 없는 경우 종료한다.
제약사항
- 1 <= N < 1,001
- 1 <= M < 10,001
주의 사항
없다.
CPP풀이
DFS와 BFS_1260.cpp
/**
* 백준 DFS와 BFS_1260
* 간단하게 탐색한 결과를 출력하는 프로그램
* 단, 1:1이 아닌 다 : 다 관계로 연결될 수 있어 간선이 여러개 존재할 수 잇다.
* 이 경우 정점 번호가 작은 것을 먼저 방문한다.
* 더 이상 방문할 수 있는 점이 없는 경우 종료한다.
*
* 제한사항
*****************************************
* 1 <= N < 1,001 *
* 1 <= M < 10,001 *
*****************************************
*
*
*
* 주의
* 없다.
*
* 풀이시간 (문제 해석 + 구현)
* 1 + 30분
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
static const int MAX_VERTICIES_COUNT = 1001;
static const int MAX_EDGE_COUNT = 10001;
using namespace std;
static int N, M, start;
static vector<vector<int>> verticies;
void DFS(int start);
void BFS(int start);
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> N >> M >> start;
verticies.assign(N + 1, vector<int>());
for (int i = 0; i < M; ++i)
{
int u, v;
cin >> u >> v;
verticies[u].push_back(v);
verticies[v].push_back(u);
}
// sort edge to other
for (int i = 1; i <= N; ++i)
{
sort(verticies[i].begin(), verticies[i].end());
}
DFS(start);
cout << '\n';
BFS(start);
cout << '\n';
return 0;
}
void DFS(int start)
{
// clear
bool isVisited[MAX_VERTICIES_COUNT] = { 0 };
stack<int> stack;
// vector<int> printQ;
stack.push(start);
while (stack.empty() != true)
{
int current = stack.top();
stack.pop();
if (isVisited[current] != 0)
{
continue;
}
isVisited[current] = 1;
// printQ.push_back(next);
cout << current << ' ';
for (int i = static_cast<int>(verticies[current].size()) - 1; i >= 0; --i)
{
int next = verticies[current][i];
if (isVisited[next] == 0)
{
stack.push(next);
}
}
}
// print
// for (int i = 0; i < N; ++i)
// {
// cout << printQ[i] << ' ';
// }
}
void BFS(int start)
{
// clear
bool isVisited[MAX_VERTICIES_COUNT] = { 0 };
queue<int> q;
q.push(start);
isVisited[start] = 1;
while (q.empty() == 0)
{
int current = q.front();
q.pop();
cout << current << ' ';
for (int i = 0; i < static_cast<int>(verticies[current].size()); ++i)
{
int next = verticies[current][i];
if (isVisited[next] == 0)
{
isVisited[next] = 1;
q.push(next);
}
}
}
// print
// for (int i = 0; i < N; ++i)
// {
// cout << isVisited[i] << ' ';
// }
}
모든 예제 코드의 소스파일은 제 개인 깃허브 레포지토리에 있습니다.
Workspace/알고리듬 풀이 at main · cyphen156/Workspace
Studying . Contribute to cyphen156/Workspace development by creating an account on GitHub.
github.com
'컴퓨터공학 > 알고리듬 풀이' 카테고리의 다른 글
| 백준-그래프와 순회 1012 유기농 배추 (0) | 2025.09.01 |
|---|---|
| 백준-그래프와 순회 2667 단지번호붙이기 (0) | 2025.09.01 |
| 백준-그래프와 순회 2606 바이러스 (2) | 2025.08.28 |
| 백준-그래프와 순회 24445 알고리즘 수업 - 너비 우선 탐색 2 (1) | 2025.08.28 |
| 백준-그래프와 순회 24444 알고리즘 수업 - 너비 우선 탐색 1 (0) | 2025.08.28 |
