Skip to content

Commit 158ef58

Browse files
committed
Update
1 parent 4c192a9 commit 158ef58

12 files changed

+452
-1
lines changed

5/1.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
Stack<Integer> s = new Stack<>();
7+
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+
System.out.println(s.peek());
20+
s.pop();
21+
}
22+
}
23+
24+
}

5/10.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using namespace std;
44

55
int n, m;
6-
int graph[1001][1001];
6+
int graph[1000][1000];
77

88
// DFS로 특정 노드를 방문하고 연결된 모든 노드들도 방문
99
bool dfs(int x, int y) {

5/10.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static int n, m;
6+
public static int[][] graph = new int[1000][1000];
7+
8+
// DFS로 특정 노드를 방문하고 연결된 모든 노드들도 방문
9+
public static boolean 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+
public static void main(String[] args) {
29+
Scanner sc = new Scanner(System.in);
30+
31+
// N, M을 공백을 기준으로 구분하여 입력 받기
32+
n = sc.nextInt();
33+
m = sc.nextInt();
34+
sc.nextLine(); // 버퍼 지우기
35+
36+
// 2차원 리스트의 맵 정보 입력 받기
37+
for (int i = 0; i < n; i++) {
38+
String str = sc.nextLine();
39+
for (int j = 0; j < m; j++) {
40+
graph[i][j] = str.charAt(j) - '0';
41+
}
42+
}
43+
44+
// 모든 노드(위치)에 대하여 음료수 채우기
45+
int result = 0;
46+
for (int i = 0; i < n; i++) {
47+
for (int j = 0; j < m; j++) {
48+
// 현재 위치에서 DFS 수행
49+
if (dfs(i, j)) {
50+
result += 1;
51+
}
52+
}
53+
}
54+
System.out.println(result); // 정답 출력
55+
}
56+
57+
}

5/11.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.util.*;
2+
3+
class Node {
4+
5+
private int index;
6+
private int distance;
7+
8+
public Node(int index, int distance) {
9+
this.index = index;
10+
this.distance = distance;
11+
}
12+
13+
public int getIndex() {
14+
return this.index;
15+
}
16+
17+
public int getDistance() {
18+
return this.distance;
19+
}
20+
}
21+
22+
public class Main {
23+
24+
public static int n, m;
25+
public static int[][] graph = new int[201][201];
26+
27+
// 이동할 네 가지 방향 정의 (상, 하, 좌, 우)
28+
public static int dx[] = {-1, 1, 0, 0};
29+
public static int dy[] = {0, 0, -1, 1};
30+
31+
public static int bfs(int x, int y) {
32+
// 큐(Queue) 구현을 위해 queue 라이브러리 사용
33+
Queue<Node> q = new LinkedList<>();
34+
q.offer(new Node(x, y));
35+
// 큐가 빌 때까지 반복하기
36+
while(!q.isEmpty()) {
37+
Node node = q.poll();
38+
x = node.getIndex();
39+
y = node.getDistance();
40+
// 현재 위치에서 4가지 방향으로의 위치 확인
41+
for (int i = 0; i < 4; i++) {
42+
int nx = x + dx[i];
43+
int ny = y + dy[i];
44+
// 미로 찾기 공간을 벗어난 경우 무시
45+
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
46+
// 벽인 경우 무시
47+
if (graph[nx][ny] == 0) continue;
48+
// 해당 노드를 처음 방문하는 경우에만 최단 거리 기록
49+
if (graph[nx][ny] == 1) {
50+
graph[nx][ny] = graph[x][y] + 1;
51+
q.offer(new Node(nx, ny));
52+
}
53+
}
54+
}
55+
// 가장 오른쪽 아래까지의 최단 거리 반환
56+
return graph[n - 1][m - 1];
57+
}
58+
59+
public static void main(String[] args) {
60+
Scanner sc = new Scanner(System.in);
61+
62+
// N, M을 공백을 기준으로 구분하여 입력 받기
63+
n = sc.nextInt();
64+
m = sc.nextInt();
65+
sc.nextLine(); // 버퍼 지우기
66+
67+
// 2차원 리스트의 맵 정보 입력 받기
68+
for (int i = 0; i < n; i++) {
69+
String str = sc.nextLine();
70+
for (int j = 0; j < m; j++) {
71+
graph[i][j] = str.charAt(j) - '0';
72+
}
73+
}
74+
75+
// BFS를 수행한 결과 출력
76+
System.out.println(bfs(0, 0));
77+
}
78+
79+
}

5/2.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
Queue<Integer> q = new LinkedList<>();
7+
8+
// 삽입(5) - 삽입(2) - 삽입(3) - 삽입(7) - 삭제() - 삽입(1) - 삽입(4) - 삭제()
9+
q.offer(5);
10+
q.offer(2);
11+
q.offer(3);
12+
q.offer(7);
13+
q.poll();
14+
q.offer(1);
15+
q.offer(4);
16+
q.poll();
17+
// 먼저 들어온 원소부터 추출
18+
while (!q.isEmpty()) {
19+
System.out.println(q.poll());
20+
}
21+
}
22+
23+
}

5/3.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static void recursiveFunction() {
6+
System.out.println("재귀 함수를 호출합니다.");
7+
recursiveFunction();
8+
}
9+
10+
public static void main(String[] args) {
11+
recursiveFunction();
12+
}
13+
14+
}

5/4.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static void recursiveFunction(int i) {
6+
// 100번째 호출을 했을 때 종료되도록 종료 조건 명시
7+
if (i == 100) return;
8+
System.out.println(i + "번째 재귀 함수에서 " + (i + 1) + "번째 재귀함수를 호출합니다.");
9+
recursiveFunction(i + 1);
10+
System.out.println(i + "번째 재귀 함수를 종료합니다.");
11+
}
12+
13+
public static void main(String[] args) {
14+
recursiveFunction(1);
15+
}
16+
17+
}

5/5.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
// 반복적으로 구현한 n!
6+
public static 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+
public static 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+
public static void main(String[] args) {
24+
// 각각의 방식으로 구현한 n! 출력(n = 5)
25+
System.out.println("반복적으로 구현:" + factorialIterative(5));
26+
System.out.println("재귀적으로 구현:" + factorialRecursive(5));
27+
}
28+
29+
}

5/6.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static final int INF = 999999999;
6+
7+
// 2차원 리스트를 이용해 인접 행렬 표현
8+
public static int[][] graph = {
9+
{0, 7, 5},
10+
{7, 0, INF},
11+
{5, INF, 0}
12+
};
13+
14+
public static void main(String[] args) {
15+
// 그래프 출력
16+
for (int i = 0; i < 3; i++) {
17+
for (int j = 0; j < 3; j++) {
18+
System.out.print(graph[i][j] + " ");
19+
}
20+
System.out.println();
21+
}
22+
}
23+
24+
}

5/7.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.*;
2+
3+
class Node {
4+
5+
private int index;
6+
private int distance;
7+
8+
public Node(int index, int distance) {
9+
this.index = index;
10+
this.distance = distance;
11+
}
12+
13+
public void show() {
14+
System.out.print("(" + this.index + "," + this.distance + ") ");
15+
}
16+
}
17+
18+
public class Main {
19+
20+
// 행(Row)이 3개인 인접 리스트 표현
21+
public static ArrayList<ArrayList<Node>> graph = new ArrayList<ArrayList<Node>>();
22+
23+
public static void main(String[] args) {
24+
// 그래프 초기화
25+
for (int i = 0; i < 3; i++) {
26+
graph.add(new ArrayList<Node>());
27+
}
28+
29+
// 노드 0에 연결된 노드 정보 저장 (노드, 거리)
30+
graph.get(0).add(new Node(1, 7));
31+
graph.get(0).add(new Node(2, 5));
32+
33+
// 노드 1에 연결된 노드 정보 저장 (노드, 거리)
34+
graph.get(1).add(new Node(0, 7));
35+
36+
// 노드 2에 연결된 노드 정보 저장 (노드, 거리)
37+
graph.get(2).add(new Node(0, 5));
38+
39+
// 그래프 출력
40+
for (int i = 0; i < 3; i++) {
41+
for (int j = 0; j < graph.get(i).size(); j++) {
42+
graph.get(i).get(j).show();
43+
}
44+
System.out.println();
45+
}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)