Skip to content

Commit d6f4c91

Browse files
committed
[Simulation] baekjoon-19236
1 parent a95de64 commit d6f4c91

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

β€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
| 31 | | [SWEA-2115 λ²ŒκΏ€μ±„μ·¨](./src/Simulation/swea2115) | |
7171
| 32 | | [Baekjoon-15686 μΉ˜ν‚¨ 배달](./src/Simulation/P15686) | |
7272
| 33 | | [Baekjoon-21608 상어 μ΄ˆλ“±ν•™κ΅](./src/Simulation/P21608) | |
73+
| 34 | | [Baekjoon-19236 μ²­μ†Œλ…„ 상어](./src/Simulation/P19236) | |
7374

7475
## String
7576

β€Žsrc/Simulation/P19236/Main.java

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package Simulation.P19236;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
final static int[] di = {0, -1, -1, 0, 1, 1, 1, 0, -1};
9+
final static int[] dj = {0, 0, -1, -1, -1, 0, 1, 1, 1};
10+
11+
static int ans;
12+
static int[][] map;
13+
static Pos[] fishes;
14+
static boolean[] shark;
15+
16+
public static void main(String[] args) throws Exception {
17+
System.setIn(new FileInputStream("src/Simulation/P19236/input.txt"));
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
20+
map = new int[4][4];
21+
fishes = new Pos[17];
22+
shark = new boolean[17];
23+
24+
for (int i = 0; i < 4; i++) {
25+
StringTokenizer st = new StringTokenizer(br.readLine());
26+
for (int j = 0; j < 4; j++) {
27+
int v = Integer.parseInt(st.nextToken());
28+
int d = Integer.parseInt(st.nextToken());
29+
map[i][j] = v;
30+
fishes[v] = new Pos(i, j, d);
31+
}
32+
}
33+
34+
int start = map[0][0];
35+
shark[start] = true;
36+
moveFish(0, 0);
37+
moveShark(0, 0, fishes[start].d, start);
38+
39+
System.out.println(ans);
40+
}
41+
42+
static void moveShark(int i, int j, int d, int sum) {
43+
44+
int tmp = map[i][j];
45+
map[i][j] = 0;
46+
for (int k = 1; k < 4; k++) {
47+
int to_i = i + di[d]*k;
48+
int to_j = j + dj[d]*k;
49+
50+
if (!isValidPath(to_i, to_j) || map[to_i][to_j] == 0) continue;
51+
52+
Pos[] copyFish = new Pos[17];
53+
System.arraycopy(fishes, 1, copyFish, 1, 16);
54+
55+
shark[map[to_i][to_j]] = true;
56+
moveFish(to_i, to_j);
57+
moveShark(to_i, to_j, fishes[map[to_i][to_j]].d, sum+map[to_i][to_j]);
58+
59+
shark[map[to_i][to_j]] = false;
60+
System.arraycopy(copyFish, 1, fishes, 1, 16);
61+
map = new int[4][4];
62+
for (int f = 1; f <= 16; f++) {
63+
if (shark[f]) continue;
64+
Pos p = fishes[f];
65+
map[p.i][p.j] = f;
66+
}
67+
}
68+
map[i][j] = tmp;
69+
70+
ans = Math.max(ans, sum);
71+
}
72+
73+
static void moveFish(int sh_i, int sh_j) {
74+
for (int i = 1; i <= 16; i++) {
75+
if (shark[i]) continue;
76+
Pos fish = fishes[i];
77+
for (int k = fish.d; k < fish.d+8; k++) {
78+
int to_d = k > 8 ? k-8 : k;
79+
int to_i = fish.i + di[to_d];
80+
int to_j = fish.j + dj[to_d];
81+
if (!isValidPath(to_i, to_j) || (to_i == sh_i && to_j == sh_j)) continue;
82+
83+
if (map[to_i][to_j] == 0) {
84+
fishes[i] = new Pos(to_i, to_j, to_d);
85+
} else {
86+
Pos tmp = fishes[map[to_i][to_j]];
87+
fishes[i] = new Pos(tmp.i, tmp.j, to_d);
88+
fishes[map[to_i][to_j]] = new Pos(fish.i, fish.j, tmp.d);
89+
}
90+
int tmpVal = map[fish.i][fish.j];
91+
map[fish.i][fish.j] = map[to_i][to_j];
92+
map[to_i][to_j] = tmpVal;
93+
break;
94+
}
95+
}
96+
}
97+
98+
static boolean isValidPath(int i, int j) {
99+
return 0 <= i && i < 4 && 0 <= j && j < 4;
100+
}
101+
102+
static class Pos {
103+
int i, j, d;
104+
105+
public Pos(int i, int j, int d) {
106+
this.i = i;
107+
this.j = j;
108+
this.d = d;
109+
}
110+
}
111+
}

β€Žsrc/Simulation/P19236/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## [baekjoon-19236] μ²­μ†Œλ…„ 상어
2+
3+
![image](https://user-images.githubusercontent.com/22045163/116815435-82a28600-ab98-11eb-92c7-8049676c3670.png)
4+
![image](https://user-images.githubusercontent.com/22045163/116815464-a6fe6280-ab98-11eb-9ee3-453c3dd290a3.png)
5+
6+
![image](https://user-images.githubusercontent.com/22045163/116815479-b1b8f780-ab98-11eb-8ff3-cc02f868ab95.png)

β€Žsrc/Simulation/P19236/input.txt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2 6 10 8 6 7 9 4
2+
1 7 16 6 4 2 5 8
3+
3 7 8 6 7 6 14 8
4+
12 7 15 4 11 3 13 3
5+
6+
12 6 14 5 4 5 6 7
7+
15 1 11 3 3 7 7 5
8+
10 3 8 2 16 6 1 1
9+
5 8 2 7 13 6 9 4
10+
11+
λ‹΅ 55 λ˜λŠ” 76
12+
13+
7 6 2 1 15 1 9 1
14+
3 1 1 1 14 7 10 3
15+
6 1 13 6 4 3 11 4
16+
16 3 8 7 5 2 12 2
17+
18+
λ‹΅ 42
19+
20+
7 6 2 6 15 7 9 3
21+
3 5 1 4 14 1 10 6
22+
6 4 13 3 4 6 11 1
23+
16 5 8 7 5 2 12 2
24+
25+
λ‹΅ 88
26+
27+
7 1 2 6 15 7 9 3
28+
3 5 1 4 14 1 10 6
29+
6 4 13 3 4 6 11 1
30+
16 5 8 7 5 2 12 2
31+
32+
λ‹΅7
33+
34+
35+
(0, 0)μ—μ„œ (1, 1)둜 μ΄λ™ν–ˆμ„ λ•Œ
36+
[6, 9, 10, 14]
37+
[16, 0, 0, 1]
38+
[5, 2, 13, 8]
39+
[3, 4, 11, 15]
40+
41+
3 +3
42+
4 +4
43+
6 +2
44+
8 +2
45+
11 +3
46+
14 +2
47+
15 +1

0 commit comments

Comments
Β (0)