https://www.acmicpc.net/problem/19236
๋ฌธ์ ์ค๋ช
- 4X4 ํฌ๊ธฐ์ ๊ณต๊ฐ์ด ์๊ณ , ํฌ๊ธฐ๊ฐ 1X1์ธ ์ ์ฌ๊ฐํ ์นธ์ผ๋ก ๋๋์ด์ ธ ์๋ค.
- ํ ์นธ์๋ ๋ฌผ๊ณ ๊ธฐ ํ ๋ง๋ฆฌ ์กด์ฌ.
- ๊ฐ ๋ฌผ๊ณ ๊ธฐ๋ ๋ฒํธ์ ๋ฐฉํฅ์ ๊ฐ์ง๊ณ ์๋ค.
- 1 <= ๋ฒํธ <= 16
- ๋ฐฉํฅ : $\uparrow, \nwarrow, \leftarrow, \swarrow, \downarrow, \searrow, \rightarrow, \nearrow$
์งํ ์์
- ์ฒญ์๋ ์์ด๊ฐ (0,0) ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน๊ณ , ๊ทธ ๋ฌผ๊ณ ๊ธฐ์ ๋ฐฉํฅ์ ๊ฐ์ง๊ณ ๊ณต๊ฐ์ ๋ค์ด๊ฐ
- ๋ฌผ๊ณ ๊ธฐ ์ด๋
1) ๋ฒํธ๊ฐ ์์ ์์ผ๋ก
2) ํ ์นธ ์ด๋ ๊ฐ๋ฅ, ๋น ์นธ๊ณผ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์๋ ์นธ์ผ๋ก ์ด๋ ๊ฐ๋ฅ1) ๋ฐฉํฅ์ ์ด๋ ๊ฐ๋ฅํ ์นธ์ด ์๋ค๋ฉด, 1-1) ๋น์นธ์ด๋ผ๋ฉด, ๊ทธ๋๋ก ์ด๋ 1-2) ๋ฌผ๊ณ ๊ธฐ๊ฐ ์๋ค๋ฉด, ๋ ๋ฌผ๊ณ ๊ธฐ ๊ต์ฒด 2) ์ด๋ ๊ฐ๋ฅํ ์นธ์ด ์๋ค๋ฉด 2-1) ๊ฐ๋ฅํ ์นธ์ด ๋์ฌ ๋๊น์ง ๋ฐ์๊ณ ๋ฐฉํฅ์ผ๋ก 45๋ ํ์ 2-2) ๊ทธ๋๋ ์๋ค๋ฉด, ํจ์ค
- ์ฒญ์๋
์์ด ์ด๋
1) ๋ฐฉํฅ์ ์๋ ์นธ์ผ๋ก ์ด๋ ๊ฐ๋ฅ, ํ ๋ฒ์ ์ฌ๋ฌ ์นธ ์ด๋ ๊ฐ๋ฅ
2) ๋ฌผ๊ณ ๊ธฐ๊ฐ ์๋ ์นธ์ผ๋ก๋ ์ด๋ ๋ถ๊ฐ๋ฅ
โ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์๋ ์นธ์ ์ง๋๊ฐ ์๋ ์์ โผ๏ธ
3) ์ด๋์ด ๋ ์ด์ ๋ถ๊ฐ๋ฅํ๋ฉด ์ข ๋ฃ - ์ฒญ์๋ ์์ด๊ฐ ๋จน์ ์ ์๋ ๋ฌผ๊ณ ๊ธฐ ๋ฒํธ์ ์ต๋๊ฐ ์ถ๋ ฅ โผ๏ธ
๋ฌธ์ ํ์ด
Fish Class
- ๋ฌผ๊ณ ๊ธฐ ์ ๋ณด
- ๋ฌผ๊ณ ๊ธฐ ๋ฒํธ, ์ขํ, ๋ฐฉํฅ, ์ด์์๋์ง ์ฌ๋ถ
1) ์์
(0,0) ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน๊ณ ์๋ฆฌ ์ฐจ์งsx = 0, sy = 0, sd = fishes[map[0][0]].dir, map[0][0] = -1
map[x][y] = -1;
์ ์์ด๋ฅผ ๋ํ๋
2) DFS
DFS(sx, sy, sd, eat)
์ฒญ์๋
์์ด๊ฐ ์ด๋ํ ์ ์๋ ๊ฒฝ์ฐ์ ์์์ ์ต๋๊ฐ์ ์ฐพ์์ผํ๋ฏ๋ก DFS ์ด์ฉ
์ต์ข
๊ฒฐ๊ณผ์ธ amountOfEating
๊ณผ eat
์ ๋น๊ตํด ์ต๋๊ฐ์ amountOfEating
์ผ๋ก ์ ์ฅ
โ
์งํํ๊ธฐ ์ด์ ์ ์ํ์์ ๋ค๋ฅธ ์ํฉ์ ํ์ธํด์ผํ๋ฏ๋ก, ๊ธฐ์กด์ ๋ฐฐ์ด์ ๋ฏธ๋ฆฌ ์์๋ก ์ ์ฅ
โ
Fish ๋ฐฐ์ด ๊ฒฝ์ฐ, Arrays.copyOf()
๋ฅผ ์ฌ์ฉํ๋๋, ๊ธฐ์กด์ ๊ฐ๋ค์ด ๋ณ๊ฒฝ ๋จ → for๋ฌธ์ ๋๋ฉด์ ์๋ก new Fish()
๋ก ์ถ๊ฐ
๋ฌผ๊ณ ๊ธฐ ์ด๋, ์์ด ์ด๋ ์ดํ, ์๋์ ๋ฐฐ์ด๋ค์ ์ ์ฅํด๋์ ์์์ ๋ฐฐ์ด๊ฐ์ผ๋ก ๋ค์ ๋ณ๊ฒฝํด์ค๋ค โผ๏ธ
2-1) ๋ฌผ๊ณ ๊ธฐ ์ด๋
moveFishes()
- 16๋ง๋ฆฌ ๋ฌผ๊ณ ๊ธฐ ๋๋ฉด์, ์ฃฝ์ด์์ผ๋ฉด PASS
- ์ด์์๋ค๋ฉด ๐
1) ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ์ผ๋ก ์ด๋ ๊ฐ๋ฅํ ์ง ํ์ธ1-1) ๊ฐ๋ฅํ๊ณ , ๋ฌผ๊ณ ๊ธฐ ์๋ค๋ฉด ๐ ๊ทธ๋๋ก ์ด๋ 1-2) ๊ฐ๋ฅํ๊ณ , ๋ค๋ฅธ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์๋ค๋ฉด ๐ ๋ ๋ฌผ๊ณ ๊ธฐ ๊ต์ฒด 1-3) ๊ฐ๋ฅํ์ง ์๋ค๋ฉด, ๋ฐ์๊ณ๋ฐฉํฅ 45๋ ํ์ ๐ ๋ชจ๋ ๋ฐฉํฅ์ด ๋ถ๊ฐํ๋ฉด PASS
2-2) ์์ด ์ด๋
moveShark(sx, sy, sd, eat)
- ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋๋ฐ,
nx = sx + dx[sd] * i
๋ก i (1<=i<4)๋ฅผ ๊ณฑํด์ค์ผ๋ก์จ, ์ฌ๋ฌ ์นธ ์ด๋ - map[nx][ny]์ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์๋ค๋ฉด, ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน๊ณ ์ด๋
- ๋จน์ ์ํ๋ก
DFS(nx, ny, nd, eat + eatFish)
ํธ์ถ - ๊ทธ ํ, ์๋จน์ ์ํ๋ก ๋๋๋ฆฐ๋ค.
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main_BOJ_19236_์ฒญ์๋
_์์ด {
static class Fish{
int num, x, y, dir;
boolean alive;
public Fish(int num, int x, int y, int dir, boolean alive) {
this.num = num;
this.x = x;
this.y = y;
this.dir = dir;
this.alive = alive;
}
}
static int[][] map;
static Fish[] fishes;
static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1};
static int[] dy = {0, -1, -1, -1, 0, 1, 1, 1};
static int amountOfEating = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer;
map = new int[4][4];
fishes = new Fish[17];
for (int i = 0; i < 4; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 0; j < 4; j++) {
int num = Integer.parseInt(stringTokenizer.nextToken());
int dir = Integer.parseInt(stringTokenizer.nextToken()) - 1;
map[i][j] = num;
fishes[num] = new Fish(num, i, j, dir, true);
}
}
int sx = 0, sy = 0;
int sd = fishes[map[0][0]].dir;
int eat = map[0][0];
fishes[map[0][0]].alive = false;
map[0][0] = -1;
DFS(sx, sy, sd, eat);
System.out.println(amountOfEating);
}
private static void DFS(int sx, int sy, int sd, int eat) {
amountOfEating = Math.max(amountOfEating, eat);
int[][] tmpMap = new int[4][4];
for (int i = 0; i < 4; i++) {
// System.arraycopy(map[i], 0, tmpMap[i], 0, map.length);
tmpMap[i] = Arrays.copyOf(map[i], 4);
}
Fish[] tmpFishes = new Fish[17];
for (int i = 1; i < 17; i++) {
tmpFishes[i] = new Fish(fishes[i].num, fishes[i].x, fishes[i].y, fishes[i].dir, fishes[i].alive);
}
moveFishes();
moveShark(sx, sy, sd, eat);
for (int i = 0; i < 4; i++) {
// System.arraycopy(tmpMap[i], 0, map[i], 0, 4);
map[i] = Arrays.copyOf(tmpMap[i], 4);
}
for (int i = 1; i < 17; i++) {
fishes[i] = new Fish(tmpFishes[i].num, tmpFishes[i].x, tmpFishes[i].y, tmpFishes[i].dir, tmpFishes[i].alive);
}
}
private static void moveShark(int sx, int sy, int sd, int eat) {
for (int i = 1; i < 4; i++) {
int nx = sx + dx[sd] * i;
int ny = sy + dy[sd] * i;
if(nx < 0 || ny < 0 || 4 <= nx || 4 <= ny || map[nx][ny] == 0) continue;
int eatFish = map[nx][ny];
int nd = fishes[eatFish].dir;
map[sx][sy] = 0;
map[nx][ny] = -1;
fishes[eatFish].alive = false;
DFS(nx, ny, nd, eat + eatFish);
fishes[eatFish].alive = true;
map[sx][sy] = -1;
map[nx][ny] = eatFish;
}
}
private static void moveFishes() {
for (int i = 1; i < 17; i++) {
if(!fishes[i].alive) continue;
int cnt = 0;
int dir = fishes[i].dir;
int nx = 0, ny = 0;
while (cnt < 8) {
nx = fishes[i].x + dx[dir];
ny = fishes[i].y + dy[dir];
if(nx < 0 || ny < 0 || 4 <= nx || 4 <= ny || map[nx][ny] == -1){
dir = (dir + 1) % 8;
fishes[i].dir = dir;
cnt += 1;
continue;
}
else if (map[nx][ny] == 0) {
map[fishes[i].x][fishes[i].y] = 0;
fishes[i].x = nx;
fishes[i].y = ny;
map[nx][ny] = i;
} else {
int changeFish = fishes[map[nx][ny]].num;
fishes[changeFish].x = fishes[i].x;
fishes[changeFish].y = fishes[i].y;
map[fishes[changeFish].x][fishes[changeFish].y] = changeFish;
fishes[i].x = nx;
fishes[i].y = ny;
map[nx][ny] = i;
}
break;
}
}
}
}
'์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ BOJ / ๋ฐฑ์ค 20061 ] ๋ชจ๋ ธ๋ฏธ๋ ธ๋๋ฏธ๋ ธ 2 ( ์๋ฐ / JAVA ) (0) | 2022.02.12 |
---|---|
[ BOJ / ๋ฐฑ์ค 17143 ] ๋์์ ( ์๋ฐ / JAVA ) (0) | 2022.02.09 |
[ ๋ฐฑ์ค / BOJ 19237 ] ์ด๋ฅธ ์์ด ( ์๋ฐ / JAVA ) (0) | 2022.02.08 |
[ ๋ฐฑ์ค / BOJ 17822 ] ์ํ ๋๋ฆฌ๊ธฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.07 |
[ BOJ / ๋ฐฑ์ค 20058 ] ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด์คํฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.05 |