Skip to content

Commit 0f90c41

Browse files
committed
Update
1 parent e44fe3b commit 0f90c41

File tree

22 files changed

+368
-0
lines changed

22 files changed

+368
-0
lines changed

5/1.cpp

Lines changed: 22 additions & 0 deletions
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

Lines changed: 48 additions & 0 deletions
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

Lines changed: 52 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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.

0 commit comments

Comments
 (0)