-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindingHer.java
More file actions
66 lines (60 loc) · 2.42 KB
/
Copy pathFindingHer.java
File metadata and controls
66 lines (60 loc) · 2.42 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package algorithm.baek.graph;
import algorithm.TestCase;
import java.io.*;
import java.text.ParseException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
/**
* https://www.acmicpc.net/problem/16502
* 그녀를 찾아서
*/
public class FindingHer implements TestCase {
static double[][] possible;
static double[] location;
@Override
public void test() throws ParseException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int time = Integer.parseInt(br.readLine());
int edge = Integer.parseInt(br.readLine());
possible = new double[4][4];
location = new double[4];
for (int i = 0; i < edge; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int from = st.nextToken().charAt(0) - 'A';
int to = st.nextToken().charAt(0) - 'A';
double percent = Double.parseDouble(st.nextToken());
possible[from][to] = percent;
}
bfs(time);
for (int i = 0; i < location.length; i++) {
bw.write(String.format("%.2f\n", location[i] * 100));
}
bw.close();
br.close();
}
private void bfs(int time) {
Queue<Double[]> queue = new LinkedList<>();
queue.add(new Double[]{Double.valueOf(0), Double.valueOf(0.25)});
queue.add(new Double[]{Double.valueOf(1), Double.valueOf(0.25)});
queue.add(new Double[]{Double.valueOf(2), Double.valueOf(0.25)});
queue.add(new Double[]{Double.valueOf(3), Double.valueOf(0.25)});
while (time-- > 0) {
location = new double[4];
int size = queue.size();
for (int i = 0; i < size; i++) {
Double[] poll = queue.poll();
int idx = poll[0].intValue();
for (int j = 0; j < possible.length; j++) {
double curPossibility = possible[idx][j];
if (curPossibility != 0) {
Double accumPossibility = poll[1];
queue.add(new Double[]{(double) j, curPossibility * accumPossibility});
location[j] += curPossibility * accumPossibility;
}
}
}
}
}
}