Skip to content

Commit 88fe2c5

Browse files
authored
Merge pull request #61 from Jsumin07/feat/baekjoon
feat (Soomin): add/week8-Baekjoon_1260.java,Baekjoon_2178.java
2 parents a6a42e1 + 14c34f9 commit 88fe2c5

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

Soomin/week8/Beakjoon_1260.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package week8;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Beakjoon_1260 {
7+
static int N, M, V;
8+
static ArrayList<Integer>[] graph;
9+
static boolean[] visited;
10+
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
15+
N = Integer.parseInt(st.nextToken());
16+
M = Integer.parseInt(st.nextToken());
17+
V = Integer.parseInt(st.nextToken());
18+
19+
graph = new ArrayList[N + 1];
20+
for (int i = 1; i <= N; i++) {
21+
graph[i] = new ArrayList<>();
22+
}
23+
24+
for (int i = 0; i < M; i++) {
25+
st = new StringTokenizer(br.readLine());
26+
int a = Integer.parseInt(st.nextToken());
27+
int b = Integer.parseInt(st.nextToken());
28+
graph[a].add(b);
29+
graph[b].add(a);
30+
}
31+
32+
33+
for (int i = 1; i <= N; i++) {
34+
Collections.sort(graph[i]);
35+
}
36+
37+
visited = new boolean[N + 1];
38+
dfs(V);
39+
System.out.println();
40+
41+
visited = new boolean[N + 1];
42+
bfs(V);
43+
System.out.println();
44+
}
45+
46+
47+
static void dfs(int node) {
48+
visited[node] = true;
49+
System.out.print(node + " ");
50+
for (int next : graph[node]) {
51+
if (!visited[next]) {
52+
dfs(next);
53+
}
54+
}
55+
}
56+
57+
58+
static void bfs(int start) {
59+
Queue<Integer> queue = new LinkedList<>();
60+
visited[start] = true;
61+
queue.offer(start);
62+
63+
while (!queue.isEmpty()) {
64+
int cur = queue.poll();
65+
System.out.print(cur + " ");
66+
for (int next : graph[cur]) {
67+
if (!visited[next]) {
68+
visited[next] = true;
69+
queue.offer(next);
70+
}
71+
}
72+
}
73+
}
74+
}

Soomin/week8/Beakjoon_2178.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package week8;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Beakjoon_2178 {
7+
static int N, M;
8+
static int[][] maze;
9+
static boolean[][] visited;
10+
11+
12+
static int[] dRow = { -1, 1, 0, 0 };
13+
static int[] dCol = { 0, 0, -1, 1 };
14+
15+
public static void main(String[] args) throws IOException {
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
19+
N = Integer.parseInt(st.nextToken());
20+
M = Integer.parseInt(st.nextToken());
21+
22+
maze = new int[N][M];
23+
visited = new boolean[N][M];
24+
25+
for (int i = 0; i < N; i++) {
26+
String line = br.readLine();
27+
for (int j = 0; j < M; j++) {
28+
maze[i][j] = line.charAt(j) - '0';
29+
}
30+
}
31+
32+
int result = bfs(0, 0);
33+
System.out.println(result);
34+
}
35+
36+
static int bfs(int sr, int sc) {
37+
Queue<int[]> queue = new LinkedList<>();
38+
queue.offer(new int[] { sr, sc });
39+
visited[sr][sc] = true;
40+
41+
while (!queue.isEmpty()) {
42+
int[] cur = queue.poll();
43+
int r = cur[0];
44+
int c = cur[1];
45+
46+
47+
if (r == N - 1 && c == M - 1) {
48+
return maze[r][c];
49+
}
50+
51+
for (int d = 0; d < 4; d++) {
52+
int nr = r + dRow[d];
53+
int nc = c + dCol[d];
54+
55+
if (nr < 0 || nr >= N || nc < 0 || nc >= M) continue;
56+
if (visited[nr][nc] || maze[nr][nc] == 0) continue;
57+
58+
visited[nr][nc] = true;
59+
maze[nr][nc] = maze[r][c] + 1;
60+
queue.offer(new int[] { nr, nc });
61+
}
62+
}
63+
64+
return -1;
65+
}
66+
}

0 commit comments

Comments
 (0)