Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.io.*;
import java.util.*;

public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int V = Integer.parseInt(st.nextToken());

List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= N; i++) {
graph.add(new ArrayList<>());
}

for (int i = 0; i < M; i++) {
st = new StringTokenizer(bf.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
graph.get(u).add(v);
graph.get(v).add(u);
}

for (int i = 1; i <= N; i++) {
Collections.sort(graph.get(i));
}

boolean[] visited = new boolean[N + 1];
DFS(graph, V, visited);
System.out.println();

visited = new boolean[N + 1];
BFS(graph, V, visited);
}

// DFS
public static void DFS(List<List<Integer>> graph, int v, boolean[] visited) {
visited[v] = true;
System.out.print(v + " ");
for (int next : graph.get(v)) {
if (!visited[next]) {
DFS(graph, next, visited);
}
}
}

// BFS
public static void BFS(List<List<Integer>> graph, int start, boolean[] visited) {
Queue<Integer> q = new LinkedList<>();
q.add(start);
visited[start] = true;

while (!q.isEmpty()) {
int v = q.poll();
System.out.print(v + " ");
for (int next : graph.get(v)) {
if (!visited[next]) {
visited[next] = true;
q.add(next);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
public static void main (String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // 수빈이가 있는 위치
int K = sc.nextInt(); // 동생이 있는 위치

boolean[] visited = new boolean[100001];
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{N, 0});

while (!queue.isEmpty()) {
int[] cur = queue.poll();
int p = cur[0];
int count = cur[1];

if (p == K) {
System.out.println(count);
break;
}

for (int next: new int[]{p-1, p+1, 2*p}) {
if (next >= 0 && next <= 100000 && !visited[next]) {
visited[next] = true;
queue.offer(new int[]{next, count+1});
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.*;

class Solution {
public int solution(int[][] maps) {
int answer = -1;
int n = maps.length;
int m = maps[0].length;
boolean[][] visited = new boolean[n][m];
int[][] direction = {
{1, 0},
{-1, 0},
{0, -1},
{0, 1}
};
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{0,0,0});

while(!queue.isEmpty()) {
int[] next = queue.poll();

if (next[0] == n-1 && next[1] == m-1) {
answer = next[2] + 1;
System.out.println(answer);
break;
}

for (int i = 0; i < direction.length; i++) {
int dx = direction[i][0] + next[0];
int dy = direction[i][1] + next[1];
int count = next[2];

if (dx >= 0 && dy >= 0 && dx < n && dy < m && maps[dx][dy] == 1 && !visited[dx][dy]) {
queue.offer(new int[]{dx, dy, count+1});
visited[dx][dy] = true;

}
}
}
return answer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.*;

class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
Queue<Integer> q = new LinkedList<>();
boolean[] visited = new boolean[n+1];

for (int s = 0 ; s < n; s++) {
if (!visited[s+1]) {
q.offer(s+1);
while(!q.isEmpty()) {
int next = q.poll();

for (int i = 1; i <= n; i++) {
if (computers[next-1][i-1] == 1 && !visited[i]) {
q.offer(i);
visited[i] = true;
}
}

}
answer++;
}

}



return answer;
}
}