์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- upperbound
- PriorityQueue
- ์๋ฃ๊ตฌ์กฐ
- LowerBound
- dp
- ํฌํฌ์ธํฐ
- Django
- ์๋ฎฌ๋ ์ด์
- ๋ฐฑํธ๋ํน
- ์นด์นด์ค
- ๋ถ๋ถ์งํฉ
- ๋นํธ๋ง์คํน
- ์ฌ๊ท
- ํธ๋ผ์ด
- ์ฐ์ ์์ํ
- ๊ทธ๋ฆฌ๋
- ํ์๊ฐ์
- ๊ตฌํ
- ์ด๋ถํ์
- ํ๋ก์ด๋์์
- ์ธ๊ทธ๋จผํธํธ๋ฆฌ
- Floyd
- ์ด์งํธ๋ฆฌ
- BFS
- Union Find
- Dijkstra
- dfs
- ๋ค์ต์คํธ๋ผ
- ์์ด
- ์กฐํฉ
- Today
- Total
J
[JAVA] BOJ ๋ฐฑ์ค 9205 ๋งฅ์ฃผ ๋ง์๋ฉด์ ๊ฑธ์ด๊ฐ๊ธฐ ๋ณธ๋ฌธ
[JAVA] BOJ ๋ฐฑ์ค 9205 ๋งฅ์ฃผ ๋ง์๋ฉด์ ๊ฑธ์ด๊ฐ๊ธฐ
snowball๐ฐ 2023. 4. 4. 09:38https://www.acmicpc.net/problem/9205
9205๋ฒ: ๋งฅ์ฃผ ๋ง์๋ฉด์ ๊ฑธ์ด๊ฐ๊ธฐ
์ก๋์ ์ฌ๋ ์๊ทผ์ด์ ์น๊ตฌ๋ค์ ์ก๋์์ ์ด๋ฆฌ๋ ํํํฌํธ ๋ฝ ํ์คํฐ๋ฒ์ ๊ฐ๋ ค๊ณ ํ๋ค. ์ฌํด๋ ๋งฅ์ฃผ๋ฅผ ๋ง์๋ฉด์ ๊ฑธ์ด๊ฐ๊ธฐ๋ก ํ๋ค. ์ถ๋ฐ์ ์๊ทผ์ด๋ค ์ง์์ ํ๊ณ , ๋งฅ์ฃผ ํ ๋ฐ์ค๋ฅผ ๋ค๊ณ ์ถ๋ฐํ๋ค.
www.acmicpc.net
๐ก DFS, BFS, ๋ค์ต์คํธ๋ผ, ํ๋ก์ด๋ ์์ ์๊ณ ๋ฆฌ์ฆ ๋ชจ๋๋ก ํ ์ ์๋ ๋ฌธ์ ์ด๋ค.
๋๋ BFS๋ฅผ ์ ์ธํ ์ธ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ๋ฌธ์ ๋ฅผ ํ์๋ค.
๐ Floyd Warshall ์๊ณ ๋ฆฌ์ฆ ์ด์ฉ
1. ์ด์ฐจ์ ๋ฐฐ์ด places์ ์ง, ํธ์์ , ํ์คํฐ๋ฒ์ ์์น๋ฅผ ์ ์ฅํ๋ค.
2. ์ด์ฐจ์ ๋ฐฐ์ด D์ ๊ฒฝ์ ์ง ์์ด ๊ฑธ์ด์ผํ๋ ์ต๋ ๊ฑฐ๋ฆฌ๋ฅผ ์ ์ฅํ๋ค. (๊ฒฝ์ ์ง = ํธ์์ )
3. beer()์์ Floyd ์๊ณ ๋ฆฌ์ฆ์ผ๋ก D์ ๊ฐ์ ๊ฐฑ์ ํ๋ค.
4. ์ด๋ ์ง๋ถํฐ ํ์คํฐ๋ฒ ์ฌ์ด์ D๊ฐ์ด 1000์ ๋๋๋ค๋ฉด ๋งฅ์ฃผ๊ฐ ๋ถ์กฑํ๊ธฐ ๋๋ฌธ์ false๋ฅผ ์๋๋ฉด true๋ฅผ ๋ฐํํ๋ค.
import java.io.*;
import java.util.*;
public class BOJ_9205_floyd {
static int[][] places, D;
static int n;
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bf.readLine());
for(int t=1; t<=T; t++) {
n = Integer.parseInt(bf.readLine());
places = new int[n+2][2];
D = new int[n+2][n+2]; // ๋ง์ง๋ง์ผ๋ก ๋งฅ์ฃผ๋ฅผ ๊ตฌ๋งคํ ๊ณณ์ผ๋ก๋ถํฐ ํ์คํฐ๋ฒ๊น์ง์ ๊ฑฐ๋ฆฌ
for(int i=0; i<n+2; i++) {
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
places[i][0] = Integer.parseInt(st.nextToken());
places[i][1] = Integer.parseInt(st.nextToken());
}
for(int i=0; i<n+2; i++) {
for(int j=i+1; j<n+2; j++) {
D[i][j] = Math.abs(places[i][0]-places[j][0])+Math.abs(places[i][1]-places[j][1]);
D[j][i] = D[i][j];
}
}
System.out.println(beer() ? "happy" : "sad");
}
}
static boolean beer() {
for(int k=1; k<=n; k++) {
for(int i=0; i<n+1; i++) {
for(int j=1; j<n+2; j++) {
if(k == i || k == j || i == j) continue;
if(D[i][k] <= 1000 && D[k][j] < D[i][j])
D[i][j] = D[k][j];
}
}
}
if(D[0][n+1] <= 1000) return true;
return false;
}
}
beer()์ 3์ค for๋ฌธ์ ๋ณด๋ฉด k, i, j์ค ๊ฐ์ ๊ฐ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ D๋ฅผ ๊ฐฑ์ ํ์ง ์์๋ ํญ์ ์ต์๊ฐ์ธ 0์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.
๊ฒฝ์ ์ง๋ฅผ ์ค๊ฐ์ ๋ง๋๊ฒ ๋๋ฉด 1. (์ถ๋ฐ์ง์์ ๊ฒฝ์ ์ง๊น์ง ๊ฑฐ๋ฆฌ๊ฐ 1000์ดํ)์ด๊ณ 2. (๊ฒฝ์ ์ง๋ถํฐ ๋์ฐฉ์ง๊น์ง ๊ฑฐ๋ฆฌ)๊ฐ < (์ถ๋ฐ์ง๋ถํฐ ๋์ฐฉ์ง๊น์ง ๊ฑฐ๋ฆฌ)๋ณด๋ค ์๋ค๋ฉด ๊ฐ์ ๊ฐฑ์ ํ๋ค.
๐ Dijkstra ์๊ณ ๋ฆฌ์ฆ ์ด์ฉ
1. ๋ง์ฐฌ๊ฐ์ง๋ก places๋ฐฐ์ด์ ์์น ๊ฐ์ ์ ์ฅํ๋ค.
2. ์ด๋ฒ์ ์ผ์ฐจ์ ๋ฐฐ์ด D์ ์ถ๋ฐ์ง๋ถํฐ ๊ฐ ์์น๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ์ ์ฅํ๋ค.
(์ด๋, ๊ฒฝ์ ์ง๋ฅผ ๊ฑฐ์ณ๊ฐ๋ ๊ฒฝ์ฐ ๊ฐ ๊ฑฐ๋ฆฌ๊ฐ 1000์ดํ์ผ๋๋ง ๊ฐ์ ์์ ํด์ค๋ค. ์ ๊ฑฐ์ณ๊ฐ๋ ๊ฒฝ์ฐ๋ ๋ง์ฐฌ๊ฐ์ง)
3. D[ํ์คํฐ๋ฒ]์ ๊ฐ์ด ์ด๊ธฐ๊ฐ์ผ๋ก๋ถํฐ ๊ฐฑ์ ๋์ง ์์๋ค๋ฉด 1000์ดํ์ ๊ฒฝ์ ์ง๋ฅผ ๊ฑฐ์ณ ๋์ฐฉํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค๋ ๋ป์ด๋ฏ๋ก "sad"๋ฐํ
import java.io.*;
import java.util.*;
public class BOJ_9205_Dijkstra {
static int[][] places;
static int[] D;
static int n;
static class Node{
int to;
int weak;
Node link;
public Node(int to, int weak, Node link) {
super();
this.to = to;
this.weak = weak;
this.link = link;
}
}
static Node[] nodes;
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bf.readLine());
for(int t=1; t<=T; t++) {
n = Integer.parseInt(bf.readLine());
places = new int[n+2][2];
for(int i=0; i<n+2; i++) {
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
places[i][0] = Integer.parseInt(st.nextToken());
places[i][1] = Integer.parseInt(st.nextToken());
}
nodes = new Node[n+2];
for(int i=0; i<n+2; i++) {
for(int j=i+1; j<n+2; j++) {
if(i == j) continue;
int weak = Math.abs(places[i][0]-places[j][0])+Math.abs(places[i][1]-places[j][1]);
if(weak <= 1000) {
nodes[i] = new Node(j, weak, nodes[i]);
nodes[j] = new Node(i, weak, nodes[j]);
}
}
}
D = new int[n+2];
Arrays.fill(D, 987654321);
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[1], o2[1]));
D[0] = 0;
pq.offer(new int[] {0, 0});
while(!pq.isEmpty()) {
int[] now = pq.poll();
if(now[1] > D[now[0]]) continue;
for(Node n = nodes[now[0]]; n!= null; n = n.link) {
if(D[n.to] > now[1] + n.weak) {
D[n.to] = now[1] + n.weak;
pq.offer(new int[] {n.to, D[n.to]});
}
}
}
System.out.println(D[n+1] == 987654321 ? "sad" : "happy");
}
}
}
์์ธํ ๋ณด์
D = new int[n+2];
Arrays.fill(D, 987654321);
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[1], o2[1]));
D[0] = 0;
pq.offer(new int[] {0, 0});
while(!pq.isEmpty()) {
int[] now = pq.poll();
if(now[1] > D[now[0]]) continue;
for(Node n = nodes[now[0]]; n!= null; n = n.link) {
if(D[n.to] > now[1] + n.weak) {
D[n.to] = now[1] + n.weak;
pq.offer(new int[] {n.to, D[n.to]});
}
}
}
System.out.println(D[n+1] == 987654321 ? "sad" : "happy");
- D ๋ฐฐ์ด ์ ์ธ
- D์ ์ด๊ธฐ๊ฐ์ ์ค๋ค.
- ์ฐ์ ์์ ํ์ ์ถ๋ฐ์ง๋ก๋ถํฐ ๊ฑฐ๋ฆฌ๊ฐ ์งง์ ์์๋๋ก ์ ๋ ฌํ ์ ์๊ฒ ํ๋ค.
- ์ด๊ธฐ๊ฐ์ ์ถ๋ฐ์ง๋ก ์ค์ ํ๋ค. ์ด๋ ์ถ๋ฐ์ง๋ก๋ถํฐ ์ถ๋ฐ์ง๊น์ง ๊ฑฐ๋ฆฌ๋ 0์ด๋ค.
- now[1] = ์ถ๋ฐ์ง๋ถํฐ now[0]๊น์ง ๊ฑฐ๋ฆฌ
- D[ํ์ฌ ์์น] < now[1]์ธ ๊ฒฝ์ฐ now[0]์ ์ต๋จ๊ฑฐ๋ฆฌ๊ฐ ์๋กญ๊ฒ ๊ฐฑ์ ๋์๊ธฐ ๋๋ฌธ์ ์ด ๊ฐ์ผ๋ก ๋ค๋ฅธ ๊ฐ๋ค์ ๊ฐฑ์ ํ์ง ์์๋ ๋๋ค.
- ๊ทธ๊ฒ์ด ์๋๋ผ๋ฉด ํ์ฌ ์์น๋ก๋ถํฐ 1000์ดํ๋ก ๊ฐ ์ ์๋ ๊ฐ๋ค์ ๊ฐฑ์ ํ๋ค.
๐ DFS ์ด์ฉ
1. places์ D์ ์ด๊ธฐ๊ฐ์ Floyd์ ๋์ผํ๋ค.
2. visited๋ฅผ ์ด์ฐจ์ ๋ฐฐ์ด๋ก ์ ์ธํ์ฌ ์ถ๋ฐ์ง๋ก๋ถํฐ ๋์ฐฉ์ง(๊ฒฝ์ ์ง)๊น์ง์ ์กฐ์ฌ๊ฐ ์๋ฃ๋์๋์ง ํ์ธํ๊ฒ ํ๋ค.
3. dfs(x)๋ x๋ฅผ ์ถ๋ฐ์ง๋ก ํ ๋ n+1(๋์ฐฉ์ง)๊น์ง ๋งฅ์ฃผ๋ฅผ ๋ง์๋ฉฐ ๋์ฐฉํ ์ ์๋์ง๋ฅผ ๋ฐํํ๋ค.
4. x๊ฐ n+1์ ๋์ฐฉํ๋ค๋ฉด ๊ฐ๋ฅํ๋ค๋ ์๋ฏธ์ด๋ฏ๋ก true๋ฅผ ๋ฐํ
5. for๋ฌธ์ ํตํด 1๋ถํฐ n+1๊น์ง ์กฐ์ฌํ๋ฉด์ ์กฐ์ฌํ์ง ์์ ๊ธธ์ด๊ณ ๊ฑฐ๋ฆฌ๊ฐ 1000์ดํ์ด๋ฉด ๊ฐ ์ ์๊ธฐ ๋๋ฌธ์ dfs๋ฅผ ์ฌ๊ทํธ์ถํ๋ค.
import java.io.*;
import java.util.*;
public class BOJ_9205_dfs{
static int[][] places, D;
static int n;
static boolean[][] v;
static boolean flag;
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bf.readLine());
for(int t=1; t<=T; t++) {
n = Integer.parseInt(bf.readLine());
places = new int[n+2][2];
D = new int[n+2][n+2];
for(int i=0; i<n+2; i++) {
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
places[i][0] = Integer.parseInt(st.nextToken());
places[i][1] = Integer.parseInt(st.nextToken());
}
for(int i=0; i<n+2; i++) {
for(int j=i+1; j<n+2; j++) {
D[i][j] = Math.abs(places[i][0]-places[j][0])+Math.abs(places[i][1]-places[j][1]);
D[j][i] = D[i][j];
}
}
v = new boolean[n+2][n+2];
flag = false;
System.out.println(dfs(0) ? "happy" : "sad");
}
}
static boolean dfs(int x) {
if(flag) return true;
if(x == n+1) {
flag = true;
return true;
}
boolean flag = false;
for(int i=n+1; i>=1; i--) {
if(i == x || v[x][i] || D[x][i] > 1000) continue;
v[x][i] = true;
flag |= dfs(i);
}
return flag;
}
}
'๐ Problem Solving > ๐ BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] BOJ ๋ฐฑ์ค 5052 ์ ํ๋ฒํธ ๋ชฉ๋ก (0) | 2023.05.10 |
---|---|
[JAVA] BOJ ๋ฐฑ์ค 1941 ์๋ฌธ๋ ์น ๊ณต์ฃผ (0) | 2023.04.12 |
[JAVA] BOJ ๋ฐฑ์ค 1194 ๋ฌ์ด ์ฐจ์ค๋ฅธ๋ค, ๊ฐ์ (0) | 2023.03.31 |
[JAVA] BOJ ๋ฐฑ์ค 16724 ํผ๋ฆฌ ๋ถ๋ ์ฌ๋์ด (0) | 2023.03.29 |
[JAVA] BOJ ๋ฐฑ์ค 4811 ์์ฝ (0) | 2023.03.28 |