forked from 2021-Algorithm/algorithms_book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package ch19_samsung; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
//https://www.acmicpc.net/problem/19236 | ||
public class Q47_YoungShark { | ||
public static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1}; | ||
public static int[] dy = {0, -1, -1, -1, 0, 1, 1, 1}; | ||
public static int result = 0; | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
Node[][] graph = new Node[4][4]; | ||
for (int i = 0; i < 4; i++) { | ||
String[] input = sc.nextLine().split(" "); | ||
for (int j = 0; j < 4; j++) { | ||
graph[i][j] = new Node(Integer.parseInt(input[j * 2]), Integer.parseInt(input[j * 2 + 1]) - 1); | ||
} | ||
} | ||
dfs(graph, 0, 0, 0); | ||
System.out.println(result); | ||
} | ||
|
||
public static int turnLeft(int d) { | ||
return (d + 1) % 8; | ||
} | ||
|
||
public static int[] findFish(Node[][] graph, int fishNum) { | ||
for (int i = 0; i < 4; i++) { | ||
for (int j = 0; j < 4; j++) { | ||
if (graph[i][j].fishNum == fishNum) { | ||
return new int[]{i, j}; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static Node[][] moveFish(Node[][] graph, int sh_x, int sh_y) { | ||
for (int i = 0; i < 17; i++) { | ||
int[] position = findFish(graph, i); | ||
if (position != null) { | ||
int x = position[0]; | ||
int y = position[1]; | ||
int d = graph[x][y].d; | ||
for (int j = 0; j < 8; j++) { | ||
int nx = x + dx[d]; | ||
int ny = y + dy[d]; | ||
if (0 <= nx && nx < 4 && 0 <= ny && ny < 4) { | ||
if (!(nx == sh_x && ny == sh_y)) { | ||
graph[x][y].d = d; | ||
Node temp = graph[nx][ny]; | ||
graph[nx][ny] = graph[x][y]; | ||
graph[x][y] = temp; | ||
break; | ||
} | ||
} | ||
d = turnLeft(d); | ||
} | ||
} | ||
} | ||
return graph; | ||
} | ||
|
||
public static List<int[]> possiblePositions(Node[][] graph, int sh_x, int sh_y) { | ||
List<int[]> positions = new ArrayList<>(); | ||
int d = graph[sh_x][sh_y].d; | ||
for (int i = 0; i < 4; i++) { | ||
sh_x += dx[d]; | ||
sh_y += dy[d]; | ||
if (0 <= sh_x && sh_x < 4 && 0 <= sh_y && sh_y < 4) { | ||
if (graph[sh_x][sh_y].fishNum != -1) { | ||
positions.add(new int[]{sh_x, sh_y}); | ||
} | ||
} | ||
} | ||
return positions; | ||
} | ||
|
||
public static void dfs(Node[][] graph, int sh_x, int sh_y, int ate) { | ||
ate += graph[sh_x][sh_y].fishNum; | ||
graph = copyGraph(graph); | ||
graph[sh_x][sh_y].fishNum = -1; | ||
Node[][] new_graph = moveFish(graph, sh_x, sh_y); | ||
List<int[]> positions = possiblePositions(new_graph, sh_x, sh_y); | ||
if (positions.size() == 0) { | ||
result = Math.max(ate, result); | ||
return; | ||
} | ||
for (int[] position : positions) { | ||
dfs(new_graph, position[0], position[1], ate); | ||
} | ||
} | ||
|
||
public static Node[][] copyGraph(Node[][] graph) { | ||
Node[][] newGraph = new Node[4][4]; | ||
for (int i = 0; i < 4; i++) { | ||
for (int j = 0; j < 4; j++) { | ||
newGraph[i][j] = new Node(graph[i][j].fishNum, graph[i][j].d); | ||
} | ||
} | ||
return newGraph; | ||
} | ||
} | ||
|
||
class Node { | ||
int fishNum; | ||
int d; | ||
|
||
public Node(int fishNum, int d) { | ||
this.fishNum = fishNum; | ||
this.d = d; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
## https://www.acmicpc.net/problem/19236 | ||
import copy | ||
|
||
def solution(): | ||
graph = [[] for _ in range(4)] | ||
fishes = [() for _ in range(17)] | ||
for i in range(4): | ||
input_list = list(map(int, input().split())) | ||
for j in range(0, 7, 2) : | ||
## 해당 위치에 들어갈 숫자, 방향 | ||
graph[i].append((input_list[j], input_list[j+1]-1)) | ||
## 인덱스를 물고기 번호로 갖는 물고기의 위치 x, u, 방향 | ||
fishes[input_list[j]] = (i, j//2) | ||
|
||
to_eat = graph[0][0][0] | ||
shark_d = graph[0][0][1] | ||
shark = (0, 0, shark_d) | ||
graph[0][0] = (-1, shark_d) | ||
fishes[to_eat] = () | ||
ate_list = [] | ||
|
||
def dfs(graph, shark, fishes, ate): | ||
graph = copy.deepcopy(graph) | ||
fishes = copy.deepcopy(fishes) | ||
|
||
graph, fishes = fish_move(graph, fishes) | ||
to_eat = shark_eat(graph, shark) | ||
if to_eat==[] : | ||
ate_list.append(ate) | ||
return | ||
sh_old_x = shark[0] | ||
sh_old_y = shark[1] | ||
sh_old_d = shark[2] | ||
for candidate in to_eat: | ||
x, y = candidate | ||
new_sh_d = graph[x][y][1] | ||
fish = graph[x][y][0] | ||
fishes[fish] = () | ||
graph[x][y] = (-1, new_sh_d) | ||
graph[sh_old_x][sh_old_y] = (0, 0) | ||
shark = (x, y, new_sh_d) | ||
ate += fish | ||
dfs(graph, shark, fishes, ate) | ||
ate -= fish | ||
shark = (sh_old_x, sh_old_y, sh_old_d) | ||
graph[sh_old_x][sh_old_y] = (-1, sh_old_d) | ||
graph[x][y] = (fish, new_sh_d) | ||
fishes[fish] = (x, y) | ||
|
||
dfs(graph, shark, fishes, to_eat) | ||
ate_list.sort(reverse=True) | ||
return ate_list[0] | ||
|
||
def fish_move(graph, fishes): | ||
move = [(-1, 0), (-1, -1), (0,-1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1)] | ||
for f, fish in enumerate(fishes): | ||
if fish == () : | ||
continue | ||
x, y = fish | ||
d = graph[x][y][1] | ||
for i in range(d, d+8): | ||
ni = i | ||
if ni > 7: | ||
ni -= 8 | ||
nx = x + move[ni][0] | ||
ny = y + move[ni][1] | ||
if 0<=nx<4 and 0<=ny<4 and graph[nx][ny][0] != -1 : | ||
graph[x][y] = (f, ni) | ||
other_fish = graph[nx][ny][0] | ||
graph[nx][ny], graph[x][y] = graph[x][y], graph[nx][ny] | ||
fishes[f] = (nx, ny) | ||
if other_fish != 0 : | ||
fishes[other_fish] = (x, y) | ||
break | ||
return graph, fishes | ||
|
||
def shark_eat(graph, shark): | ||
move = [(-1, 0), (-1, -1), (0,-1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1)] | ||
sh_x, sh_y, sh_d = shark | ||
nx = sh_x | ||
ny = sh_y | ||
to_eat = [] | ||
while True: | ||
nx = nx + move[sh_d][0] | ||
ny = ny + move[sh_d][1] | ||
if 0 <= nx < 4 and 0 <= ny < 4: | ||
if graph[nx][ny][0] != 0 : | ||
to_eat.append((nx, ny)) | ||
else: | ||
break | ||
return to_eat | ||
|
||
print(solution()) | ||
|
||
|