Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e3df09

Browse files
committed
고다혜: [BOJ] 9205 맥주 마시면서 걸어가기_241011
1 parent ac57077 commit 0e3df09

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

‎BOJ/5001-10000번/DH_9205.java‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 맥주 마시면서 걸어가기
6+
*/
7+
8+
public class DH_9205 {
9+
static class Point {
10+
int r, c;
11+
public Point(int r, int c) {
12+
this.r = r;
13+
this.c = c;
14+
}
15+
}
16+
static class Node implements Comparable<Node> {
17+
int e, w;
18+
public Node(int e, int w) {
19+
this.e = e;
20+
this.w = w;
21+
}
22+
23+
@Override
24+
public int compareTo(Node o) {
25+
return Integer.compare(this.w, o.w);
26+
}
27+
}
28+
static StringBuilder sb = new StringBuilder();
29+
static ArrayList<Node> adj[];
30+
static Point[] p;
31+
32+
public static void main(String[] args) throws Exception {
33+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
34+
StringTokenizer st;
35+
36+
int tc = Integer.parseInt(br.readLine());
37+
38+
for(int t = 0; t < tc; t++) {
39+
int n = Integer.parseInt(br.readLine()); // 맥주를 파는 편의점의 개수
40+
41+
// 시작할 떄 맥주 한 박스 들고 시작 - 200 * 50 미터 갈 수 있음
42+
p = new Point[n + 2];
43+
adj = new ArrayList[n + 2];
44+
45+
// 위치들 입력 받기
46+
// 0: 집이 있는 곳, n + 1: 락 페스티벌 좌표
47+
for(int i = 0; i < p.length; i++) {
48+
st = new StringTokenizer(br.readLine());
49+
int r = Integer.parseInt(st.nextToken());
50+
int c = Integer.parseInt(st.nextToken());
51+
52+
p[i] = new Point(r, c);
53+
}
54+
55+
// 양방향 인접리스트로 표현하기
56+
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();
57+
for(int i = 0; i < p.length - 1; i++) {
58+
for(int j = i + 1; j < p.length; j++) {
59+
int dis = getDis(i, j);
60+
61+
// 해당 지점을 가기 전 50미터에서 맥주 한 병을 다 마셔야 됨
62+
// 두 지점간 거리가 1000을 넘으면 못감
63+
if(dis > 1000) continue;
64+
adj[i].add(new Node(j, dis));
65+
adj[j].add(new Node(i, dis));
66+
}
67+
}
68+
69+
// 다익스트라를 통해 '집의 시작점: 0' 에서부터 페스티벌 지점까지 갈 수 있는지 확인
70+
sb.append(dijkstra(0)? "happy": "sad").append("\n");
71+
}
72+
73+
System.out.println(sb);
74+
}
75+
76+
// 우선순위 큐를 사용한 다익스트라
77+
static boolean dijkstra(int s) {
78+
PriorityQueue<Node> q = new PriorityQueue<Node>();
79+
80+
q.add(new Node(s, 0));
81+
boolean[] v = new boolean[adj.length];
82+
int[] dis = new int[adj.length];
83+
84+
Arrays.fill(dis, Integer.MAX_VALUE);
85+
v[0] = true;
86+
dis[0] = 0;
87+
88+
while(!q.isEmpty()) {
89+
Node current = q.poll();
90+
v[current.e] = true;
91+
92+
if(current.e == adj.length - 1) return true;
93+
for(Node next: adj[current.e]) {
94+
if(v[next.e]) continue;
95+
96+
if(dis[next.e] > dis[current.e] + next.w) {
97+
dis[next.e] = dis[current.e] + next.w;
98+
99+
q.add(new Node(next.e, dis[next.e]));
100+
}
101+
}
102+
}
103+
104+
return false;
105+
}
106+
107+
static int getDis(int i, int j) {
108+
return Math.abs(p[i].r - p[j].r) + Math.abs(p[i].c - p[j].c);
109+
}
110+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /