Skip to content

Commit 39db8c4

Browse files
committed
[BOJ] 9205 맥주 마시면서 걸어가기_241011
1 parent a4c2662 commit 39db8c4

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

BOJ/5001-10000번/YJ_9205.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package BOJ;
2+
3+
import java.io.*;
4+
import java.util.ArrayDeque;
5+
import java.util.Deque;
6+
7+
/**
8+
* 알고리즘: BFS
9+
* 시간복잡도:
10+
* 테스트 케이스 t ≤ 50 , 편의점 갯수 0 ≤ n ≤ 100
11+
* 모든 지점 간의 거리 계산 시 N * (N-1) = O(N^2) > 50 * (100^2) = 50 * 10000 = 500,000 > 10^5 으로 BFS 풀이가능
12+
* */
13+
public class YJ_9205 {
14+
static class Pos {
15+
int x;
16+
int y;
17+
Pos(int x, int y) {
18+
this.x = x;
19+
this.y = y;
20+
}
21+
}
22+
23+
static Deque<Pos> DEQUE;
24+
static int[][] CONVINI;
25+
public static void main(String[] args) throws IOException {
26+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
27+
int t = Integer.parseInt(br.readLine());
28+
29+
for(int i=0; i<t; i++){
30+
int conviniCount = Integer.parseInt(br.readLine());
31+
32+
//집
33+
String[] home = br.readLine().split("\\s");
34+
DEQUE = new ArrayDeque<>();
35+
DEQUE.offer(new Pos(Integer.parseInt(home[0]), Integer.parseInt(home[1])));
36+
//편의점
37+
CONVINI = new int[conviniCount][2];
38+
for(int j=0; j<conviniCount; j++){
39+
String[] c = br.readLine().split("\\s");
40+
CONVINI[j][0] = Integer.parseInt(c[0]);
41+
CONVINI[j][1] = Integer.parseInt(c[1]);
42+
}
43+
//페스티벌
44+
String[] f = br.readLine().split("\\s");
45+
Pos festival = new Pos(Integer.parseInt(f[0]), Integer.parseInt(f[1]));
46+
47+
bfs(conviniCount, festival);
48+
}
49+
br.close();
50+
}
51+
52+
static void bfs(int conviniCount, Pos festival){
53+
final int STAMINA = 1000;
54+
boolean[] visited = new boolean[conviniCount];
55+
56+
while(!DEQUE.isEmpty()){
57+
Pos current = DEQUE.poll();
58+
if(calculateDistance(current,festival) <= STAMINA){
59+
System.out.println("happy");
60+
return;
61+
}
62+
63+
for(int i=0; i<conviniCount; i++){
64+
if(visited[i]){
65+
continue;
66+
}
67+
Pos nextConvini = new Pos(CONVINI[i][0],CONVINI[i][1]);
68+
if(calculateDistance(current,nextConvini) <= STAMINA){
69+
DEQUE.offer(nextConvini);
70+
visited[i] = true;
71+
}
72+
}
73+
}
74+
System.out.println("sad");
75+
}
76+
77+
private static int calculateDistance(Pos current, Pos next){
78+
return Math.abs(current.x-next.x) + Math.abs(current.y-next.y);
79+
}
80+
81+
}

0 commit comments

Comments
 (0)