Skip to content

Commit 61d79bb

Browse files
authored
Merge pull request #706 from AlgorithmWithGod/JHLEE325
[20250821] BOJ / G1 / 청소년 상어 / 이준희
2 parents 1692a6d + 4cdc3ce commit 61d79bb

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
static int[] dr = {-1, -1, 0, 1, 1, 1, 0, -1};
8+
static int[] dc = { 0, -1,-1,-1, 0, 1, 1, 1};
9+
10+
static class fish {
11+
int r, c, dir;
12+
boolean live;
13+
14+
public fish(int row, int col, int d) {
15+
this.r = row;
16+
this.c = col;
17+
this.dir = d;
18+
this.live = true;
19+
}
20+
}
21+
22+
static class shark {
23+
int r, c, eat, dir;
24+
25+
public shark(int row, int col, int e, int d) {
26+
this.r = row;
27+
this.c = col;
28+
this.eat = e;
29+
this.dir = d;
30+
}
31+
}
32+
33+
static int[][] map = new int[4][4];
34+
static fish[] list = new fish[17];
35+
static int answer = 0;
36+
37+
public static void main(String[] args) throws Exception {
38+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
39+
StringTokenizer st;
40+
41+
shark s = null;
42+
43+
for (int i = 0; i < 4; i++) {
44+
st = new StringTokenizer(br.readLine());
45+
for (int j = 0; j < 4; j++) {
46+
int n = Integer.parseInt(st.nextToken());
47+
int d = Integer.parseInt(st.nextToken()) - 1;
48+
map[i][j] = n;
49+
list[n] = new fish(i, j, d);
50+
}
51+
}
52+
53+
int first = map[0][0];
54+
fish f0 = list[first];
55+
f0.live = false;
56+
s = new shark(0, 0, first, f0.dir);
57+
map[0][0] = 0;
58+
59+
dfs(copymap(map), copylist(list), s);
60+
61+
System.out.println(answer);
62+
}
63+
64+
static void dfs(int[][] curmap, fish[] curlist, shark s) {
65+
answer = Math.max(answer, s.eat);
66+
67+
moveFish(curmap, curlist, s);
68+
69+
int sr = s.r, sc = s.c, dir = s.dir;
70+
for (int step = 1; step <= 3; step++) {
71+
int nr = sr + dr[dir] * step;
72+
int nc = sc + dc[dir] * step;
73+
74+
if (nr < 0 || nr >= 4 || nc < 0 || nc >= 4) break;
75+
if (curmap[nr][nc] == 0) continue;
76+
77+
int[][] nMap = copymap(curmap);
78+
fish[] nList = copylist(curlist);
79+
80+
int target = nMap[nr][nc];
81+
fish eaten = nList[target];
82+
eaten.live = false;
83+
84+
shark ns = new shark(nr, nc, s.eat + target, eaten.dir);
85+
86+
nMap[sr][sc] = 0;
87+
nMap[nr][nc] = 0;
88+
89+
dfs(nMap, nList, ns);
90+
}
91+
}
92+
93+
static void moveFish(int[][] curmap, fish[] curlist, shark s) {
94+
for (int i = 1; i <= 16; i++) {
95+
fish f = curlist[i];
96+
if (f == null || !f.live) continue;
97+
98+
for (int rotate = 0; rotate < 8; rotate++) {
99+
int nd = (f.dir + rotate) % 8;
100+
int nr = f.r + dr[nd];
101+
int nc = f.c + dc[nd];
102+
103+
if (nr < 0 || nr >= 4 || nc < 0 || nc >= 4) continue;
104+
if (nr == s.r && nc == s.c) continue;
105+
106+
if (curmap[nr][nc] == 0) {
107+
curmap[f.r][f.c] = 0;
108+
f.r = nr; f.c = nc; f.dir = nd;
109+
curmap[nr][nc] = i;
110+
} else {
111+
int otherNum = curmap[nr][nc];
112+
fish g = curlist[otherNum];
113+
114+
curmap[f.r][f.c] = otherNum;
115+
curmap[nr][nc] = i;
116+
117+
int tr = f.r, tc = f.c;
118+
f.r = nr; f.c = nc; f.dir = nd;
119+
g.r = tr; g.c = tc;
120+
}
121+
break;
122+
}
123+
}
124+
}
125+
126+
static int[][] copymap(int[][] src) {
127+
int[][] cpymap = new int[4][4];
128+
for (int i = 0; i < 4; i++) {
129+
System.arraycopy(src[i], 0, cpymap[i], 0, 4);
130+
}
131+
return cpymap;
132+
}
133+
134+
static fish[] copylist(fish[] src) {
135+
fish[] cpylist = new fish[17];
136+
for (int i = 1; i <= 16; i++) {
137+
if (src[i] == null) continue;
138+
fish f = src[i];
139+
fish nf = new fish(f.r, f.c, f.dir);
140+
nf.live = f.live;
141+
cpylist[i] = nf;
142+
}
143+
return cpylist;
144+
}
145+
}
146+
147+
```

0 commit comments

Comments
 (0)