Skip to content

Commit 3fe8de1

Browse files
authored
[백준 19236] 청소년 상어 - 시뮬레이션
1 parent 07bd7bb commit 3fe8de1

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

hoseok/week35/Boj19236.java

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class Main {
5+
6+
static class Fish {
7+
int r, c, dir;
8+
boolean isAlive;
9+
10+
Fish(int r, int c, int dir, boolean isAlive) {
11+
this.r = r;
12+
this.c = c;
13+
this.dir = dir;
14+
this.isAlive = isAlive;
15+
}
16+
}
17+
18+
static class Shark {
19+
int r, c, dir, sum;
20+
21+
Shark(int r, int c, int dir, int sum) {
22+
this.r = r;
23+
this.c = c;
24+
this.dir = dir;
25+
this.sum = sum;
26+
}
27+
}
28+
29+
private static final int[] rows = {-1, -1, 0, 1, 1, 1, 0, -1};
30+
private static final int[] cols = {0, -1, -1, -1, 0, 1, 1, 1};
31+
private static int max;
32+
33+
public static void main(String[] args) throws Exception {
34+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
35+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
36+
int[][] map = new int[4][4];
37+
Fish[] fishes = new Fish[17];
38+
39+
for (int i = 0; i < 4; i++) {
40+
StringTokenizer st = new StringTokenizer(br.readLine());
41+
for (int j = 0; j < 4; j++) {
42+
int fishNumber = Integer.parseInt(st.nextToken());
43+
int dir = Integer.parseInt(st.nextToken());
44+
map[i][j] = fishNumber;
45+
fishes[fishNumber] = new Fish(i, j, dir - 1, true);
46+
}
47+
}
48+
49+
int fishNumber = map[0][0];
50+
map[0][0] = 0;
51+
fishes[fishNumber].isAlive = false;
52+
startMove(map, fishes, new Shark(0, 0, fishes[fishNumber].dir, fishNumber));
53+
54+
bw.write(max + "");
55+
bw.flush();
56+
bw.close();
57+
}
58+
59+
public static void startMove(int[][] map, Fish[] fishes, Shark shark) {
60+
int[][] copyMap = copyMap(map);
61+
Fish[] copyFishes = copyFishes(fishes);
62+
max = Math.max(max, shark.sum);
63+
64+
moveFish(copyMap, copyFishes, shark);
65+
66+
for (int i = 1; i < 4; i++) {
67+
int nextR = shark.r + rows[shark.dir] * i;
68+
int nextC = shark.c + cols[shark.dir] * i;
69+
70+
if (nextR < 0 || nextC < 0 || nextR >= 4 || nextC >= 4) {
71+
break;
72+
}
73+
if (copyMap[nextR][nextC] != 0) {
74+
int fishNumber = copyMap[nextR][nextC];
75+
copyMap[nextR][nextC] = 0;
76+
copyFishes[fishNumber].isAlive = false;
77+
startMove(copyMap, copyFishes, new Shark(nextR, nextC, copyFishes[fishNumber].dir, shark.sum + fishNumber));
78+
79+
copyMap[nextR][nextC] = fishNumber;
80+
copyFishes[fishNumber].isAlive = true;
81+
}
82+
}
83+
}
84+
85+
public static void moveFish(int[][] map, Fish[] fishes, Shark shark) {
86+
for (int i = 1; i <= 16; i++) {
87+
Fish fish = fishes[i];
88+
89+
// 이미 죽은 물고기나, 상어의 위치라면 불가능
90+
if (!fish.isAlive || fish.r == shark.r && fish.c == shark.c) {
91+
continue;
92+
}
93+
for (int j = 0; j < 8; j++) {
94+
int nextDir = (fish.dir + j) % 8;
95+
int nextR = fish.r + rows[nextDir];
96+
int nextC = fish.c + cols[nextDir];
97+
98+
if (nextR < 0 || nextR >= 4 || nextC < 0 || nextC >= 4) {
99+
continue;
100+
}
101+
if (nextR == shark.r && nextC == shark.c) {
102+
continue;
103+
}
104+
// 빈칸이라면
105+
fish.dir = nextDir;
106+
if (map[nextR][nextC] == 0) {
107+
map[nextR][nextC] = map[fish.r][fish.c];
108+
map[fish.r][fish.c] = 0;
109+
fish.r = nextR;
110+
fish.c = nextC;
111+
} else {
112+
// 물고기 자리 스왑
113+
Fish swapFish = fishes[map[nextR][nextC]];
114+
swapFish(fish, swapFish);
115+
int tempNumber = map[fish.r][fish.c];
116+
map[fish.r][fish.c] = map[swapFish.r][swapFish.c];
117+
map[swapFish.r][swapFish.c] = tempNumber;
118+
}
119+
break;
120+
}
121+
}
122+
}
123+
124+
public static void swapFish(Fish from, Fish to) {
125+
int tempR = from.r;
126+
int tempC = from.c;
127+
from.r = to.r;
128+
from.c = to.c;
129+
to.r = tempR;
130+
to.c = tempC;
131+
}
132+
133+
public static Fish[] copyFishes(Fish[] fishes) {
134+
Fish[] copyFishes = new Fish[17];
135+
for (int i = 1; i <= 16; i++) {
136+
copyFishes[i] = new Fish(fishes[i].r, fishes[i].c, fishes[i].dir, fishes[i].isAlive);
137+
}
138+
return copyFishes;
139+
}
140+
141+
public static int[][] copyMap(int[][] map) {
142+
int[][] copyMap = new int[4][4];
143+
for (int i = 0; i < 4; i++) {
144+
for (int j = 0; j < 4; j++) {
145+
copyMap[i][j] = map[i][j];
146+
}
147+
}
148+
return copyMap;
149+
}
150+
}

0 commit comments

Comments
 (0)