Skip to content

Commit

Permalink
feat: 알고리즘 문제 풀이
Browse files Browse the repository at this point in the history
feat: 알고리즘 문제 풀이
  • Loading branch information
dlwnsgus777 committed Nov 29, 2022
1 parent 37331d6 commit f184519
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 5 deletions.
56 changes: 51 additions & 5 deletions baekjoon/src/main/java/baekjoon/dfs_bfs/DFS와BFS.java
Expand Up @@ -3,25 +3,71 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class DFSBFS {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static boolean[] visited;
static int[][] graph;
public static void main(String[] args) throws IOException {
String[] arr = br.readLine().split(" ");

int n = Integer.parseInt(arr[0]);
int m = Integer.parseInt(arr[0]);
int startIndex = Integer.parseInt(arr[0]);
int m = Integer.parseInt(arr[1]);
int startIndex = Integer.parseInt(arr[2]);

int[][] graph = new int[][]
graph = new int[n + 1][n + 1];

visited = new boolean[n + 1];

for (int i = 0; i < m; i++) {
String[] node = br.readLine().split(" ");
int a = Integer.parseInt(node[0]);
int b = Integer.parseInt(node[1]);

graph[a][b] = graph[b][a] = 1;
}

dfs(startIndex);

System.out.println();

Arrays.fill(visited, false);

bfs(startIndex);
}

private void dfs(int nodeIndex) {
private static void dfs(int nodeIndex) {
visited[nodeIndex] = true;

System.out.print(nodeIndex + " ");

for (int i = 1; i < graph.length; i++) {
if(graph[nodeIndex][i] == 1 && !visited[i]) {
dfs(i);
}
}
}

private void bfs(int nodeIndex) {
private static void bfs(int nodeIndex) {
Queue<Integer> queue = new LinkedList<>();

queue.add(nodeIndex);
visited[nodeIndex] = true;
System.out.print(nodeIndex + " ");

while (!queue.isEmpty()) {
int temp = queue.poll();

for (int i = 1; i < graph.length; i++) {
if (graph[temp][i] == 1 && !visited[i]) {
queue.add(i);
visited[i] = true;
System.out.print(i + " ");
}
}
}
}
}
55 changes: 55 additions & 0 deletions baekjoon/src/main/java/baekjoon/dfs_bfs/바닥장식.java
@@ -0,0 +1,55 @@
package baekjoon.dfs_bfs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.Buffer;

public class 바닥장식 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static String[][] graph;

public static void main(String[] args) throws IOException {
String[] arr = br.readLine().split(" ");

int n = Integer.parseInt(arr[0]);
int m = Integer.parseInt(arr[1]);

graph = new String[n][m];

for (int i = 0; i < n; i++) {
char[] tile = br.readLine().toCharArray();

for (int j = 0; j < m; j++) {
graph[i][j] = String.valueOf(tile[j]);
}
}

System.out.println("*************************");
int count = 0;

for (int i = 0; i < graph.length; i++) {
int tmp = 0;
for (int j = 0; j < graph[i].length; j++) {
if (graph[i][j].equals("|")) {
tmp = 0;
} else {
count++;
}
}
}

for (int i = 0; i < graph.length; i++) {
int tmp = 0;
for (int j = 0; j < graph[i].length; j++) {
if (graph[j][i].equals("-")) {
tmp = 0;
} else {
count++;
}
}
}

System.out.println(count);
}
}
47 changes: 47 additions & 0 deletions baekjoon/src/main/java/baekjoon/dfs_bfs/바이러스.java
@@ -0,0 +1,47 @@
package baekjoon.dfs_bfs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class 바이러스 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int[][] graph;
static boolean[] visited;
static int result;

public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine());
int m = Integer.parseInt(br.readLine());

graph = new int[n + 1][n + 1];
visited = new boolean[n + 1];

int startIndex = 1;

result = 0;

for (int i = 0; i < m; i++) {
String[] arr = br.readLine().split(" ");
int a = Integer.parseInt(arr[0]);
int b = Integer.parseInt(arr[1]);

graph[a][b] = graph[b][a] = 1;
}

dfs(startIndex);

System.out.println(result);
}

static void dfs(int startIndex) {
visited[startIndex] = true;

for (int i = 1; i < graph.length; i++) {
if (graph[startIndex][i] == 1 && !visited[i]) {
result++;
dfs(i);
}
}
}
}
52 changes: 52 additions & 0 deletions baekjoon/src/main/java/baekjoon/dfs_bfs/한동이.java
@@ -0,0 +1,52 @@
package baekjoon.dfs_bfs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

// TODO: 틀림....
public class 한동이 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int[] graph;
static boolean[] visited;
static int result;
static int count;
static int temp;
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine());

graph = new int[n + 1];
visited = new boolean[n + 1];
temp = Integer.MIN_VALUE;

for (int i = 1; i < graph.length; i++) {
graph[i] = Integer.parseInt(br.readLine());
}

for (int i = 1; i <= n; i++) {
visited = new boolean[n + 1];
dfs(i);

if (temp < count) {
temp = count;
result = i;
}

count = 0;
}

System.out.println(result);
}

static void dfs(int startIdx) {
visited[startIdx] = true;

for (int i = 1; i < graph.length; i++) {
if (!visited[graph[i]]) {
count++;
dfs(graph[i]);
}
}
}
}

0 comments on commit f184519

Please sign in to comment.