Skip to content

Commit 0f90c41

Browse files
committed
Update
1 parent e44fe3b commit 0f90c41

22 files changed

+368
-0
lines changed

5/1.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
stack<int> s;
6+
7+
int main(void) {
8+
// 삽입(5) - 삽입(2) - 삽입(3) - 삽입(7) - 삭제() - 삽입(1) - 삽입(4) - 삭제()
9+
s.push(5);
10+
s.push(2);
11+
s.push(3);
12+
s.push(7);
13+
s.pop();
14+
s.push(1);
15+
s.push(4);
16+
s.pop();
17+
// 스택의 최상단 원소부터 출력
18+
while (!s.empty()) {
19+
cout << s.top() << ' ';
20+
s.pop();
21+
}
22+
}

5/1.java

Whitespace-only changes.

5/10.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, m;
6+
int graph[1001][1001];
7+
8+
// DFS로 특정 노드를 방문하고 연결된 모든 노드들도 방문
9+
bool dfs(int x, int y) {
10+
// 주어진 범위를 벗어나는 경우에는 즉시 종료
11+
if (x <= -1 || x >=n || y <= -1 || y >= m) {
12+
return false;
13+
}
14+
// 현재 노드를 아직 방문하지 않았다면
15+
if (graph[x][y] == 0) {
16+
// 해당 노드 방문 처리
17+
graph[x][y] = 1;
18+
// 상, 하, 좌, 우의 위치들도 모두 재귀적으로 호출
19+
dfs(x - 1, y);
20+
dfs(x, y - 1);
21+
dfs(x + 1, y);
22+
dfs(x, y + 1);
23+
return true;
24+
}
25+
return false;
26+
}
27+
28+
int main() {
29+
// N, M을 공백을 기준으로 구분하여 입력 받기
30+
cin >> n >> m;
31+
// 2차원 리스트의 맵 정보 입력 받기
32+
for (int i = 0; i < n; i++) {
33+
for (int j = 0; j < m; j++) {
34+
scanf("%1d", &graph[i][j]);
35+
}
36+
}
37+
// 모든 노드(위치)에 대하여 음료수 채우기
38+
int result = 0;
39+
for (int i = 0; i < n; i++) {
40+
for (int j = 0; j < m; j++) {
41+
// 현재 위치에서 DFS 수행
42+
if (dfs(i, j)) {
43+
result += 1;
44+
}
45+
}
46+
}
47+
cout << result << '\n'; // 정답 출력
48+
}

5/10.java

Whitespace-only changes.

5/11.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, m;
6+
int graph[201][201];
7+
8+
// 이동할 네 가지 방향 정의 (상, 하, 좌, 우)
9+
int dx[] = {-1, 1, 0, 0};
10+
int dy[] = {0, 0, -1, 1};
11+
12+
int bfs(int x, int y) {
13+
// 큐(Queue) 구현을 위해 queue 라이브러리 사용
14+
queue<pair<int, int> > q;
15+
q.push({x, y});
16+
// 큐가 빌 때까지 반복하기
17+
while(!q.empty()) {
18+
int x = q.front().first;
19+
int y = q.front().second;
20+
q.pop();
21+
// 현재 위치에서 4가지 방향으로의 위치 확인
22+
for (int i = 0; i < 4; i++) {
23+
int nx = x + dx[i];
24+
int ny = y + dy[i];
25+
// 미로 찾기 공간을 벗어난 경우 무시
26+
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
27+
// 벽인 경우 무시
28+
if (graph[nx][ny] == 0) continue;
29+
// 해당 노드를 처음 방문하는 경우에만 최단 거리 기록
30+
if (graph[nx][ny] == 1) {
31+
graph[nx][ny] = graph[x][y] + 1;
32+
q.push({nx, ny});
33+
}
34+
}
35+
}
36+
// 가장 오른쪽 아래까지의 최단 거리 반환
37+
return graph[n - 1][m - 1];
38+
}
39+
40+
int main(void) {
41+
// N, M을 공백을 기준으로 구분하여 입력 받기
42+
cin >> n >> m;
43+
// 2차원 리스트의 맵 정보 입력 받기
44+
for (int i = 0; i < n; i++) {
45+
for (int j = 0; j < m; j++) {
46+
scanf("%1d", &graph[i][j]);
47+
}
48+
}
49+
// BFS를 수행한 결과 출력
50+
cout << bfs(0, 0) << '\n';
51+
return 0;
52+
}

5/11.java

Whitespace-only changes.

5/2.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
queue<int> q;
6+
7+
int main(void) {
8+
// 삽입(5) - 삽입(2) - 삽입(3) - 삽입(7) - 삭제() - 삽입(1) - 삽입(4) - 삭제()
9+
q.push(5);
10+
q.push(2);
11+
q.push(3);
12+
q.push(7);
13+
q.pop();
14+
q.push(1);
15+
q.push(4);
16+
q.pop();
17+
// 먼저 들어온 원소부터 추출
18+
while (!q.empty()) {
19+
cout << q.front() << ' ';
20+
q.pop();
21+
}
22+
}

5/2.java

Whitespace-only changes.

5/3.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
void recursiveFunction() {
6+
cout << "재귀 함수를 호출합니다." << '\n';
7+
recursiveFunction();
8+
}
9+
10+
int main(void) {
11+
recursiveFunction();
12+
}

5/3.java

Whitespace-only changes.

5/4.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
void recursiveFunction(int i) {
6+
// 100번째 호출을 했을 때 종료되도록 종료 조건 명시
7+
if (i == 100) return;
8+
cout << i << "번째 재귀 함수에서 " << i + 1 << "번째 재귀함수를 호출합니다." << '\n';
9+
recursiveFunction(i + 1);
10+
cout << i << "번째 재귀 함수를 종료합니다." << '\n';
11+
}
12+
13+
int main(void) {
14+
recursiveFunction(1);
15+
}

5/4.java

Whitespace-only changes.

5/5.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// 반복적으로 구현한 n!
6+
int factorialIterative(int n) {
7+
int result = 1;
8+
// 1부터 n까지의 수를 차례대로 곱하기
9+
for (int i = 1; i <= n; i++) {
10+
result *= i;
11+
}
12+
return result;
13+
}
14+
15+
// 재귀적으로 구현한 n!
16+
int factorialRecursive(int n) {
17+
// n이 1 이하인 경우 1을 반환
18+
if (n <= 1) return 1;
19+
// n! = n * (n - 1)!를 그대로 코드로 작성하기
20+
return n * factorialRecursive(n - 1);
21+
}
22+
23+
int main(void) {
24+
// 각각의 방식으로 구현한 n! 출력(n = 5)
25+
cout << "반복적으로 구현:" << factorialIterative(5) << '\n';
26+
cout << "재귀적으로 구현:" << factorialRecursive(5) << '\n';
27+
}

5/5.java

Whitespace-only changes.

5/6.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <bits/stdc++.h>
2+
#define INF 999999999 // 무한의 비용 선언
3+
4+
using namespace std;
5+
6+
// 2차원 리스트를 이용해 인접 행렬 표현
7+
int graph[3][3] = {
8+
{0, 7, 5},
9+
{7, 0, INF},
10+
{5, INF, 0}
11+
};
12+
13+
int main(void) {
14+
// 그래프 출력
15+
for (int i = 0; i < 3; i++) {
16+
for (int j = 0; j < 3; j++) {
17+
cout << graph[i][j] << ' ';
18+
}
19+
cout << '\n';
20+
}
21+
}

5/6.java

Whitespace-only changes.

5/7.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// 행(Row)이 3개인 인접 리스트 표현
6+
vector<pair<int, int> > graph[3];
7+
8+
int main(void) {
9+
// 노드 0에 연결된 노드 정보 저장 {노드, 거리}
10+
graph[0].push_back({1, 7});
11+
graph[0].push_back({2, 5});
12+
13+
// 노드 1에 연결된 노드 정보 저장 {노드, 거리}
14+
graph[1].push_back({0, 7});
15+
16+
// 노드 2에 연결된 노드 정보 저장 {노드, 거리}
17+
graph[2].push_back({0, 5});
18+
19+
// 그래프 출력
20+
for (int i = 0; i < 3; i++) {
21+
for (int j = 0; j < graph[i].size(); j++) {
22+
cout << '(' << graph[i][j].first << ',' << graph[i][j].second << ')' << ' ';
23+
}
24+
cout << '\n';
25+
}
26+
}

5/7.java

Whitespace-only changes.

5/8.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
bool visited[9];
6+
vector<int> graph[9];
7+
8+
// DFS 함수 정의
9+
void dfs(int x) {
10+
// 현재 노드를 방문 처리
11+
visited[x] = true;
12+
cout << x << ' ';
13+
// 현재 노드와 연결된 다른 노드를 재귀적으로 방문
14+
for (int i = 0; i < graph[x].size(); i++) {
15+
int y = graph[x][i];
16+
if (!visited[y]) dfs(y);
17+
}
18+
}
19+
20+
int main(void) {
21+
// 노드 1에 연결된 노드 정보 저장
22+
graph[1].push_back(2);
23+
graph[1].push_back(3);
24+
graph[1].push_back(8);
25+
26+
// 노드 2에 연결된 노드 정보 저장
27+
graph[2].push_back(1);
28+
graph[2].push_back(7);
29+
30+
// 노드 3에 연결된 노드 정보 저장
31+
graph[3].push_back(1);
32+
graph[3].push_back(4);
33+
graph[3].push_back(5);
34+
35+
// 노드 4에 연결된 노드 정보 저장
36+
graph[4].push_back(3);
37+
graph[4].push_back(5);
38+
39+
// 노드 5에 연결된 노드 정보 저장
40+
graph[5].push_back(3);
41+
graph[5].push_back(4);
42+
43+
// 노드 6에 연결된 노드 정보 저장
44+
graph[6].push_back(7);
45+
46+
// 노드 7에 연결된 노드 정보 저장
47+
graph[7].push_back(2);
48+
graph[7].push_back(6);
49+
graph[7].push_back(8);
50+
51+
// 노드 8에 연결된 노드 정보 저장
52+
graph[8].push_back(1);
53+
graph[8].push_back(7);
54+
55+
dfs(1);
56+
}

5/8.java

Whitespace-only changes.

5/9.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
bool visited[9];
6+
vector<int> graph[9];
7+
8+
// BFS 함수 정의
9+
void bfs(int start) {
10+
queue<int> q;
11+
q.push(start);
12+
// 현재 노드를 방문 처리
13+
visited[start] = true;
14+
// 큐가 빌 때까지 반복
15+
while(!q.empty()) {
16+
// 큐에서 하나의 원소를 뽑아 출력
17+
int x = q.front();
18+
q.pop();
19+
cout << x << ' ';
20+
// 해당 원소와 연결된, 아직 방문하지 않은 원소들을 큐에 삽입
21+
for(int i = 0; i < graph[x].size(); i++) {
22+
int y = graph[x][i];
23+
if(!visited[y]) {
24+
q.push(y);
25+
visited[y] = true;
26+
}
27+
}
28+
}
29+
}
30+
31+
int main(void) {
32+
// 노드 1에 연결된 노드 정보 저장
33+
graph[1].push_back(2);
34+
graph[1].push_back(3);
35+
graph[1].push_back(8);
36+
37+
// 노드 2에 연결된 노드 정보 저장
38+
graph[2].push_back(1);
39+
graph[2].push_back(7);
40+
41+
// 노드 3에 연결된 노드 정보 저장
42+
graph[3].push_back(1);
43+
graph[3].push_back(4);
44+
graph[3].push_back(5);
45+
46+
// 노드 4에 연결된 노드 정보 저장
47+
graph[4].push_back(3);
48+
graph[4].push_back(5);
49+
50+
// 노드 5에 연결된 노드 정보 저장
51+
graph[5].push_back(3);
52+
graph[5].push_back(4);
53+
54+
// 노드 6에 연결된 노드 정보 저장
55+
graph[6].push_back(7);
56+
57+
// 노드 7에 연결된 노드 정보 저장
58+
graph[7].push_back(2);
59+
graph[7].push_back(6);
60+
graph[7].push_back(8);
61+
62+
// 노드 8에 연결된 노드 정보 저장
63+
graph[8].push_back(1);
64+
graph[8].push_back(7);
65+
66+
bfs(1);
67+
}

5/9.java

Whitespace-only changes.

0 commit comments

Comments
 (0)