Skip to content

Commit 43a2030

Browse files
committed
solve: 코딩테스트 문제 풀이 - 15일차
1 parent ab76a62 commit 43a2030

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
int[] dx = {0, 0, -1, 1};
5+
int[] dy = {1, -1, 0, 0};
6+
7+
int[] start;
8+
int[] lever;
9+
int[] end;
10+
11+
int n,m;
12+
char[][] map;
13+
14+
public int solution(String[] maps) {
15+
int answer = 0;
16+
17+
n = maps.length;
18+
m = maps[0].length();
19+
map = new char[n][m];
20+
for (int i=0; i<n; i++) {
21+
char[] row = maps[i].toCharArray();
22+
for (int j=0; j<m; j++) {
23+
map[i][j] = row[j];
24+
25+
if (row[j] == 'S') {
26+
start = new int[] {i, j};
27+
} else if (row[j] == 'L') {
28+
lever = new int[] {i, j};
29+
} else if (row[j] == 'E') {
30+
end = new int[] {i, j};
31+
}
32+
33+
}
34+
}
35+
36+
int toLever = bfs(start, lever);
37+
if (toLever == -1) return -1;
38+
39+
int toEnd = bfs(lever, end);
40+
if (toEnd == -1) return -1;
41+
42+
// System.out.println(start[0] + " " + start[1]);
43+
// System.out.println(lever[0] + " " + lever[1]);
44+
45+
return toLever + toEnd;
46+
}
47+
48+
public int bfs(int[] start, int[] end) {
49+
int[][] distance = new int[n][m];
50+
for (int i=0; i<n; i++) {
51+
Arrays.fill(distance[i], -1);
52+
}
53+
54+
Queue<int[]> queue = new LinkedList<>();
55+
int startY = start[0];
56+
int startX = start[1];
57+
queue.offer(new int[] {startY, startX});
58+
distance[startY][startX] = 0;
59+
60+
while (!queue.isEmpty()) {
61+
int[] curr = queue.poll();
62+
int cx = curr[1];
63+
int cy = curr[0];
64+
65+
if (cx == end[1] && cy == end[0]) {
66+
return distance[cy][cx];
67+
}
68+
69+
for (int i=0; i<4; i++) {
70+
int ny = cy + dy[i];
71+
int nx = cx + dx[i];
72+
73+
if (nx >= 0 && nx < m && ny >=0 && ny < n) {
74+
if (map[ny][nx] != 'X' && distance[ny][nx] == -1) {
75+
distance[ny][nx] = distance[cy][cx] + 1;
76+
queue.offer(new int[]{ny, nx});
77+
}
78+
}
79+
}
80+
}
81+
return -1;
82+
}
83+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
List<List<Integer>> winGraph = new ArrayList<>();
5+
List<List<Integer>> loseGraph = new ArrayList<>();
6+
7+
public int solution(int n, int[][] results) {
8+
for (int i=0; i<=n; i++) {
9+
winGraph.add(new ArrayList<>());
10+
loseGraph.add(new ArrayList<>());
11+
}
12+
13+
for (int[] result: results) {
14+
int win = result[0];
15+
int lose = result[1];
16+
17+
winGraph.get(win).add(lose);
18+
loseGraph.get(lose).add(win);
19+
}
20+
21+
int answer = 0;
22+
for (int i=1; i<=n; i++) {
23+
int winCount = bfs(i, winGraph, n);
24+
int loseCount = bfs(i, loseGraph, n);
25+
26+
if (winCount + loseCount == n-1) {
27+
answer++;
28+
}
29+
}
30+
31+
return answer;
32+
}
33+
public int bfs(int start, List<List<Integer>> graph, int n) {
34+
boolean[] visited = new boolean[n+1];
35+
Queue<Integer> queue = new LinkedList<>();
36+
queue.offer(start);
37+
visited[start] = true;
38+
39+
int count = 0;
40+
while (!queue.isEmpty()) {
41+
int curr = queue.poll();
42+
for (int next: graph.get(curr)) {
43+
if (!visited[next]) {
44+
visited[next] = true;
45+
queue.offer(next);
46+
count++;
47+
}
48+
}
49+
50+
51+
52+
}
53+
return count;
54+
55+
}
56+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public int solution(int n, int[][] results) {
3+
boolean[][] graph = new boolean[n+1][n+1];
4+
5+
for (int[] result: results) {
6+
int win = result[0];
7+
int lose = result[1];
8+
9+
graph[win][lose] = true;
10+
}
11+
12+
for (int k=1; k<=n; k++) { // 경유 선수
13+
for (int i=1; i<=n; i++) { // 출발 선수
14+
for (int j=1; j<=n; j++) { // 도착 선수
15+
if (graph[i][k] && graph[k][j]) {
16+
graph[i][j] = true;
17+
}
18+
}
19+
}
20+
}
21+
22+
int answer = 0;
23+
for (int i=1; i<=n; i++) {
24+
int count = 0;
25+
for (int j=1; j<=n; j++) {
26+
if (i == j) continue;
27+
if (graph[i][j] || graph[j][i]) {
28+
count++;
29+
}
30+
}
31+
if (count == n-1) {
32+
answer++;
33+
}
34+
}
35+
36+
return answer;
37+
}
38+
}

0 commit comments

Comments
 (0)