Skip to content

Commit 3ca25e9

Browse files
committed
nsun9505 solved 경주로 건설
1 parent b3c5b07 commit 3ca25e9

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [2020 카카오 인턴십] 경주로 건설 - JAVA
2+
3+
## 분류
4+
> BFS
5+
6+
## 코드
7+
```java
8+
import java.util.LinkedList;
9+
import java.util.Queue;
10+
11+
public class Solution {
12+
public int solution(int[][] board) {
13+
int[] dx = {-1, 0, 1, 0};
14+
int[] dy = {0, 1, 0, -1};
15+
int answer = 0;
16+
int N = board.length;
17+
Queue<Element> queue = new LinkedList<>();
18+
boolean[][] visited = new boolean[N][N];
19+
int[][] moneys = new int[N][N];
20+
queue.offer(new Element(0, 0, 0, 1));
21+
queue.offer(new Element(0, 0, 0, 2));
22+
visited[0][0] = true;
23+
24+
while(!queue.isEmpty()){
25+
Element cur = queue.poll();
26+
27+
for(int dir=0; dir<4; dir++){
28+
int nx = cur.row + dx[dir];
29+
int ny = cur.col + dy[dir];
30+
31+
if(nx < 0 || ny < 0 || nx >= N || ny >= N)
32+
continue;
33+
34+
if(board[nx][ny] == 1)
35+
continue;
36+
37+
int money = 100;
38+
if(cur.dir != dir)
39+
money += 500;
40+
41+
if(visited[nx][ny]){
42+
if(cur.money + money <= moneys[nx][ny]) {
43+
moneys[nx][ny] = cur.money + money;
44+
queue.offer(new Element(nx, ny, cur.money + money, dir));
45+
}
46+
continue;
47+
}
48+
49+
moneys[nx][ny] = cur.money + money;
50+
visited[nx][ny] = true;
51+
queue.offer(new Element(nx, ny, moneys[nx][ny], dir));
52+
}
53+
}
54+
return moneys[N-1][N-1];
55+
}
56+
57+
static class Element{
58+
int row;
59+
int col;
60+
int money;
61+
int dir;
62+
63+
public Element(int row, int col, int money, int dir) {
64+
this.row = row;
65+
this.col = col;
66+
this.money = money;
67+
this.dir = dir;
68+
}
69+
}
70+
}
71+
```
72+
73+
## 문제 풀이
74+
BFS를 사용해서 문제를 풀었습니다!
75+
76+
출발하는 지점에서 방향이 왼쪽이거나 아래일 수 있으므로 두 가지 경우를 처음에 큐로 넣어줍니다.
77+
78+
그리고 BFS로 탐색을 하면서, 아직 방문하지 않았다면, 방문한 표시를 남기고, 해당 위치까지의 도로 건설비용을 저장합니다.
79+
80+
만약, 이미 방문한 위치라면, 이미 저장된 건설 비용과 지금까지의 건설 비용 + 다음 위치로의 건설 비용과 비교합니다.
81+
82+
그래서 현재 위치에 새로 건설하는 비용이 더 작다면 원래의 값을 갱신하고 큐에 넣어줍니다.
83+
84+
아니면 더 탐색하지 않기 위해 continue로 끝내주면 됩니다.
85+
86+
그리고 건설 비용 계산은 일단 무조건 100이 들어갑니다!
87+
88+
왜냐하면 방향이 같은 경우에는 100이 들어가고, 코너를 만들 때는 코너를 만드는 비용 + 다음 위치로 가는 직진 비용이 필요하므로
89+
90+
코너를 만들 때는 500을 더해줘서 건설 비용을 계산하면 됩니다!
91+
92+
마지막으로 건설비용을 저장한 배열의 N-1, N-1좌표의 값을 리턴하면 답이 됩니다.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class Solution {
5+
public int solution(int[][] board) {
6+
int[] dx = {-1, 0, 1, 0};
7+
int[] dy = {0, 1, 0, -1};
8+
int answer = 0;
9+
int N = board.length;
10+
Queue<Element> queue = new LinkedList<>();
11+
boolean[][] visited = new boolean[N][N];
12+
int[][] moneys = new int[N][N];
13+
queue.offer(new Element(0, 0, 0, 1));
14+
queue.offer(new Element(0, 0, 0, 2));
15+
visited[0][0] = true;
16+
17+
while(!queue.isEmpty()){
18+
Element cur = queue.poll();
19+
20+
for(int dir=0; dir<4; dir++){
21+
int nx = cur.row + dx[dir];
22+
int ny = cur.col + dy[dir];
23+
24+
if(nx < 0 || ny < 0 || nx >= N || ny >= N)
25+
continue;
26+
27+
if(board[nx][ny] == 1)
28+
continue;
29+
30+
int money = 100;
31+
if(cur.dir != dir)
32+
money += 500;
33+
34+
if(visited[nx][ny]){
35+
if(cur.money + money <= moneys[nx][ny]) {
36+
moneys[nx][ny] = cur.money + money;
37+
queue.offer(new Element(nx, ny, cur.money + money, dir));
38+
}
39+
continue;
40+
}
41+
42+
moneys[nx][ny] = cur.money + money;
43+
visited[nx][ny] = true;
44+
queue.offer(new Element(nx, ny, moneys[nx][ny], dir));
45+
}
46+
}
47+
return moneys[N-1][N-1];
48+
}
49+
50+
static class Element{
51+
int row;
52+
int col;
53+
int money;
54+
int dir;
55+
56+
public Element(int row, int col, int money, int dir) {
57+
this.row = row;
58+
this.col = col;
59+
this.money = money;
60+
this.dir = dir;
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)